c# - ZXing.Net无法解码Camera捕获的二维码

标签 c# winforms camera zxing emgucv

最新信息:

@Michael的提醒下, 我使用

成功捕获解码二维码

zxingnet-88246\trunk\Clients\WindowsFormsDemo

EmguCVDemo (successful decode QR code in camera frame today)

我将尝试比较演示代码和我的代码之间的差异。谢谢迈克尔,很高兴成功解码它。

p/s:这些示例代码适用于 net4.0 zxing.dll 但 net4.5 zxing.dll


老问题:

通过使用Zxing.Net,我可以解码ZXing.Net编码的QR码的原始图像。

但是当我从Emgu.CV capture获取图像时,即使我也无法通过ZXing.Net解码
尝试裁剪调整 和添加 Canvas 大小。

但是,神奇的是 Android 二维码扫描仪 甚至可以直接从相机捕获的图像中扫描这些二维码。我试图分析 Android 源代码,但没有发现任何特别之处。 不知安卓一版是否使用相机自动对焦功能?

下面是我的代码:

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

相机捕捉像这样

Camera capture like this

经过crop,resize and add canvas后,变成这样(testfunny678.bmp)

After crop,resize and add canvas

我在第一张二维码图片处添加了 Canvas ,因为我发现二维码被黑色包围,即使是Android二维码解码器也无法解码

旧版本 我使用 HybridBinarizer 的代码无法解码第一个 QR 码。

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

我的最终目标是直接从相机解码二维码(第二个二维码),但如果我能解码第三个二维码,我也很高兴。我的 friend 告诉我锐化第三张图片,以便ZXing解码并解码第三个QR码。

顺便说一句,我可以检测 QR 码的存在(在点 (23.5, 76.5), (23.5, 23.5), (75, 24.5) 找到) 通过这两个函数生成第三个二维码

    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's 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;
    }

但即使我尝试通过更改 Detect() 函数返回DetectorResult 来使用Detect() 函数中的DetectorResult.Points解码器,由于具有null decoderResult,它在解码器部分仍然失败

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

    }

关于使用 ZXing.NET 成功解码 2nd QR code 和3rd QR code 的任何建议或更正欢迎使用二维码解码器,谢谢。

最佳答案

首先,我可以使用当前版本的 ZXing.Net 和 WinFormsDemo 成功解码第一版和第三版二维码。 我的主要问题是,为什么你得到这样一个扭曲的第二个版本?

您应该尝试正确初始化网络摄像头和 EmguCV。我在 ZXing.Net 的演示中使用了 EmguCV,但从未得到如此奇怪的结果。请查看 ZXing.Net 存储库中 EmguCV 演示的源代码。

关于c# - ZXing.Net无法解码Camera捕获的二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088147/

相关文章:

c# - Visual Studio - 无法删除项目配置

c# - 是否可以在派生类中添加 XmlIgnore 属性?

c# - 判断串口是普通COM还是SPP

c# - C#什么首先执行的构造函数或onLoad?

c# - 如何转到最后单击的数据 GridView 中的行

c# - 使用 EP Plus 在工作表上允许前导零

c# - 在 webbrowser 上获取函数 javascript 的返回值

Android 相机 RTSP/RTP 流?

android - 从相机预览中获取触摸像素的颜色

javascript - Ionic 2相机错误