简体   繁体   中英

OpenCvSharp with ZXing read barcodes with camera

I'm having a problem with integrating Zxing libary with openCvSharp. I'm trying to read barcodes live with camera.

It throws an exception at the line Result result = barcodeReader.Decode(test); that gives;

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

I would appreciate If someone have any solution, suggestion or idea! Full code is:

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

Depending on the .Net target platform you can use the bitmap directly with the method Decode. Only the .Net core/standard versions don't support the Bitmap classes directly.

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

Alternatively you can try out one of the following bindings

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

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

With them you can instantiate another BarcodeReader implementation which supports the Mat struct directly.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM