Pure-barcode mode can now deal with asymmetric borders instead of assuming it's pure generator output with equal borders

git-svn-id: https://zxing.googlecode.com/svn/trunk@1288 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2010-04-06 21:30:53 +00:00
parent 634273cfdb
commit 06a6a35ce1
4 changed files with 78 additions and 124 deletions

View file

@ -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
*/

View file

@ -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) {
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 moduleEnd = borderWidth;
int[] leftTopBlack = image.getTopLeftOnBit();
if (leftTopBlack == null) {
throw NotFoundException.getNotFoundInstance();
}
if (moduleEnd == width) {
int x = leftTopBlack[0];
int y = leftTopBlack[1];
while (x < minDimension && y < minDimension && image.get(x, y)) {
x++;
}
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);
}
}

View file

@ -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;
}
}

View file

@ -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) {
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 moduleEnd = borderWidth;
int[] leftTopBlack = image.getTopLeftOnBit();
if (leftTopBlack == null) {
throw NotFoundException.getNotFoundInstance();
}
if (moduleEnd == minDimension) {
int x = leftTopBlack[0];
int y = leftTopBlack[1];
while (x < minDimension && y < minDimension && image.get(x, y)) {
x++;
y++;
}
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);
}
}