我正在尝试读取 USB 相机获取的图像中的二维码。 在其他帖子中,我读到最好的开源库是 ZXing。
如果二维码来自数字生成的图像,则库工作正常,但如果二维码来自图像由相机获取的真实情况,则解码库会遇到一些困难。
采集的图像受到一些眩光、代码变形或对比度缓慢的干扰。
您知道一些参数可以更好地设置阅读器吗? 或者在阐述之前添加一些滤镜到图像中?
例如:
BarcodeReader reader = new BarcodeReader();
reader.AutoRotate = true;
reader.Options.TryHarder = true;
reader.Options.PureBarcode = false;
reader.Options.PossibleFormats = new List<BarcodeFormat>();
reader.Options.PossibleFormats.Add(BarcodeFormat.QR_CODE);
var result = reader.Decode(image);
谢谢你
经过多次测试,300dpi扫描图像的最佳结果是:
//use gaussian filter to remove noise
var gFilter = new GaussianBlur(2);
image = gFilter.ProcessImage(image);
var options = new DecodingOptions { PossibleFormats = new List<BarcodeFormat> { BarcodeFormat.QR_CODE }, TryHarder = true };
using (image)
{
//use GlobalHistogramBinarizer for best result
var reader = new BarcodeReader(null, null, ls => new GlobalHistogramBinarizer(ls)) { AutoRotate = false, TryInverted = false, Options = options };
var result = reader.Decode(image);
reader = null;
return result;
}
对于高斯滤波器,我使用来自 http://www.cnblogs.com/Dah/archive/2007/03/30/694527.html
的代码希望这对某人有帮助。
This is how I solve it
First you need to install one of the packages from (ZXing.Net.Bindings.*)
First example using
> ZXing.Net.Bindings.Windows.Compatibility
Please note this package only works on Windows.
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.Windows.Compatibility;
public static string ReadQRCode(byte[] byteArray)
{
Bitmap target;
target = ByteToBitmap(byteArray);
var source = new BitmapLuminanceSource(target);
var bitmap = new BinaryBitmap(new HybridBinarizer(source));
var reader = new QRCodeReader();
var result = reader.decode(bitmap);
return result.Text;
}
public static Bitmap ByteToBitmap(byte[] byteArray)
{
Bitmap target;
using (var stream = new MemoryStream(byteArray)) {
target = new Bitmap(stream);
}
return target;
}
Second example using
> ZXing.Net.Bindings.ImageSharp.V2
using ZXing;
using ZXing.Common;
using ZXing.ImageSharp;
using ZXing.QrCode;
public static string ReadQRCode(byte[] byteArray)
{
// Load image from byte array
Image<Rgba32> image = SixLabors.ImageSharp.Image.Load<Rgba32>(byteArray);
// Create an instance of ImageSharpLuminanceSource with Rgba32 pixel format
var luminanceSource = new ImageSharpLuminanceSource<Rgba32>(image);
var bitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
var reader = new QRCodeReader();
var result = reader.decode(bitmap);
return result. Text;
}