decode static method
DecoderResult
decode(
- BitMatrix image,
- ResultPoint? imageTopLeft,
- ResultPoint? imageBottomLeft,
- ResultPoint? imageTopRight,
- ResultPoint? imageBottomRight,
- int minCodewordWidth,
- int maxCodewordWidth,
Implementation
static DecoderResult decode(
BitMatrix image,
ResultPoint? imageTopLeft,
ResultPoint? imageBottomLeft,
ResultPoint? imageTopRight,
ResultPoint? imageBottomRight,
int minCodewordWidth,
int maxCodewordWidth,
) {
BoundingBox boundingBox = BoundingBox(
image,
imageTopLeft,
imageBottomLeft,
imageTopRight,
imageBottomRight,
);
DetectionResultRowIndicatorColumn? leftRowIndicatorColumn;
DetectionResultRowIndicatorColumn? rightRowIndicatorColumn;
DetectionResult? detectionResult;
for (bool firstPass = true;; firstPass = false) {
if (imageTopLeft != null) {
leftRowIndicatorColumn = _getRowIndicatorColumn(
image,
boundingBox,
imageTopLeft,
true,
minCodewordWidth,
maxCodewordWidth,
);
}
if (imageTopRight != null) {
rightRowIndicatorColumn = _getRowIndicatorColumn(
image,
boundingBox,
imageTopRight,
false,
minCodewordWidth,
maxCodewordWidth,
);
}
detectionResult = _merge(leftRowIndicatorColumn, rightRowIndicatorColumn);
if (detectionResult == null) {
throw NotFoundException.instance;
}
final resultBox = detectionResult.boundingBox;
if (firstPass &&
resultBox != null &&
(resultBox.minY < boundingBox.minY ||
resultBox.maxY > boundingBox.maxY)) {
boundingBox = resultBox;
} else {
break;
}
}
detectionResult.boundingBox = boundingBox;
final maxBarcodeColumn = detectionResult.barcodeColumnCount + 1;
detectionResult.setDetectionResultColumn(0, leftRowIndicatorColumn);
detectionResult.setDetectionResultColumn(
maxBarcodeColumn,
rightRowIndicatorColumn,
);
final leftToRight = leftRowIndicatorColumn != null;
for (int barcodeColumnCount = 1;
barcodeColumnCount <= maxBarcodeColumn;
barcodeColumnCount++) {
final barcodeColumn = leftToRight
? barcodeColumnCount
: maxBarcodeColumn - barcodeColumnCount;
if (detectionResult.getDetectionResultColumn(barcodeColumn) != null) {
// This will be the case for the opposite row indicator column, which doesn't need to be decoded again.
continue;
}
late DetectionResultColumn detectionResultColumn;
if (barcodeColumn == 0 || barcodeColumn == maxBarcodeColumn) {
detectionResultColumn =
DetectionResultRowIndicatorColumn(boundingBox, barcodeColumn == 0);
} else {
detectionResultColumn = DetectionResultColumn(boundingBox);
}
detectionResult.setDetectionResultColumn(
barcodeColumn,
detectionResultColumn,
);
int startColumn = -1;
int previousStartColumn = startColumn;
// TODO start at a row for which we know the start position, then detect upwards and downwards from there.
for (int imageRow = boundingBox.minY;
imageRow <= boundingBox.maxY;
imageRow++) {
startColumn = _getStartColumn(
detectionResult,
barcodeColumn,
imageRow,
leftToRight,
);
if (startColumn < 0 || startColumn > boundingBox.maxX) {
if (previousStartColumn == -1) {
continue;
}
startColumn = previousStartColumn;
}
final codeword = _detectCodeword(
image,
boundingBox.minX,
boundingBox.maxX,
leftToRight,
startColumn,
imageRow,
minCodewordWidth,
maxCodewordWidth,
);
if (codeword != null) {
detectionResultColumn.setCodeword(imageRow, codeword);
previousStartColumn = startColumn;
minCodewordWidth = math.min(minCodewordWidth, codeword.width);
maxCodewordWidth = math.max(maxCodewordWidth, codeword.width);
}
}
}
return _createDecoderResult(detectionResult);
}