通过ThoughtWorks.QRCode和ZXing识别二维码

开发中用到两种识别二维码的方法,在这里一一列举一下,通过例子来详细解释一下。

ThoughtWorks.QRCode

在项目中首先要引入ThoughtWorks.QRCode.dll库文件。

using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using System.Drawing;

	public string CodeDecoder(string filePath)
	{
		if (!System.IO.File.Exists (filePath))//判断有没有需要读取的主文件夹,如果不存在,终止
			return null;
		
		 Bitmap bitMap = new Bitmap (Image.FromFile (filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象
		QRCodeDecoder decoder = new QRCodeDecoder ();//实例化QRCodeDecoder
		string decoderStr = decoder.decode (new  QRCodeBitmapImage (bitMap), System.Text.Encoding.UTF8);//通过.decoder方法把颜色信息转换成字符串信息
		return decoderStr;//返回字符串信息
	}


ZXing

项目中要引入ZXing.dll

using ZXing;
using System.IO;
using System.Drawing;

public string CodeDecoder(string filePath)
	{
		if (!System.IO.File.Exists (filePath))//同上
			return;

		Bitmap bitMap = new Bitmap (filePath);//同上
		MemoryStream ms = new MemoryStream ();//实例化内存流
		bitMap.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);//把位图信息保存到内存流里面
		byte[] bytes = ms.GetBuffer ();//把颜色信息转化为byte数据

		LuminanceSource source = new RGBLuminanceSource (bytes, bitMap.Width,bitMap.Height);//得到位图的像素数值内容
		BinaryBitmap bb = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source));//处理像素值内容信息

		MultiFormatReader mutireader = new ZXing.MultiFormatReader ();//实例化MultiFormatReader
		Result str = mutireader.decode(bb);//通过mutireader.decode()得到解析后的结果
		if(str == null)
			return null;
		return str.Text;//返回解析结果
		ms.Close ();//关闭内存流
	}

互相学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值