使用zxing解析二维码图片

本文介绍了如何使用Zxing库解析二维码图片。通过两种方式实现:一是传入二维码图片路径;二是直接传入BufferedImage对象。提供了完整的代码示例,并展示了实际的测试效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

下面是具体的步骤:

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

下载地址:http://download.youkuaiyun.com/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.youkuaiyun.com/mr_smile2014


### 使用ZXING解析二维码 #### 准备工作 为了使用ZXing解析二维码,需先确保环境配置完成。如果是在Java环境中操作,则可以通过Maven引入依赖[^2]。 对于其他编程语言或环境,如Delphi,可以参考特定的安装指南并集成相应的ZXing版本,比如通过克隆或下载`DelphiZXingQRCode`库文件至项目中,并将其单元文件加入到工程里以便调用其功能函数[^3]。 #### Java环境下ZXing解析二维码示例代码 下面给出一段简单的Java程序用于展示如何利用ZXing库读取图片形式存在的二维码: ```java import com.google.zxing.*; import com.google.zxing.common.HybridBinarizer; import java.io.File; import javax.imageio.ImageIO; public class QRCodeReader { public static void main(String[] args) throws Exception{ File file = new File("qrcode.png"); BufferedImage image; try { image = ImageIO.read(file); LuminanceSource source = new BufferedImageLuminanceSource(image); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Result result; MultiFormatReader reader = new MultiFormatReader(); result = reader.decode(bitmap); System.out.println("Content: " + result.getText()); } catch (Exception e){ System.err.println(e.getMessage()); } } } ``` 这段代码实现了从指定路径加载一张名为`qrcode.png`的图像文件作为输入数据源,接着创建了一个二值化位图对象供后续处理;之后实例化一个多格式阅读器(`MultiFormatReader`)来进行实际解码动作,最终输出所含有的文本信息给控制台打印出来[^1]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值