inverted barcode support (#1371)

* also decodes inverted barcode on hint

Co-authored-by: Daniel Dehnhard <daniel@dehnhard.it>
This commit is contained in:
dehnhard 2021-04-02 00:04:55 +02:00 committed by GitHub
parent 5fb2cf8e96
commit 3bf945e4cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 106 additions and 1 deletions

View file

@ -98,6 +98,12 @@ public enum DecodeHintType {
*/ */
ALLOWED_EAN_EXTENSIONS(int[].class), ALLOWED_EAN_EXTENSIONS(int[].class),
/**
* If true, also tries to decode as inverted image. All configured decoders are simply called a
* second time with an inverted image. Doesn't matter what it maps to; use {@link Boolean#TRUE}.
*/
ALSO_INVERTED(Void.class),
// End of enumeration values. // End of enumeration values.
; ;

View file

@ -175,6 +175,17 @@ public final class MultiFormatReader implements Reader {
// continue // continue
} }
} }
if (hints != null && hints.containsKey(DecodeHintType.ALSO_INVERTED)) {
// Calling all readers again with inverted image
image.getBlackMatrix().flip();
for (Reader reader : readers) {
try {
return reader.decode(image, hints);
} catch (ReaderException re) {
// continue
}
}
}
} }
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }

View file

@ -189,6 +189,16 @@ public final class BitMatrix implements Cloneable {
bits[offset] ^= 1 << (x & 0x1f); bits[offset] ^= 1 << (x & 0x1f);
} }
/**
* <p>Flips every bit in the matrix.</p>
*/
public void flip() {
int max = bits.length;
for (int i = 0; i < max; i++) {
bits[i] = ~bits[i];
}
}
/** /**
* Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding * Exclusive-or (XOR): Flip the bit in this {@code BitMatrix} if the corresponding
* mask bit is set. * mask bit is set.

View file

@ -62,6 +62,7 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
private final Reader barcodeReader; private final Reader barcodeReader;
private final BarcodeFormat expectedFormat; private final BarcodeFormat expectedFormat;
private final List<TestResult> testResults; private final List<TestResult> testResults;
private final EnumMap<DecodeHintType,Object> hints = new EnumMap<>(DecodeHintType.class);
public static Path buildTestBase(String testBasePathSuffix) { public static Path buildTestBase(String testBasePathSuffix) {
// A little workaround to prevent aggravation in my IDE // A little workaround to prevent aggravation in my IDE
@ -92,6 +93,10 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
addTest(mustPassCount, tryHarderCount, 0, 0, rotation); addTest(mustPassCount, tryHarderCount, 0, 0, rotation);
} }
protected void addHint(DecodeHintType hint) {
hints.put(hint, Boolean.TRUE);
}
/** /**
* Adds a new test for the current directory of images. * Adds a new test for the current directory of images.
* *
@ -252,7 +257,7 @@ public abstract class AbstractBlackBoxTestCase extends Assert {
String suffix = String.format(" (%srotation: %d)", tryHarder ? "try harder, " : "", (int) rotation); String suffix = String.format(" (%srotation: %d)", tryHarder ? "try harder, " : "", (int) rotation);
Map<DecodeHintType,Object> hints = new EnumMap<>(DecodeHintType.class); Map<DecodeHintType,Object> hints = this.hints.clone();
if (tryHarder) { if (tryHarder) {
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
} }

View file

@ -0,0 +1,38 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.inverted;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.DecodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.common.AbstractBlackBoxTestCase;
/**
* Inverted barcodes
*/
public final class InvertedDataMatrixBlackBoxTestCase extends AbstractBlackBoxTestCase {
public InvertedDataMatrixBlackBoxTestCase() {
super("src/test/resources/blackbox/inverted", new MultiFormatReader(), BarcodeFormat.DATA_MATRIX);
addHint(DecodeHintType.ALSO_INVERTED);
addTest(1, 1, 0.0f);
addTest(1, 1, 90.0f);
addTest(1, 1, 180.0f);
addTest(1, 1, 270.0f);
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.inverted;
import com.google.zxing.common.AbstractNegativeBlackBoxTestCase;
/**
* Without hint inverted barcodes should not be found.
*/
public final class NoHintInvertedDataMatrixBlackBoxTestCase extends AbstractNegativeBlackBoxTestCase {
public NoHintInvertedDataMatrixBlackBoxTestCase() {
super("src/test/resources/blackbox/inverted");
addTest(0, 0.0f);
addTest(0, 90.0f);
addTest(0, 180.0f);
addTest(0, 270.0f);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -0,0 +1 @@
0123456789