基于Google的zxing生成和解析QR Code
介绍
二维码的技术在当今应用相当的的广泛,其中以QR Code应用最为广泛和流行。
在二维码发展之前还有一维码即条形码,相比条形码二维码存储容量更大,并且容错性也更好。
优点
1、高密度编码,信息容量大(大概能存储几百上千个汉字)
2、编码范围广
3、容错能力强,具有纠错功能
4、译码可靠性高
5、可引入加密措施
6、成本低,易制作,持久耐用
缺点
1、安全问题,可能成为一些病毒入侵新渠道
备注:主要是安全问题
基于zxing生成和解析二维码
maven pom依赖
<!-- 二维码zxing组件 -->
<!-- 核心组件 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<!-- javase扩展组件 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
备注:一定不能忘记javase扩展组件
封装QrCodeUtils工具类
/**
* <h3>QRCode工具类</h3>
*
* 1、生成二维码<br>
* 2、解析二维码<br>
*
* @author xuyi3
* @2016年8月30日 @上午8:49:21
* @QrUtils
* @功能说明:<br>
* @春风十里不如你
* @备注
*/
public class QrCodeUtils
{
/**
* <h3>生成二维码</h3>
*
* @param width 宽度(像素)
* @param height 高度(像素)
* @param content 文本内容
* @param charset 文本字符编码
* @param path 存放二维码路径
* @param imageFormat 图片格式(jpg/png...)
* @throws Exception
*/
public static void createQRCode(int width, int height, String content, String charset, String path,
String imageFormat) throws Exception
{
// 设置二维码一些配置信息
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, charset);//设置二维码内容编码格式
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);//设置二维码容错级别(L、M、Q、H)
//将内容编码为指定的二维矩阵图像
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
//使用MatrixToImageWriter类的静态方法将二维矩阵写入到输出流或指定路径path
MatrixToImageWriter.writeToStream(bitMatrix, imageFormat, new FileOutputStream(new File(path)));
//MatrixToImageWriter.writeToPath(bitMatrix, imageFormat, new File("").toPath());
}
/**
* <h3>解析二维码内容</h3>
*
* @param imagePath 待解析的二维码图片存放路径
* @return
* @throws Exception
*/
public static String decodeQRCode(String imagePath) throws Exception
{
// 其实下面这四步顺着推是很难推出来的,反着推倒比较简答,不过完全没必要记住,知道大概就行了
BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
//使用MultiFormatReader将二维码图片解析为内容对象
MultiFormatReader multiFormatReader = new MultiFormatReader();
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = multiFormatReader.decode(binaryBitmap, hints);
return result.getText();
}
}
main code
/**
*
* @author xuyi3
* @2016年8月30日 @上午9:51:44
* @Main
* @功能说明:<br>
* @春风十里不如你
* @备注
*/
public class Main
{
public static void main(String[] args) throws Exception
{
String content = "http://www.baidu.com";
String path = "e:/test.png";
//创建二维码
QrCodeUtils.createQRCode(300, 300, content, "utf-8", path, "png");
//解析二维码
String string = QrCodeUtils.decodeQRCode(path);
System.out.println(string);
}
}
总结
借助于zxing生成和解析二维码还是比较简单的,有时间可以看看二维码的发展和其实现原理。
本质上就是一种编码和解码,只不过其有自己的规范。
参考
1、https://github.com/zxing/zxing