我想用這個庫https://zxingnet.codeplex.com/。 圖像在本網站https://zxing.org/w/decode.jspx上成功解碼,但未在我的代碼中。ZXing用於.NET的條碼掃描器
這裏有兩種方法我都試過:
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
。我在這裏做錯了什麼? 下面是示例圖像:
您是否嘗試使用簡單的條形碼圖像? –
你可以在Windows窗體中顯示圖像嗎?也許調整高度,寬度,左上方,只有條形碼傳遞給掃描儀。如有必要,旋轉圖像。 – jdweng
@VishnuPrasad,它在我無法檢測到的非常特定的情況下在這裏和那裏工作。大多數情況下,它只是不起作用,而每張圖片都在網站https://zxing.org/w/decode.jspx –