mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
QR code "pure barcode" now forgiving of uneven module sizes
git-svn-id: https://zxing.googlecode.com/svn/trunk@2103 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
1edb8c6dca
commit
e4bd93a6e8
|
@ -112,7 +112,7 @@ public class QRCodeReader implements Reader {
|
|||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = moduleSize(leftTopBlack, image);
|
||||
float moduleSize = moduleSize(leftTopBlack, image);
|
||||
|
||||
int top = leftTopBlack[1];
|
||||
int bottom = rightBottomBlack[1];
|
||||
|
@ -125,8 +125,8 @@ public class QRCodeReader implements Reader {
|
|||
right = left + (bottom - top);
|
||||
}
|
||||
|
||||
int matrixWidth = (right - left + 1) / moduleSize;
|
||||
int matrixHeight = (bottom - top + 1) / moduleSize;
|
||||
int matrixWidth = Math.round((right - left + 1) / moduleSize);
|
||||
int matrixHeight = Math.round((bottom - top + 1) / moduleSize);
|
||||
if (matrixWidth <= 0 || matrixHeight <= 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
@ -138,16 +138,16 @@ public class QRCodeReader implements Reader {
|
|||
// 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;
|
||||
int nudge = Math.round(moduleSize / 2.0f);
|
||||
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;
|
||||
int iOffset = top + (int) (y * moduleSize);
|
||||
for (int x = 0; x < matrixWidth; x++) {
|
||||
if (image.get(left + x * moduleSize, iOffset)) {
|
||||
if (image.get(left + (int) (x * moduleSize), iOffset)) {
|
||||
bits.set(x, y);
|
||||
}
|
||||
}
|
||||
|
@ -155,24 +155,27 @@ public class QRCodeReader implements Reader {
|
|||
return bits;
|
||||
}
|
||||
|
||||
private static int moduleSize(int[] leftTopBlack, BitMatrix image) throws NotFoundException {
|
||||
private static float 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 < width && y < height && image.get(x, y)) {
|
||||
boolean inBlack = true;
|
||||
int transitions = 0;
|
||||
while (x < width && y < height) {
|
||||
if (inBlack != image.get(x, y)) {
|
||||
if (++transitions == 5) {
|
||||
break;
|
||||
}
|
||||
inBlack = !inBlack;
|
||||
}
|
||||
x++;
|
||||
y++;
|
||||
}
|
||||
if (x == width || y == height) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
|
||||
int moduleSize = x - leftTopBlack[0];
|
||||
if (moduleSize == 0) {
|
||||
throw NotFoundException.getNotFoundInstance();
|
||||
}
|
||||
return moduleSize;
|
||||
return (x - leftTopBlack[0]) / 7.0f;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue