Skip to content

Commit 8d33a36

Browse files
committed
Merge branch 'mlkit'
2 parents cf11f64 + 61954bd commit 8d33a36

File tree

4 files changed

+42
-66
lines changed

4 files changed

+42
-66
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
buildscript {
2-
ext.kotlin_version = "1.5.20"
2+
ext.kotlin_version = "1.5.21"
33

44
repositories {
55
google()
@@ -12,7 +12,7 @@ buildscript {
1212

1313
dependencies {
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15-
classpath 'com.android.tools.build:gradle:4.2.2'
15+
classpath 'com.android.tools.build:gradle:7.0.1'
1616
classpath 'gradle.plugin.com.github.jlouns:gradle-cross-platform-exec-plugin:0.5.0'
1717
classpath "gradle.plugin.gmazzo:sqlite-plugin:0.2"
1818
}
@@ -40,7 +40,7 @@ allprojects {
4040

4141
buildToolsVersion='30.0.2'
4242
okhttpVersion='4.9.1'
43-
desugarVersion='1.1.1'
43+
desugarVersion='1.1.5'
4444

4545
sdkVersion += project.getProperties().get("versionSuffix", "")
4646
}

firebase-detector/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ dependencies {
4949
implementation project(':core')
5050
implementation project(':ui')
5151
implementation project(':utils')
52-
implementation 'com.google.firebase:firebase-ml-vision:18.0.1'
52+
implementation 'com.google.mlkit:barcode-scanning:17.0.0'
5353

5454
testImplementation 'junit:junit:4.13.2'
5555
androidTestImplementation 'androidx.test:runner:1.3.0'

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

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,25 @@
44

55
import com.google.android.gms.tasks.Task;
66
import com.google.android.gms.tasks.Tasks;
7-
import com.google.firebase.ml.vision.FirebaseVision;
8-
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode;
9-
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetector;
10-
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcodeDetectorOptions;
11-
import com.google.firebase.ml.vision.common.FirebaseVisionImage;
12-
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata;
7+
import com.google.mlkit.vision.barcode.Barcode;
8+
import com.google.mlkit.vision.barcode.BarcodeScanner;
9+
import com.google.mlkit.vision.barcode.BarcodeScannerOptions;
10+
import com.google.mlkit.vision.barcode.BarcodeScanning;
11+
import com.google.mlkit.vision.common.InputImage;
12+
1313

1414
import java.util.ArrayList;
1515
import java.util.List;
1616

1717
import io.snabble.sdk.BarcodeFormat;
18-
import io.snabble.sdk.ui.scanner.Barcode;
1918
import io.snabble.sdk.ui.scanner.BarcodeDetector;
2019
import io.snabble.sdk.ui.scanner.FalsePositiveFilter;
2120
import io.snabble.sdk.utils.Logger;
2221

2322
public class FirebaseBarcodeDetector implements BarcodeDetector {
2423
private byte[] cropBuffer = null;
2524
private FalsePositiveFilter falsePositiveFilter = new FalsePositiveFilter(3);
26-
private FirebaseVisionBarcodeDetector detector;
25+
private BarcodeScanner detector;
2726

2827
public FirebaseBarcodeDetector() {
2928

@@ -38,8 +37,8 @@ public void setup(List<BarcodeFormat> barcodeFormats) {
3837
formats.add(format);
3938

4039
// EAN13 with 0 prefixes are detected as UPC-A
41-
if (format == FirebaseVisionBarcode.FORMAT_EAN_13) {
42-
formats.add(FirebaseVisionBarcode.FORMAT_UPC_A);
40+
if (format == Barcode.FORMAT_EAN_13) {
41+
formats.add(Barcode.FORMAT_UPC_A);
4342
}
4443
}
4544

@@ -48,11 +47,11 @@ public void setup(List<BarcodeFormat> barcodeFormats) {
4847
ints[i] = formats.get(i);
4948
}
5049

51-
FirebaseVisionBarcodeDetectorOptions options = new FirebaseVisionBarcodeDetectorOptions.Builder()
50+
BarcodeScannerOptions options = new BarcodeScannerOptions.Builder()
5251
.setBarcodeFormats(ints[0], ints)
5352
.build();
5453

55-
detector = FirebaseVision.getInstance().getVisionBarcodeDetector(options);
54+
detector = BarcodeScanning.getClient(options);
5655
}
5756

5857
@Override
@@ -61,56 +60,32 @@ public void reset() {
6160
}
6261

6362
@Override
64-
public Barcode detect(byte[] data, int width, int height, int bitsPerPixel, Rect detectionRect, int displayOrientation) {
63+
public io.snabble.sdk.ui.scanner.Barcode detect(byte[] data, int width, int height, int bitsPerPixel, Rect detectionRect, int displayOrientation) {
6564
byte[] buf = crop(data, width, height, bitsPerPixel, detectionRect);
6665

67-
int firebaseRotation;
68-
switch (displayOrientation) {
69-
case 0:
70-
firebaseRotation = FirebaseVisionImageMetadata.ROTATION_0;
71-
break;
72-
case 90:
73-
firebaseRotation = FirebaseVisionImageMetadata.ROTATION_90;
74-
break;
75-
case 180:
76-
firebaseRotation = FirebaseVisionImageMetadata.ROTATION_180;
77-
break;
78-
case 270:
79-
firebaseRotation = FirebaseVisionImageMetadata.ROTATION_270;
80-
break;
81-
default:
82-
firebaseRotation = FirebaseVisionImageMetadata.ROTATION_0;
83-
}
84-
85-
FirebaseVisionImage image = FirebaseVisionImage.fromByteArray(buf, new FirebaseVisionImageMetadata.Builder()
86-
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
87-
.setRotation(firebaseRotation)
88-
.setWidth(detectionRect.width())
89-
.setHeight(detectionRect.height())
90-
.build());
91-
66+
InputImage inputImage = InputImage.fromByteArray(data, width, height, displayOrientation, InputImage.IMAGE_FORMAT_NV21);
9267

93-
Task<List<FirebaseVisionBarcode>> result = detector.detectInImage(image);
68+
Task<List<Barcode>> result = detector.process(inputImage);
9469

9570
try {
9671
Tasks.await(result);
97-
List<FirebaseVisionBarcode> firebaseBarcodes = result.getResult();
72+
List<Barcode> firebaseBarcodes = result.getResult();
9873
if(firebaseBarcodes != null && firebaseBarcodes.size() > 0) {
99-
FirebaseVisionBarcode firebaseVisionBarcode = firebaseBarcodes.get(0);
74+
Barcode firebaseVisionBarcode = firebaseBarcodes.get(0);
10075
String rawValue = firebaseVisionBarcode.getRawValue();
10176

10277
if (rawValue == null) {
10378
return null;
10479
}
10580

10681
// MLKit decodes all ITF lengths, but we only care about ITF14.
107-
if (firebaseVisionBarcode.getFormat() == FirebaseVisionBarcode.FORMAT_ITF) {
82+
if (firebaseVisionBarcode.getFormat() == Barcode.FORMAT_ITF) {
10883
if (rawValue.length() != 14) {
10984
return null;
11085
}
11186
}
11287

113-
if (firebaseVisionBarcode.getFormat() == FirebaseVisionBarcode.FORMAT_UPC_A) {
88+
if (firebaseVisionBarcode.getFormat() == Barcode.FORMAT_UPC_A) {
11489
if (rawValue.length() > 13) {
11590
return null;
11691
}
@@ -132,9 +107,9 @@ public Barcode detect(byte[] data, int width, int height, int bitsPerPixel, Rect
132107
return null;
133108
}
134109

135-
Barcode barcode = new Barcode(format, rawValue, System.currentTimeMillis());
110+
io.snabble.sdk.ui.scanner.Barcode barcode = new io.snabble.sdk.ui.scanner.Barcode(format, rawValue, System.currentTimeMillis());
136111

137-
Barcode filtered = falsePositiveFilter.filter(barcode);
112+
io.snabble.sdk.ui.scanner.Barcode filtered = falsePositiveFilter.filter(barcode);
138113
if (filtered != null) {
139114
Logger.d("Detected barcode: " + barcode.toString());
140115
return filtered;

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
11
package io.snabble.sdk.firebase;
22

3-
import com.google.firebase.ml.vision.barcode.FirebaseVisionBarcode;
3+
4+
import com.google.mlkit.vision.barcode.Barcode;
45

56
import io.snabble.sdk.BarcodeFormat;
67

78
class FirebaseBarcodeHelper {
89
public static int toFirebaseFormat(BarcodeFormat barcodeFormat) {
910
switch(barcodeFormat) {
1011
case EAN_8:
11-
return FirebaseVisionBarcode.FORMAT_EAN_8;
12+
return Barcode.FORMAT_EAN_8;
1213
case EAN_13:
13-
return FirebaseVisionBarcode.FORMAT_EAN_13;
14+
return Barcode.FORMAT_EAN_13;
1415
case CODE_128:
15-
return FirebaseVisionBarcode.FORMAT_CODE_128;
16+
return Barcode.FORMAT_CODE_128;
1617
case CODE_39:
17-
return FirebaseVisionBarcode.FORMAT_CODE_39;
18+
return Barcode.FORMAT_CODE_39;
1819
case ITF_14:
19-
return FirebaseVisionBarcode.FORMAT_ITF;
20+
return Barcode.FORMAT_ITF;
2021
case DATA_MATRIX:
21-
return FirebaseVisionBarcode.FORMAT_DATA_MATRIX;
22+
return Barcode.FORMAT_DATA_MATRIX;
2223
case QR_CODE:
23-
return FirebaseVisionBarcode.FORMAT_QR_CODE;
24+
return Barcode.FORMAT_QR_CODE;
2425
}
2526

26-
return FirebaseVisionBarcode.FORMAT_UNKNOWN;
27+
return Barcode.FORMAT_UNKNOWN;
2728
}
2829

2930
public static BarcodeFormat fromFirebaseFormat(int firebaseVisionBarcode) {
3031
switch (firebaseVisionBarcode) {
31-
case FirebaseVisionBarcode.FORMAT_EAN_8:
32+
case Barcode.FORMAT_EAN_8:
3233
return BarcodeFormat.EAN_8;
33-
case FirebaseVisionBarcode.FORMAT_EAN_13:
34-
case FirebaseVisionBarcode.FORMAT_UPC_A:
34+
case Barcode.FORMAT_EAN_13:
35+
case Barcode.FORMAT_UPC_A:
3536
return BarcodeFormat.EAN_13;
36-
case FirebaseVisionBarcode.FORMAT_CODE_128:
37+
case Barcode.FORMAT_CODE_128:
3738
return BarcodeFormat.CODE_128;
38-
case FirebaseVisionBarcode.FORMAT_CODE_39:
39+
case Barcode.FORMAT_CODE_39:
3940
return BarcodeFormat.CODE_39;
40-
case FirebaseVisionBarcode.FORMAT_ITF:
41+
case Barcode.FORMAT_ITF:
4142
return BarcodeFormat.ITF_14;
42-
case FirebaseVisionBarcode.FORMAT_DATA_MATRIX:
43+
case Barcode.FORMAT_DATA_MATRIX:
4344
return BarcodeFormat.DATA_MATRIX;
44-
case FirebaseVisionBarcode.FORMAT_QR_CODE:
45+
case Barcode.FORMAT_QR_CODE:
4546
return BarcodeFormat.QR_CODE;
4647
}
4748

0 commit comments

Comments
 (0)