ZXing开发彩色二维码

为了解决彩色二维码问题,提供一个自己开发的工具类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());
}
}

 

参考地址:

ZXing工具生成二维码以及解析二维码

Java 彩色个性化二维码

如何用#000000格式更改BufferedImage的颜色