mirror of
https://github.com/zxing/zxing.git
synced 2024-11-09 20:44:03 -08:00
Overhaul 'extractPureBits' implementations and standardize. Improve by looking for bottom-right black pixel. Fix PDF417 pure barcode mode along the way.
git-svn-id: https://zxing.googlecode.com/svn/trunk@1628 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
77b2b199d5
commit
2874ed0988
|
@ -170,6 +170,28 @@ public final class BitMatrix {
|
|||
return new int[] {x, y};
|
||||
}
|
||||
|
||||
public int[] getBottomRightOnBit() {
|
||||
int bitsOffset = bits.length - 1;
|
||||
while (bitsOffset >= 0 && bits[bitsOffset] == 0) {
|
||||
bitsOffset--;
|
||||
}
|
||||
if (bitsOffset < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
int y = bitsOffset / rowSize;
|
||||
int x = (bitsOffset % rowSize) << 5;
|
||||
|
||||
int theBits = bits[bitsOffset];
|
||||
int bit = 31;
|
||||
while ((theBits >>> bit) == 0) {
|
||||
bit--;
|
||||
}
|
||||
x += bit;
|
||||
|
||||
return new int[] {x, y};
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The width of the matrix
|
||||
*/
|
||||
|
|
|
@ -86,76 +86,71 @@ public final class DataMatrixReader implements Reader {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method detects a Data Matrix code in a "pure" image -- that is, pure monochrome image
|
||||
* which contains only an unrotated, unskewed, image of a Data Matrix code, with some white border
|
||||
* This method detects a code in a "pure" image -- that is, pure monochrome image
|
||||
* which contains only an unrotated, unskewed, image of a code, with some white border
|
||||
* around it. This is a specialized method that works exceptionally fast in this special
|
||||
* case.
|
||||
*
|
||||
* @see com.google.zxing.pdf417.PDF417Reader#extractPureBits(BitMatrix)
|
||||
* @see com.google.zxing.qrcode.QRCodeReader#extractPureBits(BitMatrix)
|
||||
*/
|
||||
private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
|
||||
|
||||
int height = image.getHeight();
|
||||
int width = image.getWidth();
|
||||
int minDimension = Math.min(height, width);
|
||||
|
||||
// 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();
|
||||
}
|
||||
int x = leftTopBlack[0];
|
||||
int y = leftTopBlack[1];
|
||||
while (x < minDimension && y < minDimension && image.get(x, y)) {
|
||||
x++;
|
||||
}
|
||||
if (x == minDimension) {
|
||||
int[] rightBottomBlack = image.getBottomRightOnBit();
|
||||
if (leftTopBlack == null || rightBottomBlack == null) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = x - leftTopBlack[0];
|
||||
int moduleSize = moduleSize(leftTopBlack, image);
|
||||
|
||||
// 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 (rowEndOfSymbol < 0) {
|
||||
int top = leftTopBlack[1];
|
||||
int bottom = rightBottomBlack[1];
|
||||
int left = leftTopBlack[0];
|
||||
int right = rightBottomBlack[0];
|
||||
|
||||
int matrixWidth = (right - left + 1) / moduleSize;
|
||||
int matrixHeight = (bottom - top + 1) / moduleSize;
|
||||
if (matrixWidth == 0 || matrixHeight == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
rowEndOfSymbol++;
|
||||
|
||||
// Make sure width of barcode is a multiple of module size
|
||||
if ((rowEndOfSymbol - x) % moduleSize != 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
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.
|
||||
x -= moduleSize >> 1;
|
||||
y -= moduleSize >> 1;
|
||||
|
||||
if ((x + (dimension - 1) * moduleSize) >= width ||
|
||||
(y + (dimension - 1) * moduleSize) >= height) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
int nudge = moduleSize >> 1;
|
||||
top += nudge;
|
||||
left += nudge;
|
||||
|
||||
// Now just read off the bits
|
||||
BitMatrix bits = new BitMatrix(dimension);
|
||||
for (int i = 0; i < dimension; i++) {
|
||||
int iOffset = y + i * moduleSize;
|
||||
for (int j = 0; j < dimension; j++) {
|
||||
if (image.get(x + j * moduleSize, iOffset)) {
|
||||
bits.set(j, i);
|
||||
BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight);
|
||||
for (int y = 0; y < matrixHeight; y++) {
|
||||
int iOffset = top + y * moduleSize;
|
||||
for (int x = 0; x < matrixWidth; x++) {
|
||||
if (image.get(left + x * moduleSize, iOffset)) {
|
||||
bits.set(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
private static int moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
|
||||
int width = image.getWidth();
|
||||
int x = leftTopBlack[0];
|
||||
int y = leftTopBlack[1];
|
||||
while (x < width && image.get(x, y)) {
|
||||
x++;
|
||||
}
|
||||
if (x == width) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = x - leftTopBlack[0];
|
||||
if (moduleSize == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
return moduleSize;
|
||||
}
|
||||
|
||||
}
|
|
@ -29,7 +29,6 @@ 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;
|
||||
|
||||
|
@ -55,12 +54,11 @@ public final class PDF417Reader implements Reader {
|
|||
return decode(image, null);
|
||||
}
|
||||
|
||||
public Result decode(BinaryBitmap image, Hashtable hints)
|
||||
throws NotFoundException, FormatException {
|
||||
public Result decode(BinaryBitmap image, Hashtable hints) throws NotFoundException, FormatException {
|
||||
DecoderResult decoderResult;
|
||||
ResultPoint[] points;
|
||||
if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE)) {
|
||||
BitMatrix bits = QRCodeReader.extractPureBits(image.getBlackMatrix());
|
||||
BitMatrix bits = extractPureBits(image.getBlackMatrix());
|
||||
decoderResult = decoder.decode(bits);
|
||||
points = NO_POINTS;
|
||||
} else {
|
||||
|
@ -76,4 +74,116 @@ public final class PDF417Reader implements Reader {
|
|||
// do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* This method detects a code in a "pure" image -- that is, pure monochrome image
|
||||
* which contains only an unrotated, unskewed, image of a 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)
|
||||
* @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
|
||||
*/
|
||||
private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
|
||||
|
||||
int[] leftTopBlack = image.getTopLeftOnBit();
|
||||
int[] rightBottomBlack = image.getBottomRightOnBit();
|
||||
if (leftTopBlack == null || rightBottomBlack == null) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = moduleSize(leftTopBlack, image);
|
||||
|
||||
int top = leftTopBlack[1];
|
||||
int bottom = rightBottomBlack[1];
|
||||
int left = findPatternStart(leftTopBlack[0], top, image);
|
||||
int right = findPatternEnd(leftTopBlack[0], top, image);
|
||||
|
||||
int matrixWidth = (right - left + 1) / moduleSize;
|
||||
int matrixHeight = (bottom - top + 1) / moduleSize;
|
||||
if (matrixWidth == 0 || matrixHeight == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
// 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.
|
||||
int nudge = moduleSize >> 1;
|
||||
top += nudge;
|
||||
left += nudge;
|
||||
|
||||
// Now just read off the bits
|
||||
BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight);
|
||||
for (int y = 0; y < matrixHeight; y++) {
|
||||
int iOffset = top + y * moduleSize;
|
||||
for (int x = 0; x < matrixWidth; x++) {
|
||||
if (image.get(left + x * moduleSize, iOffset)) {
|
||||
bits.set(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
private static int moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
|
||||
int x = leftTopBlack[0];
|
||||
int y = leftTopBlack[1];
|
||||
int width = image.getWidth();
|
||||
while (x < width && image.get(x, y)) {
|
||||
x++;
|
||||
}
|
||||
if (x == width) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = (x - leftTopBlack[0]) >>> 3; // We've crossed left first bar, which is 8x
|
||||
if (moduleSize == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
return moduleSize;
|
||||
}
|
||||
|
||||
private static int findPatternStart(int x, int y, BitMatrix image) throws NotFoundException {
|
||||
int width = image.getWidth();
|
||||
int start = x;
|
||||
// start should be on black
|
||||
int transitions = 0;
|
||||
boolean black = true;
|
||||
while (start < width - 1 && transitions < 8) {
|
||||
start++;
|
||||
boolean newBlack = image.get(start, y);
|
||||
if (black != newBlack) {
|
||||
transitions++;
|
||||
}
|
||||
black = newBlack;
|
||||
}
|
||||
if (start == width - 1) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
return start;
|
||||
}
|
||||
|
||||
private static int findPatternEnd(int x, int y, BitMatrix image) throws NotFoundException {
|
||||
int width = image.getWidth();
|
||||
int end = width - 1;
|
||||
// end should be on black
|
||||
while (end > x && !image.get(end, y)) {
|
||||
end--;
|
||||
}
|
||||
int transitions = 0;
|
||||
boolean black = true;
|
||||
while (end > x && transitions < 9) {
|
||||
end--;
|
||||
boolean newBlack = image.get(end, y);
|
||||
if (black != newBlack) {
|
||||
transitions++;
|
||||
}
|
||||
black = newBlack;
|
||||
}
|
||||
if (end == x) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,8 +61,7 @@ final class BitMatrixParser {
|
|||
*/
|
||||
int[] readCodewords() throws FormatException {
|
||||
int width = bitMatrix.getWidth();
|
||||
// TODO should be a rectangular matrix
|
||||
int height = width;
|
||||
int height = bitMatrix.getHeight();
|
||||
|
||||
erasures = new int[MAX_CW_CAPACITY];
|
||||
|
||||
|
|
|
@ -90,30 +90,65 @@ public class QRCodeReader implements Reader {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* This method detects a code in a "pure" image -- that is, pure monochrome image
|
||||
* which contains only an unrotated, unskewed, image of a code, with some white border
|
||||
* around it. This is a specialized method that works exceptionally fast in this special
|
||||
* case.
|
||||
*
|
||||
* @see com.google.zxing.pdf417.PDF417Reader#extractPureBits(BitMatrix)
|
||||
* @see com.google.zxing.datamatrix.DataMatrixReader#extractPureBits(BitMatrix)
|
||||
*/
|
||||
public static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
|
||||
private static BitMatrix extractPureBits(BitMatrix image) throws NotFoundException {
|
||||
|
||||
int height = image.getHeight();
|
||||
int width = image.getWidth();
|
||||
int minDimension = Math.min(height, width);
|
||||
|
||||
// And then keep tracking across the top-left black module to determine module size
|
||||
//int moduleEnd = borderWidth;
|
||||
int[] leftTopBlack = image.getTopLeftOnBit();
|
||||
if (leftTopBlack == null) {
|
||||
int[] rightBottomBlack = image.getBottomRightOnBit();
|
||||
if (leftTopBlack == null || rightBottomBlack == null) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = moduleSize(leftTopBlack, image);
|
||||
|
||||
int top = leftTopBlack[1];
|
||||
int bottom = rightBottomBlack[1];
|
||||
int left = leftTopBlack[0];
|
||||
int right = rightBottomBlack[0];
|
||||
|
||||
int matrixWidth = (right - left + 1) / moduleSize;
|
||||
int matrixHeight = (bottom - top + 1) / moduleSize;
|
||||
if (matrixWidth == 0 || matrixHeight == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
// 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.
|
||||
int nudge = moduleSize >> 1;
|
||||
top += nudge;
|
||||
left += nudge;
|
||||
|
||||
// Now just read off the bits
|
||||
BitMatrix bits = new BitMatrix(matrixWidth, matrixHeight);
|
||||
for (int y = 0; y < matrixHeight; y++) {
|
||||
int iOffset = top + y * moduleSize;
|
||||
for (int x = 0; x < matrixWidth; x++) {
|
||||
if (image.get(left + x * moduleSize, iOffset)) {
|
||||
bits.set(x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
}
|
||||
|
||||
private static int moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
|
||||
int height = image.getHeight();
|
||||
int width = image.getWidth();
|
||||
int x = leftTopBlack[0];
|
||||
int y = leftTopBlack[1];
|
||||
while (x < minDimension && y < minDimension && image.get(x, y)) {
|
||||
while (x < width && y < height && image.get(x, y)) {
|
||||
x++;
|
||||
y++;
|
||||
}
|
||||
if (x == minDimension || y == minDimension) {
|
||||
if (x == width || y == height) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
|
@ -121,46 +156,7 @@ public class QRCodeReader implements Reader {
|
|||
if (moduleSize == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
// And now find where the rightmost black module on the first row ends
|
||||
int rowEndOfSymbol = width - 1;
|
||||
while (rowEndOfSymbol > x && !image.get(rowEndOfSymbol, y)) {
|
||||
rowEndOfSymbol--;
|
||||
}
|
||||
if (rowEndOfSymbol <= x) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
rowEndOfSymbol++;
|
||||
|
||||
// Make sure width of barcode is a multiple of module size
|
||||
if ((rowEndOfSymbol - x) % moduleSize != 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
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. Need to back up at least 1.
|
||||
int backOffAmount = moduleSize == 1 ? 1 : moduleSize >> 1;
|
||||
x -= backOffAmount;
|
||||
y -= backOffAmount;
|
||||
|
||||
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 = y + i * moduleSize;
|
||||
for (int j = 0; j < dimension; j++) {
|
||||
if (image.get(x + j * moduleSize, iOffset)) {
|
||||
bits.set(j, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bits;
|
||||
return moduleSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -250,7 +250,7 @@ public final class CommandLineRunner {
|
|||
} catch (NotFoundException nfe) {
|
||||
System.out.println(uri.toString() + ": No barcode found");
|
||||
return null;
|
||||
} finally {
|
||||
// } finally {
|
||||
// Uncomment these lines when turning on exception tracking in ReaderException.
|
||||
//System.out.println("Threw " + ReaderException.getExceptionCountAndReset() + " exceptions");
|
||||
//System.out.println("Throwers:\n" + ReaderException.getThrowersAndReset());
|
||||
|
|
Loading…
Reference in a new issue