ZXING QR Coder
ZXING 生成和读取二维码
package com.example.java11boot.im;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class Ttest2 {
public static void main(String[] args) {
}
public static void make() throws Exception {
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, "1");// 外边距
int width = 200;
int height = 200;
String content = "Hello World By 中国";
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
Path path = Paths.get("D:/hello.PNG");
MatrixToImageWriter.writeToPath(bitMatrix,"PNG", path);
}
public static void read () throws Exception {
QRCodeReader qrCodeReader = new QRCodeReader();
BufferedImage bufferedImage = ImageIO.read(new File("D:/hello.PNG"));
LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
Map<DecodeHintType, String> hints2 = new HashMap<>();
hints2.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = qrCodeReader.decode(binaryBitmap, hints2);
System.out.println(result.getText());
}
}
=========================================================================================
@GetMapping("/")
public void qr(HttpServletResponse httpServletResponse) throws Exception {
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, "1");// 外边距
int width = 200;
int height = 200;
String content = "Hello World By 中国";
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
ServletOutputStream outputStream = httpServletResponse.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix,"PNG", outputStream);
outputStream.flush();
outputStream.close();
}
=========================================================================================
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>