新浪博客

C#,使用ZXing 读取Data Matrix

2017-11-09 17:23阅读:
直接使用BarcodeReader 并不能读取,通过不断的实验,采用把位图转换为byte[]后用DatamatrixReader 进行解码
如下:
//把位图转为byte
var bmpData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.ReadWrite, img.PixelFormat);
int count = bmpData.Stride * bmpData.Height;
byte[] pic = new byte[count];
Marshal.Copy(bmpData.Scan0, pic, 0, count);
img.UnlockBits(bmpData);
LuminanceSource source = new RGBLuminanceSource(pic, img.Width, img.Height);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
DataMatrixReader reader = new DataMatrixReader();
Result result = reader.decode(bitmap);
//Result result = reader.decode(bitmap);
//Result result = reader.Decode((Bitmap)pic_model.Image);
MessageBox.Show(result.Text);
要注意的是不能用流来转字节,具体原因研究下位图成像原理
或者使用zxing.BitLuminaceSource也可以不用转为byte[]
但是要注意的是zxing只识别24位的jpg,对于32位的jpg不支持,理论上可以通过对byte[]的删减转为24位,但是失败了。

我的更多文章

下载客户端阅读体验更佳

APP专享