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的颜色

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值