package com.google.zxing.multi;
+import com.google.zxing.BinaryBitmap;
+import com.google.zxing.NotFoundException;
import com.google.zxing.Reader;
-import com.google.zxing.Result;
-import com.google.zxing.MonochromeBitmapSource;
import com.google.zxing.ReaderException;
+import com.google.zxing.Result;
import com.google.zxing.ResultPoint;
-import com.google.zxing.CroppedMonochromeBitmapSource;
import java.util.Hashtable;
import java.util.Vector;
* After one barcode is found, the areas left, above, right and below the barcode's
* {@link com.google.zxing.ResultPoint}s are scanned, recursively.</p>
*
- * <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to
- * find multiple 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might
- * prevent detecting any one of them.</p>
+ * <p>A caller may want to also employ {@link ByQuadrantReader} when attempting to find multiple
+ * 2D barcodes, like QR Codes, in an image, where the presence of multiple barcodes might prevent
+ * detecting any one of them.</p>
*
* <p>That is, instead of passing a {@link Reader} a caller might pass
* <code>new ByQuadrantReader(reader)</code>.</p>
*/
public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
+ private static final int MIN_DIMENSION_TO_RECUR = 100;
+
private final Reader delegate;
public GenericMultipleBarcodeReader(Reader delegate) {
this.delegate = delegate;
}
- public Result[] decodeMultiple(MonochromeBitmapSource image) throws ReaderException {
+ public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException {
return decodeMultiple(image, null);
}
- public Result[] decodeMultiple(MonochromeBitmapSource image, Hashtable hints) throws ReaderException {
+ public Result[] decodeMultiple(BinaryBitmap image, Hashtable hints)
+ throws NotFoundException {
Vector results = new Vector();
doDecodeMultiple(image, hints, results, 0, 0);
if (results.isEmpty()) {
- throw ReaderException.getInstance();
+ throw NotFoundException.getNotFoundInstance();
}
int numResults = results.size();
Result[] resultArray = new Result[numResults];
return resultArray;
}
- private void doDecodeMultiple(MonochromeBitmapSource image,
+ private void doDecodeMultiple(BinaryBitmap image,
Hashtable hints,
Vector results,
int xOffset,
} catch (ReaderException re) {
return;
}
+ boolean alreadyFound = false;
+ for (int i = 0; i < results.size(); i++) {
+ Result existingResult = (Result) results.elementAt(i);
+ if (existingResult.getText().equals(result.getText())) {
+ alreadyFound = true;
+ break;
+ }
+ }
+ if (alreadyFound) {
+ return;
+ }
results.addElement(translateResultPoints(result, xOffset, yOffset));
ResultPoint[] resultPoints = result.getResultPoints();
if (resultPoints == null || resultPoints.length == 0) {
}
}
- if (minX > 0) {
- doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, (int) minX - 1, height),
- hints, results, 0, 0);
+ // Decode left of barcode
+ if (minX > MIN_DIMENSION_TO_RECUR) {
+ doDecodeMultiple(image.crop(0, 0, (int) minX, height),
+ hints, results, xOffset, yOffset);
}
- if (minY > 0) {
- doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, 0, width, (int) minY - 1),
- hints, results, 0, 0);
+ // Decode above barcode
+ if (minY > MIN_DIMENSION_TO_RECUR) {
+ doDecodeMultiple(image.crop(0, 0, width, (int) minY),
+ hints, results, xOffset, yOffset);
}
- if (maxX < width - 1) {
- doDecodeMultiple(new CroppedMonochromeBitmapSource(image, (int) maxX, 0, width, height),
- hints, results, (int) maxX, 0);
+ // Decode right of barcode
+ if (maxX < width - MIN_DIMENSION_TO_RECUR) {
+ doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height),
+ hints, results, xOffset + (int) maxX, yOffset);
}
- if (maxY < height - 1) {
- doDecodeMultiple(new CroppedMonochromeBitmapSource(image, 0, (int) maxY, width, height),
- hints, results, 0, (int) maxY);
+ // Decode below barcode
+ if (maxY < height - MIN_DIMENSION_TO_RECUR) {
+ doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY),
+ hints, results, xOffset, yOffset + (int) maxY);
}
}
ResultPoint oldPoint = oldResultPoints[i];
newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset);
}
- return new Result(result.getText(), result.getRawBytes(), newResultPoints, result.getBarcodeFormat());
+ return new Result(result.getText(), result.getRawBytes(), newResultPoints,
+ result.getBarcodeFormat());
}
}