C#中的ZXing(“斑马线”)

马克西姆·扎斯拉夫斯基(Maxim Zaslavsky):

我正在寻找一个好的开源库,可以从图像中查找和读取条形码(与使用条形码扫描仪相比)。从有关Stack Overflow的其他问题中,我发现ZXing(“斑马线”)非常好。尽管它是为Java制作的,但有一个C#端口-但是,我相信它可能并不完整。您认为从这种情况下解析条形码是否足够可靠,还是其他一些库更好?

编辑:正如Ed在评论中指出的,我应该先尝试一下。哇,我没想到。:)但是我想我的问题是部分端口是否足够可靠-如果您以前曾经使用过它,它可以熟练扫描吗?

凯文·戴(Kevin Day):

当然,这取决于您使用它的目的。甚至zxing的Java版本都有一些重要的限制和性能问题。例如,它只能在页面上找到一个条形码。而且,它用于在页面上定位一维条形码的算法并不是特别有效(对二维条形码的算法一无所知-这不是我正在从事的项目的要求的一部分)。这些都是可以解决的问题-几个月前,我开始进行增强,能够显着提高一维位置的性能和可靠性,但是我们的开发优先级已经改变,因此从那时起我就再也没有进行过开发。

至于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进行设置,但是我们发现,在每种情况下,渐进式策略都比旧策略(不仅快一点,而且快得多)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章