谷歌zxing 二维码生成工具
2024-10-16 05:42:18
一、加入maven依赖
<!-- 谷歌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>
二、工具类代码
package com.example.demo.utils; import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
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 com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Hashtable; /**
* @author zsh
* @company wlgzs
* @create 2019-03-10 15:17
* @Describe 二维码生成和读的工具类
*/
public class QrCodeCreateUtil {
/**
* 生成包含字符串信息的二维码图片
* @param outputStream 文件输出流路径
* @param content 二维码携带信息
* @param qrCodeSize 二维码图片大小
* @param imageFormat 二维码的格式
* @throws WriterException
* @throws IOException
*/
public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
//设置二维码纠错级别MAP
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); // 矫错级别
QRCodeWriter qrCodeWriter = new QRCodeWriter();
//创建比特矩阵(位矩阵)的QR码编码的字符串
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// 使BufferedImage勾画QRCode (matrixWidth 是行二维码像素点)
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// 使用比特矩阵画并保存图像
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i-100, j-100, 1, 1);
}
}
}
return ImageIO.write(image, imageFormat, outputStream);
} /**
* 读二维码并输出携带的信息
*/
public static void readQrCode(InputStream inputStream) throws IOException{
//从输入流中获取字符串信息
BufferedImage image = ImageIO.read(inputStream);
//将图像转换为二进制位图源
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
QRCodeReader reader = new QRCodeReader();
Result result = null ;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
e.printStackTrace();
}
System.out.println(result.getText());
}
/**
* 测试代码
* @throws WriterException
*/
public static void main(String[] args) throws IOException, WriterException { createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"WE1231238239128sASDASDSADSDWEWWREWRERWSDFDFSDSDF123123123123213123",900,"JPEG");
readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
}
}
效果图:
最新文章
- js 对象的_proto_
- ImageLightbox.js – 响应式的图片 Lightbox 插件
- JS原型与原型链终极详解
- JSP内置对象-request
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
- Linux下集群的搭建
- Integer取值范围和NumberFormatException的解决
- ListFragment
- 初学 Canvas <;第一篇-基础篇>;
- 转:SVN Eclipse插件Subclipse安装和配置
- c#第五次作业---正文提取
- linux下的文件权限
- 在MFC中支持sqlite3
- Java开发微服务为什么一定要选spring cloud?
- Android 常见异常及解决办法
- JMeter 生成精度度为分钟的时间戳文件名
- 通过shell进行数学计算
- dom4j之selectSingleNode方法
- 【BZOJ】3173: [Tjoi2013]最长上升子序列(树状数组)
- PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
热门文章
- 多模块项目提示“Module ** must not contain source root **. The root already belongs to module **”的解决办法
- Bug 5323844-IMPDP无法导入远程数据库同义词的同义词
- Windows搭建react-native开发环境
- Nginx使用rewrite重新定向
- hbase-java-api002(flush)
- uvm设计分析——tlm
- <;2>;Cocos Creator文件结构
- 八 原型prototype和__proto__
- codefroces 266
- MQTT 发布者订阅者