使用zxing解析二维码图片

在上一篇文章中,分享了立牌二维码的生成,本篇文章介绍采用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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值