41
41
import java .util .stream .Collectors ;
42
42
43
43
import com .hardbacknutter .tinyzxingwrapper .ScanContract ;
44
+ import com .hardbacknutter .tinyzxingwrapper .ScanOptions ;
44
45
45
46
/**
46
47
* The main scanner code.
@@ -372,16 +373,15 @@ public Builder setDecoderFactory(@NonNull final DecoderFactory decoderFactory) {
372
373
* <p>
373
374
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
374
375
*
375
- * @param barcodeFormats names of {@link BarcodeFormat}s to scan for
376
+ * @param barcodeFormats the {@link BarcodeFormat}s to scan for
376
377
*
377
378
* @return this
378
379
*/
379
380
@ NonNull
380
381
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 ));
385
385
return this ;
386
386
}
387
387
@@ -449,14 +449,21 @@ public Builder addHint(@NonNull final DecodeHintType hintType,
449
449
* Add a collection of hints.
450
450
* If the data type does not match the hint type, the hint is quietly ignored.
451
451
* <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
453
458
* as it's used internally.
454
459
* <p>
455
460
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
456
461
*
457
462
* @param args a Bundle with hints; may contain other options which will be ignored.
458
463
*
459
464
* @return this
465
+ *
466
+ * @see ScanOptions#setBarcodeFormats(List)
460
467
*/
461
468
@ NonNull
462
469
public Builder addHints (@ Nullable final Bundle args ) {
@@ -467,9 +474,34 @@ public Builder addHints(@Nullable final Bundle args) {
467
474
.forEach (hintType -> {
468
475
final String hintName = hintType .name ();
469
476
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
470
485
if (hintType .getValueType ().equals (Void .class )) {
471
486
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
+ }
472
500
} else {
501
+ // OTHER
502
+ // CHARACTER_SET
503
+ // ALLOWED_LENGTHS
504
+ // ALLOWED_EAN_EXTENSIONS
473
505
final Object hintData = args .get (hintName );
474
506
if (hintType .getValueType ().isInstance (hintData )) {
475
507
this .hints .put (hintType , hintData );
0 commit comments