Java simply generates/parses two-dimensional code (zxing qrcode)

Hi I'm Shendi


Java simply generates/parses two-dimensional code (zxing qrcode)

I used qrcode.js to generate QR codes before, but there will inevitably be some compatibility issues on different devices, so I changed to the backend (Java) to generate QR code images


Google's zxing package is used here



Jar package import


Github:https://github.com/zxing/zxing


Introduced via Maven

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.5.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.5.1</version>
</dependency>

You can also download jars (javase and core) directly in the maven repository

For example, Alibaba cloud maven warehouse

https://developer.aliyun.com/mvn/search


insert image description here


Or search "Arsenic and Tellurium Knowledge Warehouse" on WeChat, send "100001" to download the jar package directly




Generate QR code

Three simple steps to generate a QR code

  1. Create QRCodeWriter
  2. Create a QR code
  3. output QR code

For example, to output the QR code to H:/1.png, the code is as follows

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

/** @author Shendi */
public class Test {
    
    

	public static void main(String[] args) throws Exception {
    
    
		QRCodeWriter qcWrite = new QRCodeWriter();
		// encode函数的四个参数分别是 内容,格式,宽,高
		BitMatrix bm = qcWrite.encode("https://sdpro.top", BarcodeFormat.QR_CODE, 500, 500);
		// 输出到 H:/1.png, writeToPath有三个参数,(BitMatrix,格式,输出地址)
		MatrixToImageWriter.writeToPath(bm, "PNG", Path.of("H:/1.png"));
	}
	
}

After the code runs, the effect is as follows

insert image description here



If you want to convert the QR code into a byte array, you can use writeToStream of MatrixToImageWriter to output to the stream




QR code interface

For example, pass a string to convert the string into a QR code and output it (http). Here, an interface written in the form of SpringBoot is used for direct copying. The code is as follows

/**
 * 生成二维码.
 * @param content	二维码内容
 * @param req		请求
 * @param resp		响应
 */
@GetMapping("/code")
public void create(String content, HttpServletRequest req, HttpServletResponse resp) {
    
    

    QRCodeWriter qcWrite = new QRCodeWriter();

    try {
    
    
        BitMatrix bm = qcWrite.encode(content, BarcodeFormat.QR_CODE, 500, 500);

        // 设置响应为图片数据
        resp.setContentType("image/png");

        MatrixToImageWriter.writeToStream(bm, "PNG", resp.getOutputStream());
    } catch (Exception e) {
    
    
        e.printStackTrace();
    }

}



Parse the QR code

Parse the QR code through QRCodeReader, for example, parse the QR code just generated, the code is as follows

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

/** @author Shendi */
public class Test {
    
    

	public static void main(String[] args) throws Exception {
    
    
		QRCodeReader qcReader = new QRCodeReader();
		// 这里的ImageIO也可以read输入流
		BufferedImage img = ImageIO.read(new File("H:/1.png"));
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(img)));
		Result result = qcReader.decode(bitmap);
		System.out.println("二维码内容: " + result.getText());
	}
	
}

The operation effect is as follows

insert image description here




END

Guess you like

Origin blog.csdn.net/qq_41806966/article/details/129264363