您的位置:首页 > 其它

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

2017-01-03 10:14 399 查看
开发中用到两种识别二维码的方法,在这里一一列举一下,通过例子来详细解释一下。

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 ();//关闭内存流
}


互相学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: