代码之家  ›  专栏  ›  技术社区  ›  Be Kind To New Users

ZXing无法读取此QRCode

  •  0
  • Be Kind To New Users  · 技术社区  · 6 年前

    我有以下代码:

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

    enter image description here

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

    ZXing中是否有允许扫描的设置?

    有没有简单的方法来擦洗这些小点?

    [编辑]我们正在使用中兴.Netv0.16.5从visualstudio中的Nuget安装。Nuget显示了可用的最新版本。

    0 回复  |  直到 6 年前
        1
  •  0
  •   Be Kind To New Users    6 年前

    以下是能够读取颜色、黑白和灰度的代码:

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