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.

任何建议,将不胜感激.

这是我正在使用的图像:

解决方法

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

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...