【问题标题】:ZXing can not read this QRCodeZXing 无法读取此二维码
【发布时间】:2019-10-16 03:17:47
【问题描述】:

我有以下代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;

namespace SeparatorPageSplit.framework
{
    class QRCodeScanner
    {
        BarcodeReader reader;
        public QRCodeScanner()
        {
            try
            {
                this.reader = new BarcodeReader { AutoRotate = true, TryInverted = true };
                this.reader = new BarcodeReader();
                this.reader.Options.PossibleFormats = new List<BarcodeFormat>();
                this.reader.Options.PossibleFormats.Add(BarcodeFormat.QR_CODE);
                // this.reader.Options.TryHarder = true;
            }
            catch (Exception ex)
            {
                Program.WriteToLogFile(ex.ToString());
            }
        }
        public Boolean IsQRCodeFound(string ImagePath)
        {
            string decoded = "";
            Bitmap bitmap = new Bitmap(ImagePath);
            try
            {
                Result result = this.reader.Decode(bitmap);
                if (result != null)
                {
                    decoded = result.ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                Program.WriteToLogFile(ex.ToString());
            }
            finally
            {
                bitmap.Dispose();
            }

            if (decoded == "CCA001")
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

我尝试阅读以下图片:

如果我以彩色或灰度扫描,此代码可以正常工作。当我以黑白扫描时,它不起作用。

ZXing 中是否有一些设置允许它进行扫描?

有没有一种简单的方法可以擦掉小点?

[编辑] 我们正在使用从 Visual Studio 中的 Nuget 安装的 ZXing.Net v0.16.5。 Nuget 在可用的最新版本中显示。

【问题讨论】:

  • 确保您使用的是最新版本:github.com/micjahn/ZXing.Net/issues/207
  • zxing 不适用于黑白图像。大多数时候,扫描仪会在代码边缘添加一些奇怪的黑色像素。内部启发式对他们有问题。如果可能,请使用灰度图像。如果无法做到这一点,请将图像从黑白转换为灰度,并在解码前添加高斯模糊滤镜。
  • @Michael 是否有可以处理黑白图像的替代二维码阅读器?
  • 还有许多其他条码扫描库。不确定是否可以处理它,但我认为可以。而且大部分都不是免费的。

标签: c# zxing zxing.net


【解决方案1】:

这是能够读取彩色、黑白和灰度的代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZXing;

namespace SeparatorPageSplit.framework
{
    class QRCodeScanner
    {
        //BarcodeReader reader;
        QRCodeDecoderLibrary.QRDecoder QRCodeDecoder;

        public QRCodeScanner()
        {
            try
            {
                QRCodeDecoder = new QRCodeDecoderLibrary.QRDecoder();
            }
            catch (Exception ex)
            {
                Program.WriteToLogFile(ex.ToString());
            }
        }
        public Boolean IsQRCodeFound(string ImagePath)
        {
            string decoded = "";
            Bitmap bitmap = new Bitmap(ImagePath);
            try
            {
                byte[][] DataByteArray = QRCodeDecoder.ImageDecoder(bitmap);
                decoded = QRCodeResult(DataByteArray);
            }
            catch (Exception ex)
            {
                Program.WriteToLogFile(ex.ToString());
            }
            finally
            {
                bitmap.Dispose();
            }

            if (decoded == "CCA001")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Format result for display
        /// </summary>
        /// <param name="DataByteArray"></param>
        /// <returns></returns>
        private static string QRCodeResult(byte[][] DataByteArray)
        {
            // no QR code
            if (DataByteArray == null) return string.Empty;

            // image has one QR code
            if (DataByteArray.Length == 1) return ForDisplay(QRCodeDecoderLibrary.QRDecoder.ByteArrayToStr(DataByteArray[0]));

            // image has more than one QR code
            StringBuilder Str = new StringBuilder();
            for (int Index = 0; Index < DataByteArray.Length; Index++)
            {
                if (Index != 0) Str.Append("\r\n");
                Str.AppendFormat("QR Code {0}\r\n", Index + 1);
                Str.Append(ForDisplay(QRCodeDecoderLibrary.QRDecoder.ByteArrayToStr(DataByteArray[Index])));
            }
            return Str.ToString();
        }

        private static string ForDisplay(string Result)
        {
            int Index;
            for (Index = 0; Index < Result.Length && (Result[Index] >= ' ' && Result[Index] <= '~' || Result[Index] >= 160); Index++) ;
            if (Index == Result.Length) return Result;

            StringBuilder Display = new StringBuilder(Result.Substring(0, Index));
            for (; Index < Result.Length; Index++)
            {
                char OneChar = Result[Index];
                if (OneChar >= ' ' && OneChar <= '~' || OneChar >= 160)
                {
                    Display.Append(OneChar);
                    continue;
                }

                if (OneChar == '\r')
                {
                    Display.Append("\r\n");
                    if (Index + 1 < Result.Length && Result[Index + 1] == '\n') Index++;
                    continue;
                }

                if (OneChar == '\n')
                {
                    Display.Append("\r\n");
                    continue;
                }

                Display.Append('¿');
            }
            return Display.ToString();
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    相关资源
    最近更新 更多