From d4d24eb42568bc5e3879cdb6007b7267442c9e85 Mon Sep 17 00:00:00 2001 From: srowen Date: Tue, 6 Apr 2010 21:30:53 +0000 Subject: [PATCH] Pure-barcode mode can now deal with asymmetric borders instead of assuming it's pure generator output with equal borders git-svn-id: http://zxing.googlecode.com/svn/trunk@1288 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- .../com/google/zxing/common/BitMatrix.java | 25 +++++++ .../zxing/datamatrix/DataMatrixReader.java | 55 +++++++------- .../com/google/zxing/pdf417/PDF417Reader.java | 74 +------------------ .../com/google/zxing/qrcode/QRCodeReader.java | 44 ++++++----- 4 files changed, 76 insertions(+), 122 deletions(-) diff --git a/core/src/com/google/zxing/common/BitMatrix.java b/core/src/com/google/zxing/common/BitMatrix.java index f1023b8b..d1e6974a 100755 --- a/core/src/com/google/zxing/common/BitMatrix.java +++ b/core/src/com/google/zxing/common/BitMatrix.java @@ -145,6 +145,31 @@ public final class BitMatrix { return row; } + /** + * This is useful in detecting a corner of a 'pure' barcode. + * + * @return {x,y} coordinate of top-left-most 1 bit, or null if it is all white + */ + public int[] getTopLeftOnBit() { + int bitsOffset = 0; + while (bitsOffset < bits.length && bits[bitsOffset] == 0) { + bitsOffset++; + } + if (bitsOffset == bits.length) { + return null; + } + int y = bitsOffset / rowSize; + int x = (bitsOffset % rowSize) << 5; + + int theBits = bits[bitsOffset]; + int bit = 0; + while ((theBits << (31-bit)) == 0) { + bit++; + } + x += bit; + return new int[] {x, y}; + } + /** * @return The width of the matrix */ diff --git a/core/src/com/google/zxing/datamatrix/DataMatrixReader.java b/core/src/com/google/zxing/datamatrix/DataMatrixReader.java index 01549172..477a989e 100644 --- a/core/src/com/google/zxing/datamatrix/DataMatrixReader.java +++ b/core/src/com/google/zxing/datamatrix/DataMatrixReader.java @@ -89,66 +89,67 @@ public final class DataMatrixReader implements Reader { * which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border * around it. This is a specialized method that works exceptionally fast in this special * case. + * + * @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix) */ private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { - // Now need to determine module size in pixels int height = image.getHeight(); int width = image.getWidth(); int minDimension = Math.min(height, width); - // First, skip white border by tracking diagonally from the top left down and to the right: - int borderWidth = 0; - while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) { - borderWidth++; - } - if (borderWidth == minDimension) { + // And then keep tracking across the top-left black module to determine module size + //int moduleEnd = borderWidth; + int[] leftTopBlack = image.getTopLeftOnBit(); + if (leftTopBlack == null) { throw NotFoundException.getNotFoundInstance(); } - - // And then keep tracking across the top-left black module to determine module size - int moduleEnd = borderWidth + 1; - while (moduleEnd < width && image.get(moduleEnd, borderWidth)) { - moduleEnd++; + int x = leftTopBlack[0]; + int y = leftTopBlack[1]; + while (x < minDimension && y < minDimension && image.get(x, y)) { + x++; } - if (moduleEnd == width) { + if (x == minDimension) { throw NotFoundException.getNotFoundInstance(); } - int moduleSize = moduleEnd - borderWidth; + int moduleSize = x - leftTopBlack[0]; - // And now find where the bottommost black module on the first column ends - int columnEndOfSymbol = height - 1; - while (columnEndOfSymbol >= 0 && !image.get(borderWidth, columnEndOfSymbol)) { - columnEndOfSymbol--; + // And now find where the rightmost black module on the first row ends + int rowEndOfSymbol = width - 1; + while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) { + rowEndOfSymbol--; } - if (columnEndOfSymbol < 0) { + if (rowEndOfSymbol < 0) { throw NotFoundException.getNotFoundInstance(); } - columnEndOfSymbol++; + rowEndOfSymbol++; // Make sure width of barcode is a multiple of module size - if ((columnEndOfSymbol - borderWidth) % moduleSize != 0) { + if ((rowEndOfSymbol - x) % moduleSize != 0) { throw NotFoundException.getNotFoundInstance(); } - int dimension = (columnEndOfSymbol - borderWidth) / moduleSize; + int dimension = 2 + ((rowEndOfSymbol - x) / moduleSize); + y += moduleSize; + // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. - borderWidth += moduleSize >> 1; + x -= moduleSize >> 1; + y -= moduleSize >> 1; - int sampleDimension = borderWidth + (dimension - 1) * moduleSize; - if (sampleDimension >= width || sampleDimension >= height) { + if ((x + (dimension - 1) * moduleSize) >= width || + (y + (dimension - 1) * moduleSize) >= height) { throw NotFoundException.getNotFoundInstance(); } // Now just read off the bits BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { - int iOffset = borderWidth + i * moduleSize; + int iOffset = y + i * moduleSize; for (int j = 0; j < dimension; j++) { - if (image.get(borderWidth + j * moduleSize, iOffset)) { + if (image.get(x + j * moduleSize, iOffset)) { bits.set(j, i); } } diff --git a/core/src/com/google/zxing/pdf417/PDF417Reader.java b/core/src/com/google/zxing/pdf417/PDF417Reader.java index 1fcd1f12..f362ff19 100644 --- a/core/src/com/google/zxing/pdf417/PDF417Reader.java +++ b/core/src/com/google/zxing/pdf417/PDF417Reader.java @@ -29,6 +29,7 @@ import com.google.zxing.common.DecoderResult; import com.google.zxing.common.DetectorResult; import com.google.zxing.pdf417.decoder.Decoder; import com.google.zxing.pdf417.detector.Detector; +import com.google.zxing.qrcode.QRCodeReader; import java.util.Hashtable; @@ -59,7 +60,7 @@ public final class PDF417Reader implements Reader { DecoderResult decoderResult; ResultPoint[] points; if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) { - BitMatrix bits = extractPureBits(image); + BitMatrix bits = QRCodeReader.extractPureBits(image.getBlackMatrix()); decoderResult = decoder.decode(bits); points = NO_POINTS; } else { @@ -75,75 +76,4 @@ public final class PDF417Reader implements Reader { // do nothing } - /** - * This method detects a barcode in a "pure" image -- that is, pure monochrome image - * which contains only an unrotated, unskewed, image of a barcode, with some white border - * around it. This is a specialized method that works exceptionally fast in this special - * case. - */ - private static BitMatrix extractPureBits(BinaryBitmap image) throws NotFoundException { - // Now need to determine module size in pixels - BitMatrix matrix = image.getBlackMatrix(); - int height = matrix.getHeight(); - int width = matrix.getWidth(); - int minDimension = Math.min(height, width); - - // First, skip white border by tracking diagonally from the top left down and to the right: - int borderWidth = 0; - while (borderWidth < minDimension && !matrix.get(borderWidth, borderWidth)) { - borderWidth++; - } - if (borderWidth == minDimension) { - throw NotFoundException.getNotFoundInstance(); - } - - // And then keep tracking across the top-left black module to determine module size - int moduleEnd = borderWidth; - while (moduleEnd < minDimension && matrix.get(moduleEnd, moduleEnd)) { - moduleEnd++; - } - if (moduleEnd == minDimension) { - throw NotFoundException.getNotFoundInstance(); - } - - int moduleSize = moduleEnd - borderWidth; - - // And now find where the rightmost black module on the first row ends - int rowEndOfSymbol = width - 1; - while (rowEndOfSymbol >= 0 && !matrix.get(rowEndOfSymbol, borderWidth)) { - rowEndOfSymbol--; - } - if (rowEndOfSymbol < 0) { - throw NotFoundException.getNotFoundInstance(); - } - rowEndOfSymbol++; - - // Make sure width of barcode is a multiple of module size - if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) { - throw NotFoundException.getNotFoundInstance(); - } - int dimension = (rowEndOfSymbol - borderWidth) / moduleSize; - - // Push in the "border" by half the module width so that we start - // sampling in the middle of the module. Just in case the image is a - // little off, this will help recover. - borderWidth += moduleSize >> 1; - - int sampleDimension = borderWidth + (dimension - 1) * moduleSize; - if (sampleDimension >= width || sampleDimension >= height) { - throw NotFoundException.getNotFoundInstance(); - } - - // Now just read off the bits - BitMatrix bits = new BitMatrix(dimension); - for (int y = 0; y < dimension; y++) { - int iOffset = borderWidth + y * moduleSize; - for (int x = 0; x < dimension; x++) { - if (matrix.get(borderWidth + x * moduleSize, iOffset)) { - bits.set(x, y); - } - } - } - return bits; - } } diff --git a/core/src/com/google/zxing/qrcode/QRCodeReader.java b/core/src/com/google/zxing/qrcode/QRCodeReader.java index d9abc934..296f27d9 100644 --- a/core/src/com/google/zxing/qrcode/QRCodeReader.java +++ b/core/src/com/google/zxing/qrcode/QRCodeReader.java @@ -95,36 +95,33 @@ public class QRCodeReader implements Reader { * around it. This is a specialized method that works exceptionally fast in this special * case. */ - private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { - // Now need to determine module size in pixels + public static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException { int height = image.getHeight(); int width = image.getWidth(); int minDimension = Math.min(height, width); - // First, skip white border by tracking diagonally from the top left down and to the right: - int borderWidth = 0; - while (borderWidth < minDimension && !image.get(borderWidth, borderWidth)) { - borderWidth++; - } - if (borderWidth == minDimension) { + // And then keep tracking across the top-left black module to determine module size + //int moduleEnd = borderWidth; + int[] leftTopBlack = image.getTopLeftOnBit(); + if (leftTopBlack == null) { throw NotFoundException.getNotFoundInstance(); } - - // And then keep tracking across the top-left black module to determine module size - int moduleEnd = borderWidth; - while (moduleEnd < minDimension && image.get(moduleEnd, moduleEnd)) { - moduleEnd++; + int x = leftTopBlack[0]; + int y = leftTopBlack[1]; + while (x < minDimension && y < minDimension && image.get(x, y)) { + x++; + y++; } - if (moduleEnd == minDimension) { + if (x == minDimension || y == minDimension) { throw NotFoundException.getNotFoundInstance(); } - int moduleSize = moduleEnd - borderWidth; + int moduleSize = x - leftTopBlack[0]; // And now find where the rightmost black module on the first row ends int rowEndOfSymbol = width - 1; - while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, borderWidth)) { + while (rowEndOfSymbol >= 0 && !image.get(rowEndOfSymbol, y)) { rowEndOfSymbol--; } if (rowEndOfSymbol < 0) { @@ -133,27 +130,28 @@ public class QRCodeReader implements Reader { rowEndOfSymbol++; // Make sure width of barcode is a multiple of module size - if ((rowEndOfSymbol - borderWidth) % moduleSize != 0) { + if ((rowEndOfSymbol - x) % moduleSize != 0) { throw NotFoundException.getNotFoundInstance(); } - int dimension = (rowEndOfSymbol - borderWidth) / moduleSize; + int dimension = 1 + ((rowEndOfSymbol - x) / moduleSize); // Push in the "border" by half the module width so that we start // sampling in the middle of the module. Just in case the image is a // little off, this will help recover. - borderWidth += moduleSize >> 1; + x -= moduleSize >> 1; + y -= moduleSize >> 1; - int sampleDimension = borderWidth + (dimension - 1) * moduleSize; - if (sampleDimension >= width || sampleDimension >= height) { + if ((x + (dimension - 1) * moduleSize) >= width || + (y + (dimension - 1) * moduleSize) >= height) { throw NotFoundException.getNotFoundInstance(); } // Now just read off the bits BitMatrix bits = new BitMatrix(dimension); for (int i = 0; i < dimension; i++) { - int iOffset = borderWidth + i * moduleSize; + int iOffset = y + i * moduleSize; for (int j = 0; j < dimension; j++) { - if (image.get(borderWidth + j * moduleSize, iOffset)) { + if (image.get(x + j * moduleSize, iOffset)) { bits.set(j, i); } } -- 2.20.1