Adds orientation of pdf417 barcode to Result metadata (#1522)

* Adds orientation of pdf417 barcode to Result metadata

Co-authored-by: Jordan Wild <jwild@simplenexus.com>
This commit is contained in:
Jordan Wild 2022-05-04 17:57:36 -06:00 committed by GitHub
parent 83cdc82aa1
commit c7a7b30f04
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 13 deletions

View file

@ -286,6 +286,27 @@ public final class BitMatrix implements Cloneable {
System.arraycopy(row.getBitArray(), 0, bits, y * rowSize, rowSize);
}
/**
* Modifies this {@code BitMatrix} to represent the same but rotated the given degrees (multiple of 0, 90, 180, 270)
*/
public void rotate(int degrees) {
switch (degrees % 360) {
case 0:
return;
case 90:
rotate90();
return;
case 180:
rotate180();
return;
case 270:
rotate90();
rotate180();
return;
}
throw new IllegalArgumentException("degrees must be a multiple of 0, 90, 180, or 270");
}
/**
* Modifies this {@code BitMatrix} to represent the same but rotated 180 degrees
*/

View file

@ -94,6 +94,7 @@ public final class PDF417Reader implements Reader, MultipleBarcodeReader {
if (pdf417ResultMetadata != null) {
result.putMetadata(ResultMetadataType.PDF417_EXTRA_METADATA, pdf417ResultMetadata);
}
result.putMetadata(ResultMetadataType.ORIENTATION, detectorResult.getRotation());
result.putMetadata(ResultMetadataType.SYMBOLOGY_IDENTIFIER, "]L" + decoderResult.getSymbologyModifier());
results.add(result);
}

View file

@ -57,6 +57,7 @@ public final class Detector {
// ensure we don't miss it.
private static final int ROW_STEP = 5;
private static final int BARCODE_MIN_HEIGHT = 10;
private static final int[] ROTATIONS = {0, 180, 270, 90};
private Detector() {
}
@ -77,20 +78,31 @@ public final class Detector {
// different binarizers
//boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
BitMatrix bitMatrix = image.getBlackMatrix();
List<ResultPoint[]> barcodeCoordinates = detect(multiple, bitMatrix);
// Try 180, 270, 90 degree rotations, in that order
for (int rotate = 0; barcodeCoordinates.isEmpty() && rotate < 3; rotate++) {
bitMatrix = bitMatrix.clone();
if (rotate != 1) {
bitMatrix.rotate180();
} else {
bitMatrix.rotate90();
BitMatrix originalMatrix = image.getBlackMatrix();
for (int rotation : ROTATIONS) {
BitMatrix bitMatrix = applyRotation(originalMatrix, rotation);
List<ResultPoint[]> barcodeCoordinates = detect(multiple, bitMatrix);
if (!barcodeCoordinates.isEmpty()) {
return new PDF417DetectorResult(bitMatrix, barcodeCoordinates, rotation);
}
barcodeCoordinates = detect(multiple, bitMatrix);
}
return new PDF417DetectorResult(bitMatrix, barcodeCoordinates);
return new PDF417DetectorResult(originalMatrix, new ArrayList<>(), 0);
}
/**
* Applies a rotation to the supplied BitMatrix.
* @param matrix bit matrix to apply rotation to
* @param rotation the degrees of rotation to apply
* @return BitMatrix with applied rotation
*/
private static BitMatrix applyRotation(BitMatrix matrix, int rotation) {
if (rotation % 360 == 0) {
return matrix;
}
BitMatrix newMatrix = matrix.clone();
newMatrix.rotate(rotation);
return newMatrix;
}
/**

View file

@ -28,10 +28,16 @@ public final class PDF417DetectorResult {
private final BitMatrix bits;
private final List<ResultPoint[]> points;
private final int rotation;
public PDF417DetectorResult(BitMatrix bits, List<ResultPoint[]> points) {
public PDF417DetectorResult(BitMatrix bits, List<ResultPoint[]> points, int rotation) {
this.bits = bits;
this.points = points;
this.rotation = rotation;
}
public PDF417DetectorResult(BitMatrix bits, List<ResultPoint[]> points) {
this(bits, points, 0);
}
public BitMatrix getBits() {
@ -42,4 +48,8 @@ public final class PDF417DetectorResult {
return points;
}
public int getRotation() {
return rotation;
}
}