2

使用下面的代码时我得到空值。我正在使用从 NuGet 下载的 ZXing dll

    using ZXing.Common;
    using ZXing.QrCode;
    using ZXing.QrCode.Internal;

    private void Decode()
    {
        Bitmap bitmap = new Bitmap(@"D:\Project\QRCodes\myqrcode.png");
        try
        {
            MemoryStream memoryStream = new MemoryStream();
            bitmap.Save(memoryStream, ImageFormat.Bmp);

            byte[] byteArray = memoryStream.GetBuffer();

            ZXing.LuminanceSource source = new RGBLuminanceSource(byteArray, bitmap.Width, bitmap.Height);
            var binarizer = new HybridBinarizer(source);
            var binBitmap = new BinaryBitmap(binarizer);
            QRCodeReader qrCodeReader = new QRCodeReader();

            Result str = qrCodeReader.decode(binBitmap);

        }
        catch{ }

    }

请给我一个解决方案提前谢谢

4

1 回答 1

5

问题解决了我通过使用下面的代码让它工作了

         Bitmap bitmap = new Bitmap(@"D:\Project\QRCodes\myqrcode.png");
         try
        {               
            BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true };
            Result result = reader.Decode(bitmap);
            string decodedData = result.Text;                
        }
        catch
        {
            throw new Exception("Cannot decode the QR code");
        }
于 2013-09-11T08:19:48.887 回答