我正在寻找一个良好的开源库,可以从图像中查找和读取条形码(而不是使用条形码扫描器)。从 Stack Overflow 上的其他问题中,我发现 ZXing(“斑马线”)非常好。虽然它是为 Java 设计的,但有一个 C# 移植版 - 不过,我认为它可能不完整。你认为它足够可靠以解析这种情况下的条形码,还是有其他更好的库?
编辑:正如 Ed 在评论中指出的那样,我应该先尝试一下。哇,我没有想到。 :) 但我想知道这个部分移植版是否足够可靠 - 如果你们中有人以前用过它,它能够熟练扫描吗?
我已经使用了一年以上的Java版本,每天大约扫描100个,效果非常好。我认为C#版本不会差到哪去。
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设置的内容,但我们发现在我们能够使用的每种情况下,渐进策略比旧策略更快(而且不仅仅是快一点点,而是快得多)。