爲了解決彩色二維碼問題,提供一個自己開發的工具類QRCodeUtil:
下載jar包
<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.0</version>
</dependency>
工具類開發
package com.tencent.wll.rqdj.util;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
public class QRCodeUtil {
public static final QRCodeWriter QR_CODE_WRITER = new QRCodeWriter();
/**
* 生成二維碼 字節數組
* @param contents 內容
* @param format 格式(如png)
* @param width 寬
* @param height 高
* @return 二維碼字節數組
*/
public static byte[] generate(String contents, String format, int width, int height) throws WriterException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
BitMatrix bitMatrix = QR_CODE_WRITER.encode(contents, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(bitMatrix, format, os);
return os.toByteArray();
} catch (IOException e) {
throw new RuntimeException(String.format("fail to writeToStream when generating qr code: text[%s]", contents), e);
}
}
/**
* 生成彩色二維碼 字節數組
* @param contents 內容
* @param format 格式(如png)
* @param width 寬
* @param height 高
* @param onColor 二維碼顏色
* @param offColor 背景色
* @return 二維碼字節數組
*/
public static byte[] generateWithColor(String contents, String format, int width, int height, int onColor, int offColor) throws WriterException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 0);
BitMatrix matrix = (new MultiFormatWriter()).encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
// 二維碼爲紅色,背景色爲綠色
BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix, new MatrixToImageConfig(onColor, offColor));
ImageIO.write(image, "png", os);
return os.toByteArray();
} catch (IOException e) {
throw new RuntimeException(String.format("fail to writeToStream when generating qr code: text[%s]", contents), e);
}
}
public static void main(String[] args) throws WriterException {
byte[] qrcode = QRCodeUtil.generateWithColor("{\"trip_id\":1177290,\"plate_type\":9,\"plate_number\":\"WC8760\"," +
"\"trip_code\":\"2592\"}", "png",
250, 250, Color.RED.getRGB(), Color.GREEN.getRGB());
byte[] encode = Base64.getEncoder().encode(qrcode);
String s1 = new String(encode);
System.out.println(s1.length());
StringBuilder sb = new StringBuilder("<img id=\"img1\" src=\"data:image/png;base64,");
sb.append(s1).append("\"/>");
System.out.println(sb.toString());
}
}
參考地址: