简体   繁体   English

ZXing.Net的C#:解码QR码

[英]C# with ZXing.Net: Decoding the QR code

I'm new to C# and got problem with QR code decoding using ZXing.Net . 我是C#新手,使用ZXing.Net进行QR码解码时遇到问题。 The application launches with no errors, but I get nothing in the result string. 该应用程序启动时没有错误,但是结果字符串中没有任何信息。 I think the problem could be in RGBLuminanceSource() . 我认为问题可能出在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 ***
}

Just call function. 只需调用函数。 Also, replace ... with your handling 另外,将...替换为您的处理方式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM