加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:31:55 所属栏目:百科 来源:网络整理
导读:我正在使用C#库来读取QRCodes.我发现的很多样本都基于旧版本的zxing,其中RGBLuminanceSource构造函数仍然采用位图.在最新版本中,RGBLuminanceSource只接受byte [].我试图将位图转换为byte [],但解码结果始终为null. 这是我用于转换的代码: private byte[] G
我正在使用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.

任何建议,将不胜感激.

这是我正在使用的图像:

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读