0

I want to decode different codes in C# by using OpenCvSharp and ZXing.NET. The Image-Capture is done by an IDS-Camera (10 mp). My code looks like this:

using System;
using System.Drawing;

using OpenCvSharp;

using ZXing;
using ZXing.Datamatrix;
using ZXing.Multi;

using uEye;

class Program
{ 
    static void Main(string[] args)
    {
        {
            while (true)
            {
                // create new camera
                Camera cam = new Camera();  
                cam.Init(1);
                cam.Memory.Allocate();
                // acquire image
                cam.Acquisition.Capture(uEye.Defines.DeviceParameter.Wait);
                // convert to byte-array (to show in window) and to bitmap (to decode)
                cam.Memory.CopyToArray(1, out byte[] Imag);
                cam.Memory.CopyToBitmap(1, out Bitmap Image);
                // show image in window
                Mat mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(Image);
                Cv2.Resize(mat, mat, new OpenCvSharp.Size(960, 687), 0.25, 0.25);
                Cv2.ImShow("Code", mat);
                Cv2.ResizeWindow("Code", 960, 687);

                //BitMap --> LuminanceSource --> BinaryBitmap
                LuminanceSource bild = new RGBLuminanceSource(Imag, 3840, 2748);
                BinaryBitmap barcodeBitmap = new BinaryBitmap(new HybridBinarizer(bild));
                // implement new reader and decode
                var reader = new MultiFormatReader {};
                var result = reader.decode(barcodeBitmap);
                // write to console if decoded successfully, if not: write "failed"
                if (result != null){
                    Console.WriteLine(result.Text);
                }
                else{
                    Console.WriteLine("failed");
                }
                int c = Cv2.WaitKey(0);
                if (c != -1) { break; } 
            }
        }
    }
}

The image-acquisition works but it won't decode.

I hope somebody can help me.

Edit: Just if somebody is having the same Problem. I came back to ZXing and instead of OpenCV i used AForge.NET. First i load the image from the camera-memory and convert it into Grayscale. Then im using an thresholding-algorithm to get an 1bit-image. After this i am looking for quadrilaterales to get the code area and rotate the image so that the ZXing-reader can decode it. Actually its working fine for 1D- and datamatrix codes. If somebody needs the code, just ask me.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Ecklase
  • 1
  • 2
  • 1
    what exactly do you mean by "it won't decode" do you get an error message? – Tobias Theel Sep 01 '17 at 11:41
  • if it decode the datamatrix code in the image it should write the string to the console but (result != null) is always 'false' – Ecklase Sep 01 '17 at 11:52
  • There are two nuget packages available which are directly supporting OpenCVSharp 2.x and 3.x: https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCVSharp.V2/ https://www.nuget.org/packages/ZXing.Net.Bindings.OpenCV/ Perhaps they are a better option. They support the Mat class directly. You only have to instantiate the class ZXing.OpenCV.BarcodeReader and use the Decode method with mat. – Michael Sep 05 '17 at 19:27
  • One addition: What color format uses the output of the cam instance? Is it really 8bit grayscale? Because the class RGBLuminanceSource assumes it if no other value is given. Perhaps you should set the 4th argument of the constructor to BitmapFormat.RGB24 or something similar which better matchs with the content of Imag. – Michael Sep 05 '17 at 19:35
  • Thanks for your answer. I tried it with the 2nd nuget-package you posted but it still dont read anything.... :/ – Ecklase Sep 11 '17 at 10:45

0 Answers0