带 ZXing 的 OpenCvSharp 用相机读取条码

OpenCvSharp with ZXing read barcodes with camera

我在将 Zxing 库与 openCvSharp 集成时遇到问题。我正在尝试使用相机实时读取条形码。

它在 Result result = barcodeReader.Decode(test); 行抛出异常;

System.InvalidOperationException: 'You have to declare a delegate which converts your byte array to a luminance source object.'

如果有人有任何解决方案、建议或想法,我将不胜感激! 完整代码是:

static void Main(string[] args)
        {
            
            VideoCapture capture = new VideoCapture(0);
            LuminanceSource source;

            using (Window window = new Window("Camera"))
                
            using(Mat image = new Mat())
            {

                while (true)
                {
                    capture.Read(image); // same as cvQueryFrame

                    BarcodeReader barcodeReader = new BarcodeReader();

                    var barcodeBitMap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

                    byte[] test;

                    using (var stream = new MemoryStream())
                    {
                        barcodeBitMap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                        test = stream.ToArray();
                    }

                    Result result = barcodeReader.Decode(test);

                    if (result != null)
                    {
                        Console.WriteLine("result: " + result.Text);
                    }

                    window.ShowImage(image);

                    Cv2.WaitKey(30);
                }
            }
        }

根据 .Net 目标平台,您可以通过解码方法直接使用位图。只有 .Net core/standard 版本不直接支持位图 类。

    static void Main(string[] args)
    {
        VideoCapture capture = new VideoCapture(0);

        BarcodeReader barcodeReader = new BarcodeReader();

        using (Window window = new Window("Camera"))
        using(Mat image = new Mat())
        {
            while (true)
            {
                capture.Read(image); // same as cvQueryFrame

                var barcodeBitMap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);

                Result result = barcodeReader.Decode(barcodeBitMap);

                if (result != null)
                {
                    Console.WriteLine("result: " + result.Text);
                }

                window.ShowImage(image);

                Cv2.WaitKey(30);
            }
        }
    }

或者您可以尝试以下绑定之一

https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCV/

https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCVSharp.V2/

使用它们,您可以实例化另一个直接支持 Mat 结构的 BarcodeReader 实现。

    static void Main(string[] args)
    {
        VideoCapture capture = new VideoCapture(0);

        BarcodeReader barcodeReader = new ZXing.OpenCV.BarcodeReader();

        using (Window window = new Window("Camera"))
        using(Mat image = new Mat())
        {
            while (true)
            {
                capture.Read(image); // same as cvQueryFrame

                Result result = barcodeReader.Decode(image);

                if (result != null)
                {
                    Console.WriteLine("result: " + result.Text);
                }

                window.ShowImage(image);

                Cv2.WaitKey(30);
            }
        }
    }