1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| public final class GenericMultipleBarcodeReader implements MultipleBarcodeReader {
private static final int MIN_DIMENSION_TO_RECUR = 100; private static final int MAX_DEPTH = 4;
private final Reader delegate;
public GenericMultipleBarcodeReader(Reader delegate) { this.delegate = delegate; }
@Override public Result[] decodeMultiple(BinaryBitmap image) throws NotFoundException { return decodeMultiple(image, null); }
@Override public Result[] decodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints) throws NotFoundException { List<Result> results = new ArrayList<>(); doDecodeMultiple(image, hints, results, 0, 0, 0); if (results.isEmpty()) { throw NotFoundException.getNotFoundInstance(); } return results.toArray(new Result[results.size()]); }
private void doDecodeMultiple(BinaryBitmap image, Map<DecodeHintType,?> hints, List<Result> results, int xOffset, int yOffset, int currentDepth) { if (currentDepth > MAX_DEPTH) { return; }
Result result; try { result = delegate.decode(image, hints); } catch (ReaderException ignored) { return; } boolean alreadyFound = false; for (Result existingResult : results) { if (existingResult.getText().equals(result.getText())) { alreadyFound = true; break; } } if (!alreadyFound) { results.add(translateResultPoints(result, xOffset, yOffset)); } ResultPoint[] resultPoints = result.getResultPoints(); if (resultPoints == null || resultPoints.length == 0) { return; } int width = image.getWidth(); int height = image.getHeight(); float minX = width; float minY = height; float maxX = 0.0f; float maxY = 0.0f; for (ResultPoint point : resultPoints) { if (point == null) { continue; } float x = point.getX(); float y = point.getY(); if (x < minX) { minX = x; } if (y < minY) { minY = y; } if (x > maxX) { maxX = x; } if (y > maxY) { maxY = y; } }
if (minX > MIN_DIMENSION_TO_RECUR) { doDecodeMultiple(image.crop(0, 0, (int) minX, height), hints, results, xOffset, yOffset, currentDepth + 1); } if (minY > MIN_DIMENSION_TO_RECUR) { doDecodeMultiple(image.crop(0, 0, width, (int) minY), hints, results, xOffset, yOffset, currentDepth + 1); } if (maxX < width - MIN_DIMENSION_TO_RECUR) { doDecodeMultiple(image.crop((int) maxX, 0, width - (int) maxX, height), hints, results, xOffset + (int) maxX, yOffset, currentDepth + 1); } if (maxY < height - MIN_DIMENSION_TO_RECUR) { doDecodeMultiple(image.crop(0, (int) maxY, width, height - (int) maxY), hints, results, xOffset, yOffset + (int) maxY, currentDepth + 1); } }
private static Result translateResultPoints(Result result, int xOffset, int yOffset) { ResultPoint[] oldResultPoints = result.getResultPoints(); if (oldResultPoints == null) { return result; } ResultPoint[] newResultPoints = new ResultPoint[oldResultPoints.length]; for (int i = 0; i < oldResultPoints.length; i++) { ResultPoint oldPoint = oldResultPoints[i]; if (oldPoint != null) { newResultPoints[i] = new ResultPoint(oldPoint.getX() + xOffset, oldPoint.getY() + yOffset); } } Result newResult = new Result(result.getText(), result.getRawBytes(), result.getNumBits(), newResultPoints, result.getBarcodeFormat(), result.getTimestamp()); newResult.putAllMetadata(result.getResultMetadata()); return newResult; }
}
|