mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Added some needed bounds checking in Data Matrix, QR Code extractPureBits() methods
git-svn-id: https://zxing.googlecode.com/svn/trunk@267 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
fd1620e753
commit
df4e3ac853
|
@ -70,27 +70,41 @@ public final class DataMatrixReader implements Reader {
|
||||||
* around it. This is a specialized method that works exceptionally fast in this special
|
* around it. This is a specialized method that works exceptionally fast in this special
|
||||||
* case.
|
* case.
|
||||||
*/
|
*/
|
||||||
private static BitMatrix extractPureBits(MonochromeBitmapSource image)
|
private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
|
||||||
throws ReaderException {
|
|
||||||
// Now need to determine module size in pixels
|
// 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:
|
// First, skip white border by tracking diagonally from the top left down and to the right:
|
||||||
int borderWidth = 0;
|
int borderWidth = 0;
|
||||||
while (!image.isBlack(borderWidth, borderWidth)) {
|
while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
|
||||||
borderWidth++;
|
borderWidth++;
|
||||||
}
|
}
|
||||||
|
if (borderWidth == minDimension) {
|
||||||
|
throw new ReaderException("No black pixels found along diagonal");
|
||||||
|
}
|
||||||
|
|
||||||
// And then keep tracking across the top-left black module to determine module size
|
// And then keep tracking across the top-left black module to determine module size
|
||||||
int moduleEnd = borderWidth + 1;
|
int moduleEnd = borderWidth + 1;
|
||||||
while (image.isBlack(moduleEnd, borderWidth)) {
|
while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
|
||||||
moduleEnd++;
|
moduleEnd++;
|
||||||
}
|
}
|
||||||
|
if (moduleEnd == width) {
|
||||||
|
throw new ReaderException("No end to black pixels found along row");
|
||||||
|
}
|
||||||
|
|
||||||
int moduleSize = moduleEnd - borderWidth;
|
int moduleSize = moduleEnd - borderWidth;
|
||||||
|
|
||||||
// And now find where the bottommost black module on the first column ends
|
// And now find where the bottommost black module on the first column ends
|
||||||
int columnEndOfSymbol = image.getHeight() - 1;
|
int columnEndOfSymbol = image.getHeight() - 1;
|
||||||
while (!image.isBlack(borderWidth, columnEndOfSymbol)) {
|
while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
|
||||||
columnEndOfSymbol--;
|
columnEndOfSymbol--;
|
||||||
}
|
}
|
||||||
|
if (columnEndOfSymbol < 0) {
|
||||||
|
throw new ReaderException("Can't find end of bottommost black module");
|
||||||
|
}
|
||||||
columnEndOfSymbol++;
|
columnEndOfSymbol++;
|
||||||
|
|
||||||
// Make sure width of barcode is a multiple of module size
|
// Make sure width of barcode is a multiple of module size
|
||||||
|
|
|
@ -73,27 +73,39 @@ public final class QRCodeReader implements Reader {
|
||||||
* around it. This is a specialized method that works exceptionally fast in this special
|
* around it. This is a specialized method that works exceptionally fast in this special
|
||||||
* case.
|
* case.
|
||||||
*/
|
*/
|
||||||
private static BitMatrix extractPureBits(MonochromeBitmapSource image)
|
private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
|
||||||
throws ReaderException {
|
|
||||||
// Now need to determine module size in pixels
|
// Now need to determine module size in pixels
|
||||||
|
|
||||||
|
int minDimension = Math.min(image.getHeight(), image.getWidth());
|
||||||
|
|
||||||
// First, skip white border by tracking diagonally from the top left down and to the right:
|
// First, skip white border by tracking diagonally from the top left down and to the right:
|
||||||
int borderWidth = 0;
|
int borderWidth = 0;
|
||||||
while (!image.isBlack(borderWidth, borderWidth)) {
|
while (borderWidth < minDimension && !image.isBlack(borderWidth, borderWidth)) {
|
||||||
borderWidth++;
|
borderWidth++;
|
||||||
}
|
}
|
||||||
|
if (borderWidth == minDimension) {
|
||||||
|
throw new ReaderException("No black pixels found along diagonal");
|
||||||
|
}
|
||||||
|
|
||||||
// And then keep tracking across the top-left black module to determine module size
|
// And then keep tracking across the top-left black module to determine module size
|
||||||
int moduleEnd = borderWidth;
|
int moduleEnd = borderWidth;
|
||||||
while (image.isBlack(moduleEnd, moduleEnd)) {
|
while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
|
||||||
moduleEnd++;
|
moduleEnd++;
|
||||||
}
|
}
|
||||||
|
if (moduleEnd == minDimension) {
|
||||||
|
throw new ReaderException("No end to black pixels found along diagonal");
|
||||||
|
}
|
||||||
|
|
||||||
int moduleSize = moduleEnd - borderWidth;
|
int moduleSize = moduleEnd - borderWidth;
|
||||||
|
|
||||||
// And now find where the rightmost black module on the first row ends
|
// And now find where the rightmost black module on the first row ends
|
||||||
int rowEndOfSymbol = image.getWidth() - 1;
|
int rowEndOfSymbol = image.getWidth() - 1;
|
||||||
while (!image.isBlack(rowEndOfSymbol, borderWidth)) {
|
while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
|
||||||
rowEndOfSymbol--;
|
rowEndOfSymbol--;
|
||||||
}
|
}
|
||||||
|
if (rowEndOfSymbol < 0) {
|
||||||
|
throw new ReaderException("Can't find end of rightmost black module");
|
||||||
|
}
|
||||||
rowEndOfSymbol++;
|
rowEndOfSymbol++;
|
||||||
|
|
||||||
// Make sure width of barcode is a multiple of module size
|
// Make sure width of barcode is a multiple of module size
|
||||||
|
|
Loading…
Reference in a new issue