ZXing.Net cannot decode QR code captured by camera

Last information:

After reminding @Michael I to successfully capture and decode the QR code using

zxingnet-88246 \ trunk \ Clients \ WindowsFormsDemo

EmguCVDemo (successful decoding of a QR code in a camera frame today)

I will try to compare the difference between the demo code and my code. Thanks, Michael, so happy to successfully decode it.

p / s: these example codes work with net4.0 zxing.dll but not net4.5 zxing.dll


Old questions:

Using Zxing.Net, I can decode the original image of the QR code encoded by ZXing.Net.

But when I get the image from Emgu.CV capture , it cannot be decoded by ZXing.Net even I | try cropping, resizing and adding canvas size.

But the magic is the Android QR code scanner . Scan the QR code even directly from the camera. I tried to parse the Android source code, but I did not find anything special. I wonder if the Android version uses one autofocus function of the camera?

Below is my code:

DecoderResult decoderResult;
Bitmap bitmap = new Bitmap(@"C:\testfunny678.bmp");
LuminanceSource source = new BitmapLuminanceSource(bitmap);
//
BinaryBitmap binBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));

try
{
    //null Hashable Hints
    DetectorResult detectorResult = new Detector(binBitmap.BlackMatrix).detect(null);

    Result result = decoder.decode(binBitmap);

    //decoderResult = decoder.decode(detectorResult.Bits,null);
    //points = detectorResult.Points;
    //Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE);

    if (result.Text != null)
    {
        //QR_CODE
        //MessageBox.Show("format is: " + result.BarcodeFormat);
        MessageBox.Show("result is: " + result.Text);
    }
    else
    {
        MessageBox.Show("bitmap is null");
    }
}
//com.google.zxing.ReaderException: Exception of type 'com.google.zxing.ReaderException' was thrown.
//   at com.google.zxing.qrcode.detector.FinderPatternFinder.selectBestPatterns() in d:\Temp\zxing-2.1\csharp\qrcode\detector\FinderPatternFinder.cs:line 602
//   at com.google.zxing.qrcode.detector.FinderPatternFinder.find(Hashtable hints) in d:\Temp\zxing-2.1\csharp\qrcode\detector\FinderPatternFinder.cs:line 238
//   at com.google.zxing.qrcode.detector.Detector.detect(Hashtable hints) in d:\Temp\zxing-2.1\csharp\qrcode\detector\Detector.cs:line 90
//   at qrcode.Form1.Decode3() in c:\Users\User\Documents\Visual Studio 2013\Projects\qrcode\qrcode\Form1.cs:line 139
catch (Exception e)
{
    //MessageBox.Show(e.ToString());
}

My code can decode this

My code can decode this

Camera capture like this

Camera capture like this

After cropping, resizing and adding the canvas, it will look like this (testfunny678.bmp)

After crop, resize and add canvas

canvas QR-, QR- , , Android- QR-.

HybridBinarizer QR-.

        LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
        BinaryBitmap binBitmap = new BinaryBitmap(new HybridBinarizer(source));

- QR- ( QR-), QR-, . sharpen , ZXing QR-.

, QR- ( (23.5, 76.5), (23.5, 23.5), (75, 24.5))   THIRD QR

    public string Detect(Bitmap bitmap)
    {
        try
        {
            ZXing.LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            BitMatrix bm = binBitmap.BlackMatrix;
            Detector detector = new Detector(bm);
            DetectorResult result = detector.detect();

            string retStr = "Found at points ";
            foreach (ResultPoint point in result.Points)
            {
                retStr += point.ToString() + ", ";
            }

            return retStr;
        }
        catch
        {
            return "Failed to detect QR code.";
        }
    }

    private byte[] GetRGBValues(Bitmap bmp)
    {
        // Lock the bitmap bits. 
        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
        System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);

        // Get the address of the first line.
        IntPtr ptr = bmpData.Scan0;

        // Declare an array to hold the bytes of the bitmap.
        int bytes = bmpData.Stride * bmp.Height;
        byte[] rgbValues = new byte[bytes];
        // Copy the RGB values into the array.
        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
        bmp.UnlockBits(bmpData);

        return rgbValues;
    }

DetectorResult.Points Detect() Detect() DetectorResult , fail , Result.

    public DetectorResult Detect(Bitmap bitmap)
    //public string Detect(Bitmap bitmap)
    {
        try
        {
            ZXing.LuminanceSource source = new RGBLuminanceSource(GetRGBValues(bitmap), bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            BitMatrix bm = binBitmap.BlackMatrix;
            Detector detector = new Detector(bm);
            DetectorResult result = detector.detect();
            return result;
            //string retStr = "Found at points ";
            //foreach (ResultPoint point in result.Points)
            //{
            //    retStr += point.ToString() + ", ";
            //}

            //return retStr;
        }
        catch
        {
            //return "Failed to detect QR code.";
            return null;
        }
    }

    void Decode3()
    {
        //System.Collections.Hashtable hints = null;
        DecoderResult decoderResult;
        Bitmap bitmap = new Bitmap(@"C:\testfunny678.bmp");
        LuminanceSource source = new BitmapLuminanceSource(bitmap);
        BinaryBitmap binBitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
        ResultPoint[] points;

        ZXing.QrCode.Internal.Decoder decoder = new ZXing.QrCode.Internal.Decoder();
        //ZXing.MultiFormatReader decoder = new ZXing.MultiFormatReader();

        try
        {
            DetectorResult detectorResult = new Detector(binBitmap.BlackMatrix).detect(null);
            //DetectorResult detectorResult = Detect(bitmap);
            //Result result = decoder.decode(binBitmap);
            //null decoderResult here
            decoderResult = decoder.decode(detectorResult.Bits,null);
            points = detectorResult.Points;
            Result result = new Result(decoderResult.Text, decoderResult.RawBytes, points, BarcodeFormat.QR_CODE);

            if (result.Text != null)
            {
                MessageBox.Show("result is: " + result.Text);
            }
            else
            {
                MessageBox.Show("bitmap is null");
            }
        }
        catch (Exception e)
        {
            //MessageBox.Show(e.ToString());
        }

    }

2- QR QR ZXing.NET QR- , .

+4
1

-, QR- ZXing.Net WinFormsDemo. : ​​ ?

- EmguCV. EmguCV ZXing.Net, . , EmguCV ZXing.Net.

+2

Source: https://habr.com/ru/post/1529362/


All Articles