使用ZBar-Sharp(https://github.com/jonasfj/zbar-sharp),图像在Image类中使用系统库进行处理,代码如下
===============
public Image(System.Drawing.Image image) : this() {
Byte[] data = new byte[image.Width * image.Height * 3];
//Convert the image to RBG3
using(Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb)){
using(Graphics g = Graphics.FromImage(bitmap)){
g.PageUnit = GraphicsUnit.Pixel;
g.DrawImageUnscaled(image, 0, 0);
}
// Vertically flip image as we are about to store it as BMP on a memory stream below
// This way we don't need to worry about BMP being upside-down when copying to byte array
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
using(MemoryStream ms = new MemoryStream()){
bitmap.Save(ms, ImageFormat.Bmp);
ms.Seek(54, SeekOrigin.Begin);
ms.Read(data, 0, data.Length);
}
}
//Set the data
this.Data = data;
this.Width = (uint)image.Width;
this.Height = (uint)image.Height;
this.Format = FourCC('R', 'G', 'B', '3');
}
===============
后来从Zbar内转换为Y800。
然而,使用这种方法,当我将ZBar本机扫描与99.x%正确比较时,扫描精度受到影响,而.Net包装器只有80%。
由于ZBar本身使用图像魔术进行图像处理任务,因此建议使用imageMagic.Net处理图像并传递给Zbar DLL,而不是使用.Net库。
所以我认为ZBarSharp中的Image类应该用下面的重载来更新
=================
public Image(string fileName)
: this()
{
MagickImage image = new MagickImage(fileName, settings);
//Set the data
this.Data = image.ToByteArray();
this.Width = (uint)image.Width;
this.Height = (uint)image.Height;
this.Format = FourCC('R', 'G', 'B', '3');
}
=================
然而,我意识到,虽然原始.Net库创建的图像Byte []的长度约为5650614,偏移54但是在一个特定文件上得到5650560,但imageMagic读取相同的文件并生成长度为15194的Byte [],这显然更小,而且imageMagic.Net的Byte []总是得不到扫描结果。我已经为imageMagic尝试了许多不同的设置读取密度,格式等等,但是数组长度总是很短,我没有得到扫描结果。
只是想知道是否有人可以帮助我指出我错过了什么来使用imageMagic.Net获得.Net库生成的类似等价物但质量更好?
先谢谢。