在上一篇文章中,分享了立牌二维码的生成,本篇文章介绍采用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.测试效果:
---------------------------------------------------------------------------版权声明------------------------------------------------------------------------------------------