使用zxing解析二维码图片

来源:互联网 发布:高中网络辅导班哪家好 编辑:程序博客网 时间:2024/04/29 13:39

在上一篇文章中,分享了立牌二维码的生成,本篇文章介绍采用zxing解析二维码图片。

下面是具体的步骤:

1.在解析的时,需要使用到javase.jar包中的BufferedImageLuminanceSource类,所以需先在工程中导入javase.jar包。

下载地址:http://download.csdn.net/detail/mr_smile2014/9715554

2.下面提供传图片地址和传图片BufferedImage的方式,代码如下:


/** *  * @Title: deqrcode * @Description: 解析二维码 * @param imgPath *            二维码图片路径 * @return void 返回类型 * @throws */@SuppressWarnings({ "rawtypes", "unchecked" })public static String deqrcode(String imgPath) {try {File file = new File(imgPath);// 获取该图片文件BufferedImage image = ImageIO.read(file);if (image == null) {System.out.println("未获取到图片对象");}LuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable hints = new Hashtable();// 将图片反解码为二维矩阵hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = new MultiFormatReader().decode(bitmap, hints);// 将该二维矩阵解码成内容String resultStr = result.getText();return resultStr;} catch (Exception ex) {ex.printStackTrace();}return null;}/** *  * @Title: deqrcode * @Description: 解析二维码图片 * @param image *            二维码图片对象 * @return String 二维码文本值 * @throws */@SuppressWarnings({ "rawtypes", "unchecked" })public static String deqrcode(BufferedImage image) {try {if (image == null) {System.out.println("image参数不能为空");}LuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Hashtable hints = new Hashtable();// 将图片反解码为二维矩阵hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");Result result = new MultiFormatReader().decode(bitmap, hints);// 将该二维矩阵解码成内容String resultStr = result.getText();return resultStr;} catch (Exception ex) {ex.printStackTrace();}return null;}

3.测试:

测试的二维码图片:



测试的代码:

public class codeTest {public static void main(String[] args) throws Exception {// 根据图片文件路径解析二维码String result = QCodeUtils.deqrcode("F:/qrcode/text/111.png");System.out.println("文件地址解析结果:"+result);// 根据图片BufferedImage对象解析二维码File file = new File("F:/qrcode/text/111.png");// 获取该图片文件BufferedImage image = ImageIO.read(file);String result1 = QCodeUtils.deqrcode(image);System.out.println("image对象解析结果:"+result1);}}


4.测试效果:



---------------------------------------------------------------------------版权声明------------------------------------------------------------------------------------------

版权声明:本文为博主原创文章,未经博主允许不得转载。博客地址:http://blog.csdn.net/mr_smile2014


4 0
原创粉丝点击