小知识,大挑战!本文正在参与“ 程序员必备小知识 ”创作活动
pom文件引入google 二维码工具包依赖
<!-- 二维码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
java生成二维码图片和解析二维码图片内容代码
import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;
import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.tarzan.reptile.utils.SmartBeanUtil;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class QrCodeUtil {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
//视频保存目录
private static final String qrCodeSavePath = "d:/二维码/";
public static void main(String[] args) {
String text = "https://www.baidu.com/";
try {
//生成二维码图片
String pathName = generateQRCode(text);
System.out.println("生成二维码的图片存放路径: " + pathName);
//解析二维码图片
String content = parseQRCode(pathName);
System.out.println("解析内容为: " + content);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 生成二维码图片
*
* @param text
* @return
*/
public static String generateQRCode(String text) throws Exception {
return generateQRCode(text, 300, 300, "jpg", qrCodeSavePath + SmartBeanUtil.genUUID() + ".jpg");
}
/**
* 解析指定路径下的二维码图片
*
* @param filePath
* @return
*/
private static String parseQRCode(String filePath) {
String content = "";
try {
File file = new File(filePath);
BufferedImage image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
MultiFormatReader formatReader = new MultiFormatReader();
Result result = formatReader.decode(binaryBitmap, hints);
//设置返回值
content = result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return content;
}
/**
* 根据内容,生成指定宽高、指定格式的二维码图片
*
* @param text 内容
* @param width 宽
* @param height 高
* @param format 图片格式
* @return 生成的二维码图片路径
* @throws Exception
*/
public static String generateQRCode(String text, int width, int height, String format, String pathName) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
File outputFile = new File(pathName);
if (!outputFile.exists()) {
outputFile.mkdirs();
}
writeToFile(bitMatrix, format, outputFile);
return pathName;
}
//输出为文件
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("文件写入异常");
}
}
//输出为流
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("文件写入异常");
}
}
//缓冲图片
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
}
控制台输出
生成二维码图片效果