Skip to content
This repository was archived by the owner on Jun 16, 2023. It is now read-only.

Commit f72289f

Browse files
seancheungmikeduminyZhang Xuan
authored
feat: migrate android to google ML Kit (#3272)
BREAKING CHANGE: migrate to Google ML Kit * migrate to google ML Kit * refactor: migrate android to google ML Kit * refactor: migrate android general flavor to mlkit * docs: update docs for MLKit * docs: update installation doc Co-authored-by: Mike Duminy <michael-james.duminy@klarna.com> Co-authored-by: Zhang Xuan <xuan.zhang@xiaobao100.com>
1 parent 670b973 commit f72289f

25 files changed

+490
-532
lines changed

android/build.gradle

+5-5
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ repositories {
6666
}
6767

6868
dependencies {
69-
def googlePlayServicesVisionVersion = safeExtGet('googlePlayServicesVisionVersion', safeExtGet('googlePlayServicesVersion', '17.0.2'))
70-
7169
//noinspection GradleDynamicVersion
7270
implementation 'com.facebook.react:react-native:+' // From node_modules
7371
implementation "com.google.zxing:core:3.3.3"
7472
implementation "com.drewnoakes:metadata-extractor:2.11.0"
75-
generalImplementation "com.google.android.gms:play-services-vision:$googlePlayServicesVisionVersion"
7673
implementation "androidx.exifinterface:exifinterface:1.3.2"
7774
implementation "androidx.annotation:annotation:1.0.0"
7875
implementation "androidx.legacy:legacy-support-v4:1.0.0"
79-
mlkitImplementation "com.google.firebase:firebase-ml-vision:${safeExtGet('firebase-ml-vision', '19.0.3')}"
80-
mlkitImplementation "com.google.firebase:firebase-ml-vision-face-model:${safeExtGet('firebase-ml-vision-face-model', '17.0.2')}"
76+
implementation "com.google.android.gms:play-services-mlkit-text-recognition:16.3.0"
77+
generalImplementation "com.google.android.gms:play-services-mlkit-barcode-scanning:16.2.0"
78+
generalImplementation "com.google.android.gms:play-services-mlkit-face-detection:16.2.0"
79+
mlkitImplementation "com.google.mlkit:barcode-scanning:16.2.0"
80+
mlkitImplementation "com.google.mlkit:face-detection:16.1.2"
8181
}

android/src/general/java/org/reactnative/barcodedetector/BarcodeFormatUtils.java

+27-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.reactnative.barcodedetector;
22

33
import android.util.SparseArray;
4-
import com.google.android.gms.vision.barcode.Barcode;
4+
import com.google.mlkit.vision.barcode.Barcode;
55

66
import java.util.Collections;
77
import java.util.HashMap;
@@ -18,33 +18,32 @@ public class BarcodeFormatUtils {
1818
static {
1919
// Initialize integer to string map
2020
SparseArray<String> map = new SparseArray<>();
21-
map.put(Barcode.CODE_128, "CODE_128");
22-
map.put(Barcode.CODE_39, "CODE_39");
23-
map.put(Barcode.CODE_93, "CODE_93");
24-
map.put(Barcode.CODABAR, "CODABAR");
25-
map.put(Barcode.DATA_MATRIX, "DATA_MATRIX");
26-
map.put(Barcode.EAN_13, "EAN_13");
27-
map.put(Barcode.EAN_8, "EAN_8");
28-
map.put(Barcode.ITF, "ITF");
29-
map.put(Barcode.QR_CODE, "QR_CODE");
30-
map.put(Barcode.UPC_A, "UPC_A");
31-
map.put(Barcode.UPC_E, "UPC_E");
32-
map.put(Barcode.PDF417, "PDF417");
33-
map.put(Barcode.AZTEC, "AZTEC");
34-
map.put(Barcode.ALL_FORMATS, "ALL");
35-
map.put(Barcode.CALENDAR_EVENT, "CALENDAR_EVENT");
36-
map.put(Barcode.CONTACT_INFO, "CONTACT_INFO");
37-
map.put(Barcode.DRIVER_LICENSE, "DRIVER_LICENSE");
38-
map.put(Barcode.EMAIL, "EMAIL");
39-
map.put(Barcode.GEO, "GEO");
40-
map.put(Barcode.ISBN, "ISBN");
41-
map.put(Barcode.PHONE, "PHONE");
42-
map.put(Barcode.PRODUCT, "PRODUCT");
43-
map.put(Barcode.SMS, "SMS");
44-
map.put(Barcode.TEXT, "TEXT");
45-
map.put(Barcode.UPC_A, "UPC_A");
46-
map.put(Barcode.URL, "URL");
47-
map.put(Barcode.WIFI, "WIFI");
21+
map.put(Barcode.FORMAT_CODE_128, "CODE_128");
22+
map.put(Barcode.FORMAT_CODE_39, "CODE_39");
23+
map.put(Barcode.FORMAT_CODE_93, "CODE_93");
24+
map.put(Barcode.FORMAT_CODABAR, "CODABAR");
25+
map.put(Barcode.FORMAT_DATA_MATRIX, "DATA_MATRIX");
26+
map.put(Barcode.FORMAT_EAN_13, "EAN_13");
27+
map.put(Barcode.FORMAT_EAN_8, "EAN_8");
28+
map.put(Barcode.FORMAT_ITF, "ITF");
29+
map.put(Barcode.FORMAT_QR_CODE, "QR_CODE");
30+
map.put(Barcode.FORMAT_UPC_A, "UPC_A");
31+
map.put(Barcode.FORMAT_UPC_E, "UPC_E");
32+
map.put(Barcode.FORMAT_PDF417, "PDF417");
33+
map.put(Barcode.FORMAT_AZTEC, "AZTEC");
34+
map.put(Barcode.FORMAT_ALL_FORMATS, "ALL");
35+
map.put(Barcode.TYPE_CALENDAR_EVENT, "CALENDAR_EVENT");
36+
map.put(Barcode.TYPE_CONTACT_INFO, "CONTACT_INFO");
37+
map.put(Barcode.TYPE_DRIVER_LICENSE, "DRIVER_LICENSE");
38+
map.put(Barcode.TYPE_EMAIL, "EMAIL");
39+
map.put(Barcode.TYPE_GEO, "GEO");
40+
map.put(Barcode.TYPE_ISBN, "ISBN");
41+
map.put(Barcode.TYPE_PHONE, "PHONE");
42+
map.put(Barcode.TYPE_PRODUCT, "PRODUCT");
43+
map.put(Barcode.TYPE_SMS, "SMS");
44+
map.put(Barcode.TYPE_TEXT, "TEXT");
45+
map.put(Barcode.TYPE_URL, "URL");
46+
map.put(Barcode.TYPE_WIFI, "WIFI");
4847
map.put(-1, "None");
4948
FORMATS = map;
5049

android/src/general/java/org/reactnative/barcodedetector/RNBarcodeDetector.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
package org.reactnative.barcodedetector;
22

33
import android.content.Context;
4-
import android.util.SparseArray;
5-
import com.google.android.gms.vision.barcode.Barcode;
6-
import com.google.android.gms.vision.barcode.BarcodeDetector;
4+
import com.google.mlkit.vision.barcode.Barcode;
5+
import com.google.mlkit.vision.barcode.BarcodeScanner;
6+
import com.google.mlkit.vision.barcode.BarcodeScannerOptions;
7+
import com.google.mlkit.vision.barcode.BarcodeScanning;
8+
79
import org.reactnative.camera.utils.ImageDimensions;
810
import org.reactnative.frame.RNFrame;
911

12+
import java.util.List;
13+
1014
public class RNBarcodeDetector {
1115

1216
public static int NORMAL_MODE = 0;
1317
public static int ALTERNATE_MODE = 1;
1418
public static int INVERTED_MODE = 2;
15-
public static int ALL_FORMATS = Barcode.ALL_FORMATS;
19+
public static int ALL_FORMATS = Barcode.FORMAT_ALL_FORMATS;
1620

17-
private BarcodeDetector mBarcodeDetector = null;
21+
private BarcodeScanner mBarcodeDetector = null;
1822
private ImageDimensions mPreviousDimensions;
19-
private BarcodeDetector.Builder mBuilder;
2023

21-
private int mBarcodeType = Barcode.ALL_FORMATS;
24+
private int mBarcodeType = Barcode.FORMAT_ALL_FORMATS;
25+
private BarcodeScannerOptions.Builder mBuilder;
2226

2327
public RNBarcodeDetector(Context context) {
24-
mBuilder = new BarcodeDetector.Builder(context)
28+
mBuilder = new BarcodeScannerOptions.Builder()
2529
.setBarcodeFormats(mBarcodeType);
2630
}
2731

@@ -32,10 +36,10 @@ public boolean isOperational() {
3236
createBarcodeDetector();
3337
}
3438

35-
return mBarcodeDetector.isOperational();
39+
return true;
3640
}
3741

38-
public SparseArray<Barcode> detect(RNFrame frame) {
42+
public List<Barcode> detect(RNFrame frame) {
3943
// If the frame has different dimensions, create another barcode detector.
4044
// Otherwise we will most likely get nasty "inconsistent image dimensions" error from detector
4145
// and no barcode will be detected.
@@ -48,7 +52,7 @@ public SparseArray<Barcode> detect(RNFrame frame) {
4852
mPreviousDimensions = frame.getDimensions();
4953
}
5054

51-
return mBarcodeDetector.detect(frame.getFrame());
55+
return mBarcodeDetector.process(frame.getFrame()).getResult();
5256
}
5357

5458
public void setBarcodeType(int barcodeType) {
@@ -69,12 +73,12 @@ public void release() {
6973

7074
private void releaseBarcodeDetector() {
7175
if (mBarcodeDetector != null) {
72-
mBarcodeDetector.release();
76+
mBarcodeDetector.close();
7377
mBarcodeDetector = null;
7478
}
7579
}
7680

7781
private void createBarcodeDetector() {
78-
mBarcodeDetector = mBuilder.build();
82+
mBarcodeDetector = BarcodeScanning.getClient(mBuilder.build());
7983
}
8084
}

android/src/general/java/org/reactnative/camera/tasks/BarcodeDetectorAsyncTask.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package org.reactnative.camera.tasks;
22

33
import android.graphics.Rect;
4-
import android.util.SparseArray;
54

65
import com.facebook.react.bridge.Arguments;
76
import com.facebook.react.bridge.WritableArray;
87
import com.facebook.react.bridge.WritableMap;
9-
import com.google.android.gms.vision.barcode.Barcode;
8+
import com.google.mlkit.vision.barcode.Barcode;
109

1110
import org.reactnative.barcodedetector.BarcodeFormatUtils;
1211
import org.reactnative.camera.utils.ImageDimensions;
1312
import org.reactnative.frame.RNFrame;
1413
import org.reactnative.frame.RNFrameFactory;
1514
import org.reactnative.barcodedetector.RNBarcodeDetector;
1615

17-
public class BarcodeDetectorAsyncTask extends android.os.AsyncTask<Void, Void, SparseArray<Barcode>> {
16+
import java.util.List;
17+
18+
public class BarcodeDetectorAsyncTask extends android.os.AsyncTask<Void, Void, List<Barcode>> {
1819

1920
private byte[] mImageData;
2021
private int mWidth;
@@ -55,7 +56,7 @@ public BarcodeDetectorAsyncTask(
5556
}
5657

5758
@Override
58-
protected SparseArray<Barcode> doInBackground(Void... ignored) {
59+
protected List<Barcode> doInBackground(Void... ignored) {
5960
if (isCancelled() || mDelegate == null || mBarcodeDetector == null || !mBarcodeDetector.isOperational()) {
6061
return null;
6162
}
@@ -65,7 +66,7 @@ protected SparseArray<Barcode> doInBackground(Void... ignored) {
6566
}
6667

6768
@Override
68-
protected void onPostExecute(SparseArray<Barcode> barcodes) {
69+
protected void onPostExecute(List<Barcode> barcodes) {
6970
super.onPostExecute(barcodes);
7071

7172
if (barcodes == null) {
@@ -78,16 +79,16 @@ protected void onPostExecute(SparseArray<Barcode> barcodes) {
7879
}
7980
}
8081

81-
private WritableArray serializeEventData(SparseArray<Barcode> barcodes) {
82+
private WritableArray serializeEventData(List<Barcode> barcodes) {
8283
WritableArray barcodesList = Arguments.createArray();
8384

8485
for (int i = 0; i < barcodes.size(); i++) {
85-
Barcode barcode = barcodes.valueAt(i);
86+
Barcode barcode = barcodes.get(i);
8687
WritableMap serializedBarcode = Arguments.createMap();
8788

88-
serializedBarcode.putString("data", barcode.displayValue);
89-
serializedBarcode.putString("rawData", barcode.rawValue);
90-
serializedBarcode.putString("type", BarcodeFormatUtils.get(barcode.format));
89+
serializedBarcode.putString("data", barcode.getDisplayValue());
90+
serializedBarcode.putString("rawData", barcode.getRawValue());
91+
serializedBarcode.putString("type", BarcodeFormatUtils.get(barcode.getFormat()));
9192
serializedBarcode.putMap("bounds", processBounds(barcode.getBoundingBox()));
9293
barcodesList.pushMap(serializedBarcode);
9394
}

android/src/general/java/org/reactnative/camera/tasks/FaceDetectorAsyncTask.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package org.reactnative.camera.tasks;
22

3-
import android.util.SparseArray;
4-
53
import com.facebook.react.bridge.Arguments;
64
import com.facebook.react.bridge.WritableArray;
75
import com.facebook.react.bridge.WritableMap;
86
import com.google.android.cameraview.CameraView;
9-
import com.google.android.gms.vision.face.Face;
7+
import com.google.mlkit.vision.face.Face;
108

119
import org.reactnative.camera.utils.ImageDimensions;
1210
import org.reactnative.facedetector.FaceDetectorUtils;
1311
import org.reactnative.frame.RNFrame;
1412
import org.reactnative.frame.RNFrameFactory;
1513
import org.reactnative.facedetector.RNFaceDetector;
1614

17-
public class FaceDetectorAsyncTask extends android.os.AsyncTask<Void, Void, SparseArray<Face>> {
15+
import java.util.List;
16+
17+
public class FaceDetectorAsyncTask extends android.os.AsyncTask<Void, Void, List<Face>> {
1818
private byte[] mImageData;
1919
private int mWidth;
2020
private int mHeight;
@@ -55,7 +55,7 @@ public FaceDetectorAsyncTask(
5555
}
5656

5757
@Override
58-
protected SparseArray<Face> doInBackground(Void... ignored) {
58+
protected List<Face> doInBackground(Void... ignored) {
5959
if (isCancelled() || mDelegate == null || mFaceDetector == null || !mFaceDetector.isOperational()) {
6060
return null;
6161
}
@@ -65,7 +65,7 @@ protected SparseArray<Face> doInBackground(Void... ignored) {
6565
}
6666

6767
@Override
68-
protected void onPostExecute(SparseArray<Face> faces) {
68+
protected void onPostExecute(List<Face> faces) {
6969
super.onPostExecute(faces);
7070

7171
if (faces == null) {
@@ -78,11 +78,11 @@ protected void onPostExecute(SparseArray<Face> faces) {
7878
}
7979
}
8080

81-
private WritableArray serializeEventData(SparseArray<Face> faces) {
81+
private WritableArray serializeEventData(List<Face> faces) {
8282
WritableArray facesList = Arguments.createArray();
8383

8484
for(int i = 0; i < faces.size(); i++) {
85-
Face face = faces.valueAt(i);
85+
Face face = faces.get(i);
8686
WritableMap serializedFace = FaceDetectorUtils.serializeFace(face, mScaleX, mScaleY, mWidth, mHeight, mPaddingLeft, mPaddingTop);
8787
if (mImageDimensions.getFacing() == CameraView.FACING_FRONT) {
8888
serializedFace = FaceDetectorUtils.rotateFaceX(serializedFace, mImageDimensions.getWidth(), mScaleX);

0 commit comments

Comments
 (0)