【问题标题】:OpenCvSharp with ZXing read barcodes with cameraOpenCvSharp with ZXing 用摄像头读取条码
【发布时间】:2020-09-02 13:55:56
【问题描述】:

我在将 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);
                }
            }
        }

【问题讨论】:

    标签: opencv zxing opencvsharp zxing.net


    【解决方案1】:

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

        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);
                }
            }
        }
    

    【讨论】:

    • 我正在使用 .NET Core 3.1 和 OpenCvSharp4。使用绑定让我更清楚,但现在我在尝试调试代码时面临另一个异常。 System.MissingMethodException HResult=0x80131513 Message=Method not found: 'Void OpenCvSharp.VideoCapture..ctor(Int32, OpenCvSharp.VideoCaptureAPIs)'. Source=<Cannot evaluate the exception source> StackTrace: <Cannot evaluate the exception stack trace>
    • 删除所有ZXing的东西,只尝试捕获图像并显示它。异常是否仍然发生?
    • 是的,删除所有ZXing的东西后仍然会出现异常。
    • 您使用的是最新版本的 OpenCvSharp v4 吗?也许您引用不同的版本? Zxing 绑定目前仅支持 v3。也许您的项目中有两个不同的程序集引用? github.com/shimat/opencvsharp/issues/868
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多