1111import com .google .firebase .ml .vision .common .FirebaseVisionImage ;
1212import com .google .firebase .ml .vision .common .FirebaseVisionImageMetadata ;
1313
14+ import java .util .ArrayList ;
1415import java .util .List ;
1516
1617import 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 );
0 commit comments