我正在尝试使用这个库https://zxingnet.codeplex.com/。 在这个网站https://zxing.org/w/decode.jspx上成功解码了图片,但是在我的代码中却没有成功。
我尝试了两种方法:
BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true, TryInverted = true, PossibleFormats = fmts };
Result result = reader.Decode(new Bitmap(@"D:\\6.jpg"));
并且:
public static byte[] ImageToByte(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
MultiFormatReader m_BarcodeReader = new MultiFormatReader();
var hints = new Dictionary<DecodeHintType, object>();
var fmts = new List<BarcodeFormat>();
fmts.Add(BarcodeFormat.EAN_13);
hints.Add(DecodeHintType.TRY_HARDER_WITHOUT_ROTATION, false);
hints.Add(DecodeHintType.POSSIBLE_FORMATS, fmts);
Result rawResult;
Bitmap image = new Bitmap(@"D:\\6.jpg");
RGBLuminanceSource r = new RGBLuminanceSource(ImageToByte(image), image.Width, image.Height);
GlobalHistogramBinarizer x = new
HybridBinarizer(r);
BinaryBitmap bitmap = new BinaryBitmap(x);
try
{
rawResult = m_BarcodeReader.decode(bitmap, hints);
if (rawResult != null)
{
return rawResult.Text;
}
}
catch (ReaderException e)
{
}
在这两种情况下,解码的结果都是
null
。我做错了什么?
这是示例图片: