相关依赖
本文二维码生成采用Google的Zxing相关依赖进行生成。
<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>
介绍
本文为本人使用Java进行I/O练习时所编写的工具类,该工具类有两个方法一个为生成二维码一个是将二维码的内容解析,详细代码内容如下。
生成二维码
public static Path createQrFile(String content, int qrWidth, int qrHeight) throws WriterException, IOException {
Hashtable<EncodeHintType, Object> hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
File directory = new File(createFilePath);
if (!directory.exists()){
directory.mkdirs();
}
File file = new File(createFilePath + "/" + new Date().getTime() + ".png");
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(image, "png", file);
return file.toPath();
}
解析二维码
public static String qrToContent(File file){
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("File read failure");
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(hybridBinarizer);
MultiFormatReader multiFormatReader = new MultiFormatReader();
Result result;
try {
result = multiFormatReader.decode(bitmap);
return result.getText();
} catch (NotFoundException e) {
throw new RuntimeException("Qr code cannot be decoded");
}
}
工具类全部代码
package utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Date;
import java.util.Hashtable;
public class QrAnalysisUtils {
private static String createFilePath = "D:/createFile";
public static String qrToContent(File file){
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("File read failure");
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
HybridBinarizer hybridBinarizer = new HybridBinarizer(source);
BinaryBitmap bitmap = new BinaryBitmap(hybridBinarizer);
MultiFormatReader multiFormatReader = new MultiFormatReader();
Result result;
try {
result = multiFormatReader.decode(bitmap);
return result.getText();
} catch (NotFoundException e) {
throw new RuntimeException("Qr code cannot be decoded");
}
}
public static Path createQrFile(String content, int qrWidth, int qrHeight) throws WriterException, IOException {
Hashtable<EncodeHintType, Object> hints = new Hashtable();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, qrWidth, qrHeight, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
}
}
File directory = new File(createFilePath);
if (!directory.exists()){
directory.mkdirs();
}
File file = new File(createFilePath + "/" + new Date().getTime() + ".png");
if (!file.exists()) {
file.createNewFile();
}
ImageIO.write(image, "png", file);
return file.toPath();
}
}