2009-10-28 30 views
8

我正在尋找一個很好的開源庫,可以從圖像中找到並讀取條形碼(與使用條形碼掃描器相比)。從堆棧溢出的其他問題,我發現ZXing(「斑馬線」)是相當不錯的。儘管它是爲Java編寫的,但有一個C#端口 - 但是,我認爲它可能不完整。你認爲這是可靠的足以從這樣的情況解析條碼,或者是其他一些圖書館更好?C中的ZXing(「Zebra Crossing」)#

編輯:正如埃德在評論中指出的,我應該先嚐試一下。哇,我沒有想到這一點。 :)但我想我的問題是部分端口是否足夠可靠 - 如果您之前使用過它,它可以掃描熟練嗎?

+3

爲什麼你不只是,你知道...嘗試一下嗎? – 2009-10-28 23:39:19

+0

@Ed哦,恩,呃!哈哈,你知道,我想我需要結束我的問題。 Oopsey。 – 2009-10-28 23:40:48

+0

嗯,這個問題本身對將來的人會有用,所以我認爲這是不對的。另外,有些人可能會有需要花時間學習的見解,乍一看你可能會錯過。我只是在暗示,同時,嘗試這將是我的第一個目標。 – 2009-10-28 23:51:58

回答

2

當然,這取決於您使用的是什麼。即使Java版本的zxing也有一些重要的限制和性能問題。例如,它只能在頁面上找到一個條形碼。此外,它用於在頁面上定位1-D條形碼的算法效率不高(不瞭解二維條形碼的算法 - 這不屬於我正在開發的項目的要求)。這就是所有可以解決的問題 - 幾個月前我開始進行增強,並且能夠顯着提高一維位置性能和可靠性,但是我們的開發優先級已經發生變化,因此我從那時起就沒有開發過它。至於是否C#的部分端口是好的,如果你想回傳差異是什麼,我很樂意發表評論。

編輯 - 這裏是一些我做的重構:

首先,分解出RowNumberStrategy如下:

public interface RowNumberStrategy { 
public int getNextRowNumber(); 

public class OriginalRowStrategy implements RowNumberStrategy{ 
    int middle; 
    boolean tryHarder = false; 
    int rowStep; 
    int maxLines; 
    int maxRows; 

    int x; 

    public OriginalRowStrategy(int maxRows, boolean tryHarder) { 
     this.x = 0; 
     this.maxRows = maxRows; 
     this.middle = maxRows >> 1; // divide by 2 
     this.tryHarder = tryHarder; 
     rowStep = Math.max(1, maxRows >> (tryHarder ? 7 : 4)); 
     if (tryHarder) { 
      maxLines = maxRows; // Look at the whole image, not just the center 
     } else { 
      maxLines = 9; // Nine rows spaced 1/16 apart is roughly the middle half of the image 
     } 
    } 

    public int getNextRowNumber() { 
     if (x > maxLines) 
      return -1; 

     int rowStepsAboveOrBelow = (x + 1) >> 1; 
     boolean isAbove = (x & 0x01) == 0; // i.e. is x even? 
     int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow); 
     if (rowNumber < 0 || rowNumber >= maxRows) { 
      // Oops, if we run off the top or bottom, stop 
      return -1; 
     } 

     x = x + 1; 

     return rowNumber; 
    } 

} 

public class LinearScanRowStrategy implements RowNumberStrategy{ 
    private final int maxRows; 
    private int currentRow; 
    public LinearScanRowStrategy(int totalRows) { 
     maxRows = totalRows; 
     currentRow = 0; 
    } 

    public int getNextRowNumber() { 
     if (currentRow > maxRows) 
      return -1; 

     return maxRows - 1 - currentRow++; 
    } 

} 

public class ProgressiveScanRowStrategy implements RowNumberStrategy{ 
    private final int maxRows; 
    private int currentStepSize; 
    private int currentStep; 

    public ProgressiveScanRowStrategy(int totalRows) { 
     maxRows = totalRows; 
     currentStep = 0; 
     currentStepSize = maxRows; 
    } 

    public int getNextRowNumber() { 
     int nextRow = (currentStep++) * currentStepSize; 
     if (nextRow < maxRows) 
      return nextRow; 

     currentStepSize = currentStepSize >> 1; 
     if (currentStepSize <= 0) 
      return -1; 
     currentStep = 1; 

     nextRow = currentStep * currentStepSize; 

     return nextRow; 
    } 

} 



} 

然後doDecode的頂部變爲如下:

private Result doDecode(MonochromeBitmapSource image, Hashtable hints) throws ReaderException { 


int width = image.getWidth(); 
int height = image.getHeight(); 
BitArray row = new BitArray(width); 
boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); 
RowNumberStrategy rowProvider = new RowNumberStrategy.ProgressiveScanRowStrategy(height); 

int rowNumber; 
while ((rowNumber = rowProvider.getNextRowNumber()) != -1){ 
... 
} 

最終,這應該是可以通過DecodeHintType設置的東西,但我們發現在任何情況下,漸進式策略都比舊式策略要快聳聳肩(而不僅僅是更快一點 - 更快)。

+0

嘗試使用MultipleBarcodeReader在圖像中查找多個條形碼。不知道你發現一維檢測效率低下 - 從中​​心向外掃描幾條線。如果有的話,默認模式是快速但粗略的。 – 2009-11-01 01:39:59

+0

我肯定會檢查MBR - 感謝您的提示。在算法上,掃描策略對於大圖像(比如說3300行)無效,因爲它使用了固定步進方法。我會在全文中回覆我正在談論的內容,以便我可以展示代碼。 – 2009-11-05 05:30:35

+0

不,掃描步驟是高度的函數 - 默認情況下,它跳過每一步中圖像的高度/ 16行。 – 2009-11-17 11:49:39

-1

嘗試編譯與ikvmc的Java版本,而不是從您的C#代碼訪問它。

3

我一直在使用Java版本超過一年,每天掃描大約100次,而且效果很好。我看不出有任何理由的C#版本會更糟糕。