Skip to content

Commit 6301c56

Browse files
author
hardbacknutter
committed
bugfix: adding DecodeHintType.POSSIBLE_FORMATS to limit the formats to scan for to the Android intent was being ignored due to a mix of enum versus enum-names. This commit ensures proper encoding/decoding of the enums when passed via the Intent; also fixes using the BarcodeScanner API directly by using enums.
1 parent 10f6f86 commit 6301c56

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ Add a repository:
2424

2525
Gradle dependency string:
2626

27-
com.hardbacknutter.tinyzxingwrapper:TinyZXingWrapper:1.0.1:release@aar
27+
com.hardbacknutter.tinyzxingwrapper:TinyZXingWrapper:1.1.0:release@aar

TinyZXingWrapper/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ kotlin {
2626

2727
ext {
2828
// build version for the library
29-
tinyZXingWrapperVersion = "1.0.1"
29+
tinyZXingWrapperVersion = "1.1.0"
3030
}
3131

3232
android {

TinyZXingWrapper/src/main/java/com/hardbacknutter/tinyzxingwrapper/ScanOptions.java

+7
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,25 @@ public ScanOptions setUseCameraWithLensFacing(final int lensFacing) {
6868

6969
/**
7070
* Set the desired barcode formats to try and decode.
71+
* <p>
72+
* <strong>IMPORTANT:</strong>
73+
* The value is stored as an {@link ArrayList} with the {@code String} values
74+
* of the {@link BarcodeFormat} enum names !
7175
*
7276
* @param list of {@link BarcodeFormat}s to try decoding
7377
*
7478
* @return this
7579
*
7680
* @see DecodeHintType#POSSIBLE_FORMATS
81+
* @see BarcodeScanner.Builder#addHints(Bundle)
7782
*/
7883
@NonNull
7984
public ScanOptions setBarcodeFormats(@NonNull final List<BarcodeFormat> list) {
8085
if (!list.isEmpty()) {
8186
intent.putStringArrayListExtra(DecodeHintType.POSSIBLE_FORMATS.name(),
8287
list.stream()
88+
// Note we store the NAME of the enum
89+
// so we do not have to parcel the enum values
8390
.map(Enum::name)
8491
.collect(Collectors.toCollection(ArrayList::new)));
8592
}

TinyZXingWrapper/src/main/java/com/hardbacknutter/tinyzxingwrapper/scanner/BarcodeScanner.java

+38-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.stream.Collectors;
4242

4343
import com.hardbacknutter.tinyzxingwrapper.ScanContract;
44+
import com.hardbacknutter.tinyzxingwrapper.ScanOptions;
4445

4546
/**
4647
* The main scanner code.
@@ -372,16 +373,15 @@ public Builder setDecoderFactory(@NonNull final DecoderFactory decoderFactory) {
372373
* <p>
373374
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
374375
*
375-
* @param barcodeFormats names of {@link BarcodeFormat}s to scan for
376+
* @param barcodeFormats the {@link BarcodeFormat}s to scan for
376377
*
377378
* @return this
378379
*/
379380
@NonNull
380381
public Builder setBarcodeFormats(@NonNull final List<BarcodeFormat> barcodeFormats) {
381-
hints.put(DecodeHintType.POSSIBLE_FORMATS,
382-
barcodeFormats.stream()
383-
.map(Enum::name)
384-
.collect(Collectors.toCollection(ArrayList::new)));
382+
// Used directly, so we just store the enums.
383+
// For intent use, see #addHints
384+
hints.put(DecodeHintType.POSSIBLE_FORMATS, new ArrayList<>(barcodeFormats));
385385
return this;
386386
}
387387

@@ -449,14 +449,21 @@ public Builder addHint(@NonNull final DecodeHintType hintType,
449449
* Add a collection of hints.
450450
* If the data type does not match the hint type, the hint is quietly ignored.
451451
* <p>
452-
* {@link DecodeHintType#NEED_RESULT_POINT_CALLBACK} is NOT supported
452+
* <strong>IMPORTANT:</strong>
453+
* {@link DecodeHintType#POSSIBLE_FORMATS} is expected to have as value
454+
* an {@link ArrayList} with the {@code String} values of the {@link BarcodeFormat}
455+
* enum names !
456+
* <p>
457+
* Note that {@link DecodeHintType#NEED_RESULT_POINT_CALLBACK} is NOT supported
453458
* as it's used internally.
454459
* <p>
455460
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
456461
*
457462
* @param args a Bundle with hints; may contain other options which will be ignored.
458463
*
459464
* @return this
465+
*
466+
* @see ScanOptions#setBarcodeFormats(List)
460467
*/
461468
@NonNull
462469
public Builder addHints(@Nullable final Bundle args) {
@@ -467,9 +474,34 @@ public Builder addHints(@Nullable final Bundle args) {
467474
.forEach(hintType -> {
468475
final String hintName = hintType.name();
469476
if (args.containsKey(hintName)) {
477+
// A switch 'hint': if present, store it with a boolean 'True'
478+
// (this is faster than 6 string equality test)
479+
// PURE_BARCODE
480+
// TRY_HARDER
481+
// ASSUME_CODE_39_CHECK_DIGIT
482+
// ASSUME_GS1
483+
// RETURN_CODABAR_START_END
484+
// ALSO_INVERTED
470485
if (hintType.getValueType().equals(Void.class)) {
471486
this.hints.put(hintType, Boolean.TRUE);
487+
488+
} else if ("POSSIBLE_FORMATS".equals(hintName)) {
489+
// the value is ArrayList of strings with the enum names.
490+
// Convert them back to the actual enums.
491+
final ArrayList<String> list = args.getStringArrayList(
492+
hintName);
493+
if (list != null) {
494+
final List<BarcodeFormat> formats =
495+
list.stream()
496+
.map(BarcodeFormat::valueOf)
497+
.collect(Collectors.toList());
498+
this.hints.put(hintType, formats);
499+
}
472500
} else {
501+
// OTHER
502+
// CHARACTER_SET
503+
// ALLOWED_LENGTHS
504+
// ALLOWED_EAN_EXTENSIONS
473505
final Object hintData = args.get(hintName);
474506
if (hintType.getValueType().isInstance(hintData)) {
475507
this.hints.put(hintType, hintData);

0 commit comments

Comments
 (0)