Java generates a QR code through QRCode

I. Introduction

QR Code is a kind of matrix two-dimensional code symbol developed by Denso in September 1994. It has large information capacity, high reliability, and can represent Chinese characters and images with one-dimensional bar codes and other two-dimensional bar codes. A variety of text information, strong confidentiality and anti-counterfeiting advantages.

Download the QRCode.jar package first: https://pan.baidu.com/s/1Pb9XzWKhumgwaYrE90vyWg

Two, code examples

1. Generate a QR code

//加密:文字信息 -> 二维码
public static void encoderQRCode(String content, String imgPath, String imgType, int size) throws Exception{
    //RenderedImage是一个接口,因此要找到它的实现类 BufferedImage
    //RenderedImage bufferedImage = null;
    //代表内存中的一张图片
    BufferedImage bufferedImage = generateQRCodeCommon(content, imgType, size);
    //设置图片格式,与输出的路径
    ImageIO.write(bufferedImage, "jpg", new File("H:/qrcode.jpg"));
}

//产生一个二维码的BufferedImage
private static BufferedImage generateQRCodeCommon(String content, String imgType, int size) throws Exception{
    //QRCode对象:字符串转为boolean[][]
    Qrcode qrcode = new Qrcode();
    //设置二维码的排错率
    /**
     * 纠错等级分为
     * level L : 最大 7% 的错误能够被纠正;
     * level M : 最大 15% 的错误能够被纠正;
     * level Q : 最大 25% 的错误能够被纠正;
     * level H : 最大 30% 的错误能够被纠正;
     */
    qrcode.setQrcodeErrorCorrect('M');
    qrcode.setQrcodeEncodeMode('B');//注意版本信息 N代表数字 、A代表 a-z,A-Z、B代表 其他)
    qrcode.setQrcodeVersion(size);//尺寸  1-40

    boolean[][] codeOuts = qrcode.calQrcode(content.getBytes("gbk"));

    int imgSize = 67 + 12 * (size - 1);

    BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB);
    //生成一个画板
    Graphics2D graphics = bufferedImage.createGraphics();
    graphics.setBackground(Color.WHITE);//将画板的背景色设置为白色
    graphics.clearRect(0, 0, imgSize, imgSize);//初始化
    graphics.setColor(Color.BLACK);//设置画板上图像的颜色

    int pixoff = 2;
    for(int j = 0;j<codeOuts.length;j++){
        for(int i = 0;i<codeOuts.length;i++){
            if (codeOuts[j][i]) {
                graphics.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3);
            }
        }
    }
    //增加logo
    Image logo = ImageIO.read(new File("H:\\CSDN\\哪吒.jpg"));
    int maxWidth = bufferedImage.getWidth();
    int maxHeight = bufferedImage.getHeight();
    graphics.drawImage(logo, imgSize/5*2, imgSize/5*2, maxWidth/5, maxHeight/5, null);
    graphics.dispose();//释放空间
    bufferedImage.flush();//清理
    return bufferedImage;
}

Note: The most important thing to type code is the idea. Start with ImageIO.write(bufferedImage, "jpg", new File("H:/qrcode.jpg"));, build the parameters in turn, because RenderedImage is an interface, so you have to find Its implementation class BufferedImage,

2. Analyze the QR code

//解密
public static String decoderQRCode(String imgPath) throws IOException {
    //硬盘中图片加载入内存
    BufferedImage bufferedImage = ImageIO.read(new File(imgPath));
    //解密
    QRCodeDecoder qrCodeDecoder = new QRCodeDecoder();
    QRCodeImage qrCodeImage = new QRCodeImageImpl(bufferedImage);
    byte[] decode = qrCodeDecoder.decode(qrCodeImage);
    return new String(decode, "gbk");
}

 

package com.guor.qrcode;

import jp.sourceforge.qrcode.data.QRCodeImage;

import java.awt.image.BufferedImage;

public class QRCodeImageImpl implements QRCodeImage {
    BufferedImage bufferedImage;//内存中的二维码
    public QRCodeImageImpl(BufferedImage bufferedImage){
        this.bufferedImage = bufferedImage;
    }

    @Override
    public int getWidth() {
        return bufferedImage.getHeight();
    }

    @Override
    public int getHeight() {
        return bufferedImage.getHeight();
    }

    //像素
    @Override
    public int getPixel(int x, int y) {
        return bufferedImage.getRGB(x, y);
    }
}

3. Testing

package com.guor.qrcode;

public class QrcodeTest {
    public static void main(String[] args) throws Exception {
        String content = "https://blog.csdn.net/guorui_java/article/details/112391105";//内容信息
        String path = "H:/qrcode.jpg";
        //加密:文字信息 -> 二维码
        QRCodeUtil.encoderQRCode(content, path, "png", 20);
        //解密
        String decoderQRCode = QRCodeUtil.decoderQRCode(path);
        System.out.println(decoderQRCode);
    }
}

4. Generated QR code

After sweeping out the magic, I really jumped to the summary of my Java knowledge system (2021 version) blog

Parsed text:

Three, the problems encountered

At the beginning, when decrypting, there was a bug,

I searched on the Internet and found that the size of the logo inserted in the middle was too large, so reduce it a bit and it will be fine.

 

Highlights from previous issues:

Summary of Java knowledge system (2021 version)

Summary of basic knowledge of Java multithreading (absolutely classic)

Super detailed springBoot study notes

Summary of common data structures and algorithms

Java design patterns: a comprehensive analysis of 23 design patterns (super detailed)

Summary of Java interview questions (with answers)

Guess you like

Origin blog.csdn.net/guorui_java/article/details/114709179