Skip to content

Commit c13c9e8

Browse files
committed
FirebaseBarcodeDetector: Detect UPC-A as EAN13
1 parent c4bf33c commit c13c9e8

File tree

4 files changed

+49
-8
lines changed

4 files changed

+49
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.12.1]
5+
6+
### Fixed
7+
- Configured FirebaseBarcodeDetector to detect UPC-A as EAN13 to match the behaviour of ZXing
8+
49
## [0.12.0]
510

611
### Fixed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ allprojects {
2323
}
2424

2525
project.ext {
26-
sdkVersion='0.12.0'
26+
sdkVersion='0.12.1'
2727
versionCode=1
2828

2929
compileSdkVersion=28
3030
minSdkVersion=16
3131
targetSdkVersion=28
3232

3333
buildToolsVersion='28.0.3'
34-
okhttpVersion='3.12.0'
34+
okhttpVersion='3.12.1'
3535

3636
sdkVersion += project.getProperties().get("versionSuffix", "")
3737
}

firebase-detector/src/main/java/io/snabble/sdk/firebase/FirebaseBarcodeDetector.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
1212
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata;
1313

14+
import java.util.ArrayList;
1415
import java.util.List;
1516

1617
import io.snabble.sdk.BarcodeFormat;
@@ -30,13 +31,25 @@ public FirebaseBarcodeDetector() {
3031

3132
@Override
3233
public void setup(List<BarcodeFormat> barcodeFormats) {
33-
int[] formats = new int[barcodeFormats.size()];
34-
for (int i = 0; i<formats.length; i++) {
35-
formats[i] = FirebaseBarcodeHelper.toFirebaseFormat(barcodeFormats.get(i));
34+
List<Integer> formats = new ArrayList<>();
35+
36+
for (int i = 0; i<barcodeFormats.size(); i++) {
37+
int format = FirebaseBarcodeHelper.toFirebaseFormat(barcodeFormats.get(i));
38+
formats.add(format);
39+
40+
// EAN13 with 0 prefixes are detected as UPC-A
41+
if (format == FirebaseVisionBarcode.FORMAT_EAN_13) {
42+
formats.add(FirebaseVisionBarcode.FORMAT_UPC_A);
43+
}
44+
}
45+
46+
int[] ints = new int[formats.size()];
47+
for (int i=0; i<formats.size(); i++) {
48+
ints[i] = formats.get(i);
3649
}
3750

3851
FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder()
39-
.setBarcodeFormats(formats[0], formats)
52+
.setBarcodeFormats(ints[0], ints)
4053
.build();
4154

4255
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
@@ -84,16 +97,38 @@ public Barcode detect(byte[] data, int width, int height, int bitsPerPixel, Rect
8497
List<FirebaseVisionBarcode> firebaseBarcodes = result.getResult();
8598
if(firebaseBarcodes != null && firebaseBarcodes.size() > 0) {
8699
FirebaseVisionBarcode firebaseVisionBarcode = firebaseBarcodes.get(0);
100+
String rawValue = firebaseVisionBarcode.getRawValue();
101+
102+
if (rawValue == null) {
103+
return null;
104+
}
87105

88106
// MLKit decodes all ITF lengths, but we only care about ITF14.
89107
if (firebaseVisionBarcode.getFormat() == FirebaseVisionBarcode.FORMAT_ITF) {
90-
if (firebaseVisionBarcode.getRawValue().length() != 14) {
108+
if (rawValue.length() != 14) {
91109
return null;
92110
}
93111
}
94112

113+
if (firebaseVisionBarcode.getFormat() == FirebaseVisionBarcode.FORMAT_UPC_A) {
114+
if (rawValue.length() > 13) {
115+
return null;
116+
}
117+
118+
Logger.d("Detected UPC-A with length %d, converting to EAN13", rawValue.length());
119+
120+
int diff = 13 - rawValue.length();
121+
StringBuilder sb = new StringBuilder();
122+
for (int i=0; i<diff; i++) {
123+
sb.append('0');
124+
}
125+
sb.append(rawValue);
126+
127+
rawValue = sb.toString();
128+
}
129+
95130
Barcode barcode = new Barcode(FirebaseBarcodeHelper.fromFirebaseFormat(firebaseVisionBarcode.getFormat()),
96-
firebaseVisionBarcode.getRawValue(),
131+
rawValue,
97132
System.currentTimeMillis());
98133

99134
Barcode filtered = falsePositiveFilter.filter(barcode);

firebase-detector/src/main/java/io/snabble/sdk/firebase/FirebaseBarcodeHelper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static BarcodeFormat fromFirebaseFormat(int firebaseVisionBarcode) {
3131
case FirebaseVisionBarcode.FORMAT_EAN_8:
3232
return BarcodeFormat.EAN_8;
3333
case FirebaseVisionBarcode.FORMAT_EAN_13:
34+
case FirebaseVisionBarcode.FORMAT_UPC_A:
3435
return BarcodeFormat.EAN_13;
3536
case FirebaseVisionBarcode.FORMAT_CODE_128:
3637
return BarcodeFormat.CODE_128;

0 commit comments

Comments
 (0)