2016-02-02 65 views
2

我想用這個庫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。我在這裏做錯了什麼? 下面是示例圖像:

enter image description here

+0

您是否嘗試使用簡單的條形碼圖像? –

+0

你可以在Windows窗體中顯示圖像嗎?也許調整高度,寬度,左上方,只有條形碼傳遞給掃描儀。如有必要,旋轉圖像。 – jdweng

+0

@VishnuPrasad,它在我無法檢測到的非常特定的情況下在這裏和那裏工作。大多數情況下,它只是不起作用,而每張圖片都在網站https://zxing.org/w/decode.jspx –

回答

1

我終於做了一個總的重新開始時爲預期不工作。

我實現了以下algorythm:如果解碼器不讀取條碼,請將圖像拆分爲4並重新啓動。

它工作得很好後,我認爲這是你提到的網站是如何工作的。太糟糕了,它不會從頭開始使用這種方法。

注:此代碼是遠遠不夠完善,使得很多的假設,如果你將它複製並使用它,它可能如果你的圖片是不是在一個由提供相同的格式崩潰OP

internal class Program 
{ 
    private static readonly List<BarcodeFormat> Fmts = new List<BarcodeFormat> { BarcodeFormat.All_1D }; 

    static void Main(string[] args) 
    { 
     Bitmap originalBitmap = new Bitmap(@"C:\Users\me\Desktop\6.jpg"); 
     Bitmap img = CropImage(originalBitmap, new Rectangle(0 , 0, originalBitmap.Width, originalBitmap.Height)); 
     int width = img.Width; 
     int heigth = img.Height; 
     int nbOfFrames = 1; 
     bool found = false; 
     while (!found && width > 10 && heigth > 10) 
     { 
      if (DecodeImg(img)) 
      { 
       break; 
      } 
      nbOfFrames *= 4; 
      width /= 2; 
      heigth /= 2; 
      var x = 0; 
      var y = 0; 

      for (int i = 0; i < nbOfFrames; i++) 
      { 
       img.Dispose(); 
       img = new Bitmap(CropImage(originalBitmap, new Rectangle(x, y, width, heigth))); 

       if (DecodeImg(img)) 
       { 
        found = true; 
       } 
       x += width; 
       if (x < originalBitmap.Width) 
       { 
        continue; 
       } 
       x = 0; 
       y += heigth; 
       if (y >= originalBitmap.Height) 
       { 
        y = 0; 
       } 
      } 
     } 


    } 

    public static Bitmap CropImage(Image img, Rectangle cropArea) 
    { 
     Bitmap bmpImage = new Bitmap(img); 
     return bmpImage.Clone(cropArea, PixelFormat.Format24bppRgb); 
    } 

    public static bool DecodeImg(Bitmap img) 
    { 
     BarcodeReader reader = new BarcodeReader 
     { 
      AutoRotate = true, 
      TryInverted = true, 
      Options = 
      { 
       PossibleFormats = Fmts, 
       TryHarder = true, 
       ReturnCodabarStartEnd = true, 
       PureBarcode = false 
      } 
     }; 
     Result result = reader.Decode(img); 

     if (result != null) 
     { 
      Console.WriteLine(result.BarcodeFormat); 
      Console.WriteLine(result.Text); 
      return true; 
     } 

     Console.Out.WriteLine("Raté"); 
     return false; 
    } 
} 
+0

讓我試試這個... –

+0

嗯,它在我的機器上會導致null。你能否將解決方案上傳到某個地方? –

+0

@GiorgiNakeuri我會,只要給我一個小時 –