微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 用zxing读取图像中的QR码

我正在使用C#库来读取QRCodes.我发现的很多样本都基于旧版本的zxing,其中RGBluminanceSource构造函数仍然采用位图.在最新版本中,RGBluminanceSource只接受byte [].我试图将位图转换为byte [],但解码结果始终为null.

这是我用于转换的代码

private byte[] GetRGBValues(Bitmap bmp)
{
  // Lock the bitmap's bits. 
  System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0,bmp.Width,bmp.Height);
  System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect,System.Drawing.Imaging.ImageLockMode.ReadOnly,bmp.PixelFormat);

  // Get the address of the first line.
  IntPtr ptr = bmpData.Scan0;

  // Declare an array to hold the bytes of the bitmap.
  int bytes = bmpData.Stride * bmp.Height;
  byte[] rgbValues = new byte[bytes];
  // copy the RGB values into the array.
  System.Runtime.InteropServices.Marshal.copy(ptr,rgbValues,bytes);
  bmp.UnlockBits(bmpData);

  return rgbValues;
}

并用于解码:

Bitmap bitmap = Bitmap.FromFile(@"C:\QRimages.jpg") as Bitmap;
luminanceSource source = new RGBluminanceSource(GetRGBValues(bitmap),bitmap.Width,bitmap.Height);

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

不知何故,结果始终为null.

另外,我们的要求是我们必须使用相机拍摄的图像.我试过这个:

Bitmap bitmap = Bitmap.FromFile(@"C:\QRimages.jpg") as Bitmap;
BarcodeReader reader = new BarcodeReader { AutoRotate = true,TryHarder = true };
Result result = reader.Decode(bitmap);

它仅适用于我在线下载的QR图像,但是如果我打印出该图像并用手机拍照,那么尝试处理该图像,结果返回null.

任何建议,将不胜感激.

这是我正在使用的图像:

解决方法

尝试捕捉平行于表面的照片.它会解码.我试过了.

原文地址:https://www.jb51.cc/csharp/98798.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐