ZXing.Net 从 0.16.5 升级到 0.16.6 后读取条码的标准方式

问题描述

将 ZXing.Net nuget 包从 0.16.5 升级到 0.16.6 后,现有源显示错误

使用泛型类型“BarcodeReader”需要 1 个类型参数

public void ReadBarcode(System.Drawing.Bitmap readerBitmap,BarcodeFormat barcodeFormat)
{
    // here when instantiate the barcode reader object 
    // the error occurs with the new updated nuget package 0.16.6
    var barcodeReader = new ZXing.BarcodeReader();

    barcodeReader.Options.PossibleFormats = new List<BarcodeFormat>();
    barcodeReader.Options.PossibleFormats.Add(barcodeFormat);
    barcodeReader.AutoRotate = true;
    barcodeReader.Options.TryHarder = true;
    barcodeReader.Options.PureBarcode = false;

    ZXing.Result barcodeResult = null;

    try
    {
        barcodeResult = barcodeReader.Decode(readerBitmap);
    }
    catch (Exception ex)
    {
        Log.LogError($"Exception in barcode lib - {ex.Message}");
    }

我应该从 BarcodeReader 更改为 BarcodeReaderGenericIBarcodeReaderIBarcodeReaderGeneric 还是应该添加参数 createluminanceSource

我无法找到使用 0.16.6 的现有示例,甚至文档仍然显示无参数构造函数

BarcodeReader Class ZXing.Net Documentation

解决方法

如果您将 ZXing.Net 与 .Net Core / .Net Standard 一起使用,则必须使用不同的绑定包之一。它们包含不同图像库的特定 BarcodeReader 实现。 https://www.nuget.org/packages?q=zxing.bindings 这是设计使然,因为 .Net 核心在核心包中不包含位图实现。

,

如果您感到绝望并且无法正确引用从 BarcodeReader 继承的 BarcodeReader<Bitmap>,您可以直接调用 BarcodeReader<Bitmap> 的基本构造函数,该构造函数在您创建时调用一个新的BarcodeReader

var luminanceSource = (Func<Bitmap,LuminanceSource>) (bitmap => new BitmapLuminanceSource(bitmap));
var barcodeReader = new ZXing.BarcodeReader<Bitmap>(null,luminanceSource,null);



//...so on
barcodeReader.Options.PossibleFormats = new List<BarcodeFormat>();
barcodeReader.Options.PossibleFormats.Add(barcodeFormat);