Skip to content

Commit

Permalink
bugfix: adding DecodeHintType.POSSIBLE_FORMATS to limit the formats t…
Browse files Browse the repository at this point in the history
…o 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.
  • Loading branch information
hardbacknutter committed Nov 29, 2024
1 parent 10f6f86 commit 6301c56
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ Add a repository:

Gradle dependency string:

com.hardbacknutter.tinyzxingwrapper:TinyZXingWrapper:1.0.1:release@aar
com.hardbacknutter.tinyzxingwrapper:TinyZXingWrapper:1.1.0:release@aar
2 changes: 1 addition & 1 deletion TinyZXingWrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ kotlin {

ext {
// build version for the library
tinyZXingWrapperVersion = "1.0.1"
tinyZXingWrapperVersion = "1.1.0"
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,25 @@ public ScanOptions setUseCameraWithLensFacing(final int lensFacing) {

/**
* Set the desired barcode formats to try and decode.
* <p>
* <strong>IMPORTANT:</strong>
* The value is stored as an {@link ArrayList} with the {@code String} values
* of the {@link BarcodeFormat} enum names !
*
* @param list of {@link BarcodeFormat}s to try decoding
*
* @return this
*
* @see DecodeHintType#POSSIBLE_FORMATS
* @see BarcodeScanner.Builder#addHints(Bundle)
*/
@NonNull
public ScanOptions setBarcodeFormats(@NonNull final List<BarcodeFormat> list) {
if (!list.isEmpty()) {
intent.putStringArrayListExtra(DecodeHintType.POSSIBLE_FORMATS.name(),
list.stream()
// Note we store the NAME of the enum
// so we do not have to parcel the enum values
.map(Enum::name)
.collect(Collectors.toCollection(ArrayList::new)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.stream.Collectors;

import com.hardbacknutter.tinyzxingwrapper.ScanContract;
import com.hardbacknutter.tinyzxingwrapper.ScanOptions;

/**
* The main scanner code.
Expand Down Expand Up @@ -372,16 +373,15 @@ public Builder setDecoderFactory(@NonNull final DecoderFactory decoderFactory) {
* <p>
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
*
* @param barcodeFormats names of {@link BarcodeFormat}s to scan for
* @param barcodeFormats the {@link BarcodeFormat}s to scan for
*
* @return this
*/
@NonNull
public Builder setBarcodeFormats(@NonNull final List<BarcodeFormat> barcodeFormats) {
hints.put(DecodeHintType.POSSIBLE_FORMATS,
barcodeFormats.stream()
.map(Enum::name)
.collect(Collectors.toCollection(ArrayList::new)));
// Used directly, so we just store the enums.
// For intent use, see #addHints
hints.put(DecodeHintType.POSSIBLE_FORMATS, new ArrayList<>(barcodeFormats));
return this;
}

Expand Down Expand Up @@ -449,14 +449,21 @@ public Builder addHint(@NonNull final DecodeHintType hintType,
* Add a collection of hints.
* If the data type does not match the hint type, the hint is quietly ignored.
* <p>
* {@link DecodeHintType#NEED_RESULT_POINT_CALLBACK} is NOT supported
* <strong>IMPORTANT:</strong>
* {@link DecodeHintType#POSSIBLE_FORMATS} is expected to have as value
* an {@link ArrayList} with the {@code String} values of the {@link BarcodeFormat}
* enum names !
* <p>
* Note that {@link DecodeHintType#NEED_RESULT_POINT_CALLBACK} is NOT supported
* as it's used internally.
* <p>
* Only used if {@link #setDecoderFactory(DecoderFactory)} is <strong>NOT</strong> called.
*
* @param args a Bundle with hints; may contain other options which will be ignored.
*
* @return this
*
* @see ScanOptions#setBarcodeFormats(List)
*/
@NonNull
public Builder addHints(@Nullable final Bundle args) {
Expand All @@ -467,9 +474,34 @@ public Builder addHints(@Nullable final Bundle args) {
.forEach(hintType -> {
final String hintName = hintType.name();
if (args.containsKey(hintName)) {
// A switch 'hint': if present, store it with a boolean 'True'
// (this is faster than 6 string equality test)
// PURE_BARCODE
// TRY_HARDER
// ASSUME_CODE_39_CHECK_DIGIT
// ASSUME_GS1
// RETURN_CODABAR_START_END
// ALSO_INVERTED
if (hintType.getValueType().equals(Void.class)) {
this.hints.put(hintType, Boolean.TRUE);

} else if ("POSSIBLE_FORMATS".equals(hintName)) {
// the value is ArrayList of strings with the enum names.
// Convert them back to the actual enums.
final ArrayList<String> list = args.getStringArrayList(
hintName);
if (list != null) {
final List<BarcodeFormat> formats =
list.stream()
.map(BarcodeFormat::valueOf)
.collect(Collectors.toList());
this.hints.put(hintType, formats);
}
} else {
// OTHER
// CHARACTER_SET
// ALLOWED_LENGTHS
// ALLOWED_EAN_EXTENSIONS
final Object hintData = args.get(hintName);
if (hintType.getValueType().isInstance(hintData)) {
this.hints.put(hintType, hintData);
Expand Down

0 comments on commit 6301c56

Please sign in to comment.