C#使用zxing,zbar,thoughtworkQRcode解析二维码,附源代码

时间:2022-06-28 05:46:47

做了zxing,zbar和thoughtworkqrcode解析二维码案例,希望大家有帮助。

zxing是谷歌开源的二维码库,,zbar,thoughtworkQRcode也是开源的,三者之间比较各有优劣

下面通过一个案例demo源码,来认识学习下这三者的实际解码效果,

第一次上传demo源码,献丑了

zbar解析关键代码:

Image primaryImage = Image.FromFile(fileName);
Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
{
List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Start();
symbols = scanner.Scan((Image)pImg);
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
MessageBox.Show("扫描执行时间:" + timeSpan.TotalMilliseconds.ToString());

if (symbols != null && symbols.Count > 0)
{
string result = string.Empty;
symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Type + Environment.NewLine);
MessageBox.Show(result);
}
}

zxing解析关键代码:

Image primaryImage = Image.FromFile(fileName);
Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
BarcodeReader reader = new BarcodeReader();
System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Start();
Result result = reader.Decode((Bitmap)pImg); //通过reader解码
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
MessageBox.Show("二维码值:"+result+"扫描执行时间:" + timeSpan.TotalMilliseconds.ToString());

thoughtworkqrcode解析关键代码:

Image primaryImage = Image.FromFile(fileName);

Bitmap pImg = (Bitmap)primaryImage;// MakeGrayscale3((Bitmap)primaryImage);
try
{
System.Diagnostics.Stopwatch watch = new Stopwatch();
watch.Start();
string decodedString = new QRCodeDecoder().decode(new QRCodeBitmapImage(pImg), Encoding.UTF8);
watch.Stop();
TimeSpan timeSpan = watch.Elapsed;
MessageBox.Show("二维码值:" + decodedString + "扫描执行时间:" + timeSpan.TotalMilliseconds.ToString());
}
catch (Exception ex)
{
MessageBox.Show("好难过");
}

下面来测试下三者之间的给力程度吧,在草料二维码网页上在线生成一个二维码

为了测试给力程度,我们截出如下图片,三者对比解码:

下面来看下效果吧:

zbar效果

zxing效果

thoughtworqrcode效果

从解码的效率上来看zbar》zxing》thoughtworkqrcode

从稳定性来看zbar是最差的,zbar这个开源项目已经很久没再维护更新了,所以这也是跟不上时代的一个原因,但是效率确实比较高。

最后附上源码:源码