【问题标题】:C# with ZXing.Net: Decoding the QR codeC# 与 ZXing.Net:解码 QR 码
【发布时间】:2015-02-17 12:42:05
【问题描述】:

我是 C# 新手,使用 ZXing.Net 解码 QR 码 时遇到问题。应用程序启动时没有错误,但我在结果字符串中什么也没有。我认为问题可能出在 RGBLuminanceSource()

private static byte[] ToByteArray(Image img)
{
    byte[] byteArray = new byte[0];
    using (MemoryStream stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
        stream.Close();

        byteArray = stream.ToArray();
    }
    return byteArray;
}
private void button1_Click(object sender, EventArgs e)
{
    *** SOME OTHER CODE HERE ***

    Bitmap BitmapImage = new Bitmap(@"D:\1.png"); 

    QRCodeReader reader = new QRCodeReader();
    LuminanceSource source = new RGBLuminanceSource(ToByteArray(BitmapImage), BitmapImage.Width, BitmapImage.Height);

    var binarizer = new HybridBinarizer(source);
    var binBitmap = new BinaryBitmap(binarizer);
    string result = reader.decode(binBitmap).Text;

    *** SOME OTHER CODE HERE ***
}

【问题讨论】:

    标签: c# decode qr-code zxing


    【解决方案1】:

    只需调用函数。另外,将 ... 替换为您的处理方式

    public Result decode(Uri uri)
    {
             Bitmap image;
             try
             {
                image = (Bitmap) Bitmap.FromFile(uri.LocalPath);
             }
             catch (Exception)
             {
                throw new FileNotFoundException("Resource not found: " + uri);
             }
    
             using (image)
             {
                LuminanceSource source;
                source = new BitmapLuminanceSource(image);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                Result result = new MultiFormatReader().decode(bitmap);
                if (result != null)
                {
                    ... code found
                }
                else
                {
                    ... no code found
                }
                return result;
             }
    }
    

    【讨论】:

    • 谢谢! BitmapLuminanceSource 帮助了我!
    • 这对我有帮助。非常感谢 :)
    猜你喜欢
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多