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:
srowen 2008-03-11 15:26:45 +00:00
parent fd1620e753
commit df4e3ac853
2 changed files with 37 additions and 11 deletions

View file

@ -70,27 +70,41 @@ public final class DataMatrixReader implements Reader {
* around it. This is a specialized method that works exceptionally fast in this special
* case.
*/
private static BitMatrix extractPureBits(MonochromeBitmapSource image)
throws ReaderException {
private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
// Now need to determine module size in pixels
// First, skip white border by tracking diagonally from the top left down and to the right:
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 (!image.isBlack(borderWidth, borderWidth)) {
while (borderWidth < minDimension && !image.isBlack(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
int moduleEnd = borderWidth + 1;
while (image.isBlack(moduleEnd, borderWidth)) {
while (moduleEnd < width && image.isBlack(moduleEnd, borderWidth)) {
moduleEnd++;
}
if (moduleEnd == width) {
throw new ReaderException("No end to black pixels found along row");
}
int moduleSize = moduleEnd - borderWidth;
// And now find where the bottommost black module on the first column ends
int columnEndOfSymbol = image.getHeight() - 1;
while (!image.isBlack(borderWidth, columnEndOfSymbol)) {
while (columnEndOfSymbol >= 0 && !image.isBlack(borderWidth, columnEndOfSymbol)) {
columnEndOfSymbol--;
}
if (columnEndOfSymbol < 0) {
throw new ReaderException("Can't find end of bottommost black module");
}
columnEndOfSymbol++;
// Make sure width of barcode is a multiple of module size

View file

@ -73,27 +73,39 @@ public final class QRCodeReader implements Reader {
* around it. This is a specialized method that works exceptionally fast in this special
* case.
*/
private static BitMatrix extractPureBits(MonochromeBitmapSource image)
throws ReaderException {
private static BitMatrix extractPureBits(MonochromeBitmapSource image) throws ReaderException {
// 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:
int borderWidth = 0;
while (!image.isBlack(borderWidth, borderWidth)) {
while (borderWidth < minDimension && !image.isBlack(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
int moduleEnd = borderWidth;
while (image.isBlack(moduleEnd, moduleEnd)) {
while (moduleEnd < minDimension && image.isBlack(moduleEnd, moduleEnd)) {
moduleEnd++;
}
if (moduleEnd == minDimension) {
throw new ReaderException("No end to black pixels found along diagonal");
}
int moduleSize = moduleEnd - borderWidth;
// And now find where the rightmost black module on the first row ends
int rowEndOfSymbol = image.getWidth() - 1;
while (!image.isBlack(rowEndOfSymbol, borderWidth)) {
while (rowEndOfSymbol >= 0 && !image.isBlack(rowEndOfSymbol, borderWidth)) {
rowEndOfSymbol--;
}
if (rowEndOfSymbol < 0) {
throw new ReaderException("Can't find end of rightmost black module");
}
rowEndOfSymbol++;
// Make sure width of barcode is a multiple of module size