Skip to content

Commit 472e159

Browse files
committed
scanner size config, scanner line animation
1 parent 1d636e2 commit 472e159

File tree

10 files changed

+104
-121
lines changed

10 files changed

+104
-121
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import { MlkitBarcodeView, BARCODE } from "react-native-mlkit-barcode";
1414
// ...
1515

1616
<MlkitBarcodeView
17-
enableQrScanner={this.state.enableQrScanner}
17+
style={{width:800, height:800}}
18+
enableScanner={this.state.enableScanner}
1819
barcodeFormat={BARCODE.FORMAT_ALL_FORMATS}
1920
onSuccess={(data) => {
20-
console.log("App BarCode On Success :", data);
21-
this.setState({enableQrScanner:false})
21+
console.log("BarCode On Success :", data);
22+
this.setState({enableScanner:false})
2223
}}
2324
/>
2425
```

android/build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ dependencies {
138138
// implementation 'com.google.android.gms:play-services-mlkit-barcode-scanning:18.1.0'
139139
implementation 'com.google.mlkit:barcode-scanning:17.0.3'
140140

141-
implementation 'com.facebook.fresco:fresco:2.5.0'
142-
implementation 'com.facebook.fresco:animated-gif:2.6.0'
143-
144141
// From node_modules
145142
}
146143

android/src/main/java/com/reactnativemlkitbarcode/MlKitBarcodeDecoder.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected void startCamera() {
9595
cameraProvider = processCameraProvider.get();
9696

9797
cameraProvider.unbindAll();
98-
cameraProvider.bindToLifecycle(frag.getActivity(), cameraSelector, preview, imageAnalysis);
98+
cameraProvider.bindToLifecycle(frag, cameraSelector, preview, imageAnalysis);
9999
preview.setSurfaceProvider(this.previewView.getSurfaceProvider());
100100

101101
} catch (ExecutionException e) {
@@ -119,7 +119,7 @@ protected void stopAll(){
119119
scanner.close();
120120
if(cameraProvider!=null){
121121
cameraProvider.unbindAll();
122-
// cameraProvider.shutdown();
122+
cameraProvider.shutdown();
123123
}
124124
}
125125

@@ -136,17 +136,6 @@ private Size getRes() {
136136
return new Size(displayMetrics.widthPixels,displayMetrics.heightPixels);
137137
}
138138

139-
private int aspectRatio() {
140-
DisplayMetrics displayMetrics = new DisplayMetrics();
141-
this.previewView.getDisplay().getRealMetrics(displayMetrics);
142-
Log.e(TAG, "PreviewSize :: W:"+displayMetrics.widthPixels+", H:"+displayMetrics.heightPixels);
143-
double previewRatio = (double) Math.max(displayMetrics.widthPixels, displayMetrics.heightPixels) / Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels);
144-
if (Math.abs(previewRatio - RATIO_4_3_VALUE) <= Math.abs(previewRatio - RATIO_16_9_VALUE)) {
145-
return AspectRatio.RATIO_4_3;
146-
}
147-
return AspectRatio.RATIO_16_9;
148-
}
149-
150139
private void createBarCodeScanner(){
151140
Log.e(TAG, "createBarCodeScanner: BarCode Format :"+barcodeFormat);
152141
BarcodeScannerOptions options =
Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.reactnativemlkitbarcode;
22

3+
import android.animation.ObjectAnimator;
4+
import android.animation.ValueAnimator;
35
import android.os.Bundle;
46
import android.util.Log;
5-
import android.view.Gravity;
67
import android.view.LayoutInflater;
78
import android.view.View;
89
import android.view.ViewGroup;
@@ -13,11 +14,6 @@
1314
import androidx.camera.view.PreviewView;
1415
import androidx.fragment.app.Fragment;
1516

16-
import com.facebook.drawee.backends.pipeline.Fresco;
17-
import com.facebook.drawee.interfaces.DraweeController;
18-
import com.facebook.drawee.view.SimpleDraweeView;
19-
import com.facebook.imagepipeline.request.ImageRequest;
20-
import com.facebook.imagepipeline.request.ImageRequestBuilder;
2117
import com.facebook.react.bridge.ReactContext;
2218
import com.facebook.react.uimanager.ThemedReactContext;
2319

@@ -28,10 +24,11 @@ public class MlKitBarcodeFragment extends Fragment {
2824
private ReactContext reactContext;
2925
private int barcodeFormat;
3026

31-
private PreviewView previewView = null;
3227
private int WIDTH = 0;
3328
private int HEIGHT = 0;
34-
private SimpleDraweeView simpleDraweeView = null;
29+
30+
private PreviewView previewView = null;
31+
private View viewLine = null;
3532

3633
public MlKitBarcodeFragment(ThemedReactContext reactContext, int barcodeFormat) {
3734
this.reactContext = reactContext;
@@ -48,19 +45,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
4845
super.onCreateView(inflater, container, savedInstanceState);
4946
mlKitBarcodeDecoder = new MlKitBarcodeDecoder(this, reactContext);
5047

51-
// RelativeLayout relativeLayout = new RelativeLayout(MlKitBarcodeFragment.this.getContext());
52-
// relativeLayout.setGravity(Gravity.CENTER);
53-
// relativeLayout.addView(mlKitBarcodeDecoder.createScannerView());
54-
55-
5648
View view = inflater.inflate(R.layout.fragview,container,false);
5749
previewView = view.findViewById(R.id.previewView);
58-
59-
simpleDraweeView = view.findViewById(R.id.imgViewGif);
60-
61-
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithResourceId(R.drawable.scanner2).build();
62-
DraweeController controller = Fresco.newDraweeControllerBuilder().setImageRequest(imageRequest).setAutoPlayAnimations(true).build();
63-
simpleDraweeView.setController(controller);
50+
viewLine = view.findViewById(R.id.lineView);
6451
mlKitBarcodeDecoder.createScannerView(previewView);
6552
return view;
6653
}
@@ -71,38 +58,23 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
7158
Log.e(TAG, "onViewCreated: W:"+WIDTH+", H:"+HEIGHT);
7259
mlKitBarcodeDecoder.setBarCodeFormat(barcodeFormat);
7360
previewView.setLayoutParams(new RelativeLayout.LayoutParams(WIDTH,HEIGHT));
74-
simpleDraweeView.setLayoutParams(new RelativeLayout.LayoutParams(WIDTH,HEIGHT));
75-
mlKitBarcodeDecoder.stopAll();
61+
viewLine.setLayoutParams(new RelativeLayout.LayoutParams(WIDTH,8));
7662
mlKitBarcodeDecoder.startCamera();
77-
78-
}
79-
80-
@Override
81-
public void onDestroy() {
82-
super.onDestroy();
83-
Log.e(TAG, "onDestroy: ....");
84-
// mlKitBarcodeDecoder.stopAll();
85-
}
86-
87-
@Override
88-
public void onDetach() {
89-
super.onDetach();
90-
Log.e(TAG, "onDetach: ....");
91-
// mlKitBarcodeDecoder.stopAll();
63+
playScanningAnimation();
9264
}
9365

9466
public void updatePreviewSize(int width, int height){
9567
Log.e(TAG, "updatePreviewSize: ");
9668
WIDTH = width;
9769
HEIGHT = height;
70+
}
9871

99-
// if(previewView != null){
100-
// if(mlKitBarcodeDecoder != null){
101-
// mlKitBarcodeDecoder.stopAll();
102-
// mlKitBarcodeDecoder.startCamera();
103-
// }
104-
// previewView.setLayoutParams(new RelativeLayout.LayoutParams(width,height));
105-
// }
72+
private void playScanningAnimation(){
73+
ObjectAnimator animation = ObjectAnimator.ofFloat(viewLine, "translationY", HEIGHT);
74+
animation.setDuration(2000);
75+
animation.setRepeatCount(-1);
76+
animation.setRepeatMode(ValueAnimator.REVERSE);
77+
animation.start();
10678
}
10779

10880
}

android/src/main/java/com/reactnativemlkitbarcode/MlkitBarcodeViewManager.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,34 @@ public FrameLayout createViewInstance(ThemedReactContext reactContext) {
5050
this.reactContext = reactContext;
5151
FrameLayout frameLayout = new FrameLayout(reactContext);
5252
Fresco.initialize(reactContext);
53+
Log.e(TAG, "createViewInstance: ID :"+frameLayout.getId());
5354
return frameLayout;
5455
}
5556

57+
58+
59+
@Override
60+
public void onDropViewInstance(@NonNull FrameLayout view) {
61+
removeFragment();
62+
Log.e(TAG, "onDropViewInstance: "+view.getId());
63+
super.onDropViewInstance(view);
64+
}
65+
5666
@Override
5767
public void receiveCommand(@NonNull FrameLayout root, String commandId, @Nullable ReadableArray args) {
58-
super.receiveCommand(root, commandId, args);
68+
Log.e(TAG, "receiveCommand: "+commandId);
5969
int reactNativeViewId = args.getInt(0);
6070
switch (commandId) {
6171
case COMMAND_CREATE:{
6272
createFragment(root, reactNativeViewId);
6373
break;
6474
}
65-
case COMMAND_DESTROY:
66-
removeFragment();
67-
break;
75+
// case COMMAND_DESTROY:
76+
// removeFragment();
77+
// break;
6878
default: {}
6979
}
80+
super.receiveCommand(root, commandId, args);
7081
}
7182

7283
@ReactProp(name="barcodeFormat")
@@ -76,21 +87,25 @@ public void setBarcodeFormat( View view,int barcodeFormat){
7687

7788
@ReactProp(name = "height")
7889
public void setHeight(View view, int height) {
79-
Log.e(TAG, "setHeight: "+height);
90+
Log.e(TAG, "setHeight: "+height+" View :"+view.getId());
8091
HEIGHT = height;
81-
if(scannerFrag != null)
82-
scannerFrag.updatePreviewSize(WIDTH,HEIGHT);
92+
// if(scannerFrag != null)
93+
// scannerFrag.updatePreviewSize(WIDTH,HEIGHT);
8394
}
8495

8596
@ReactProp(name = "width")
8697
public void setWidth(View view, int width) {
8798
Log.e(TAG, "setWidth: "+width);
8899
WIDTH = width;
89-
if(scannerFrag != null)
90-
scannerFrag.updatePreviewSize(WIDTH,HEIGHT);
100+
// if(scannerFrag != null)
101+
// scannerFrag.updatePreviewSize(WIDTH,HEIGHT);
91102
}
92103

93-
104+
@Override
105+
public void receiveCommand(@NonNull FrameLayout root, int commandId, @Nullable ReadableArray args) {
106+
super.receiveCommand(root, commandId, args);
107+
Log.e(TAG, "receiveCommand @ :: "+commandId);
108+
}
94109

95110
public void createFragment(FrameLayout root, int reactNativeViewId) {
96111
Log.e(TAG, "createFragment:...");
@@ -106,12 +121,14 @@ public void createFragment(FrameLayout root, int reactNativeViewId) {
106121
}
107122

108123
public void removeFragment(){
124+
Log.e(TAG, "removeFragment: ...");
109125
try {
110126
if(scannerFrag != null && scannerFrag.getScannerView() != null)
111127
scannerFrag.getScannerView().stopAll();
112128
Choreographer.getInstance().removeFrameCallback(this.frameCallback);
113129
FragmentActivity activity = (FragmentActivity) this.reactContext.getCurrentActivity();
114-
activity.getSupportFragmentManager().beginTransaction().remove(this.scannerFrag).commit();
130+
if(this.scannerFrag != null)
131+
activity.getSupportFragmentManager().beginTransaction().remove(this.scannerFrag).commit();
115132
}
116133
catch (Exception e){
117134
Log.e(TAG, "removeFragment: Error :"+e.toString());
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<selector xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<item>
5+
<shape android:shape="rectangle">
6+
<solid android:color="#FF0000"></solid>
7+
<size android:height="4dp" />
8+
</shape>
9+
</item>
10+
11+
</selector>
-690 KB
Binary file not shown.
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22

33
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
4-
xmlns:fresco="http://schemas.android.com/apk/res-auto"
54
android:orientation="vertical"
65
android:layout_width="wrap_content"
76
android:layout_height="wrap_content"
@@ -10,22 +9,17 @@
109

1110
<androidx.camera.view.PreviewView
1211
android:id="@+id/previewView"
13-
android:layout_width="200dp"
14-
android:layout_height="200dp"
12+
android:layout_width="400dp"
13+
android:layout_height="400dp"
1514
android:scaleType="fitCenter"
1615
/>
1716

18-
19-
<com.facebook.drawee.view.SimpleDraweeView
20-
android:id="@+id/imgViewGif"
21-
android:layout_width="100dp"
22-
android:layout_height="100dp"
23-
android:scaleType="fitCenter"
24-
/>
25-
26-
17+
<View
18+
android:id="@+id/lineView"
19+
android:layout_width="400dp"
20+
android:layout_height="4dp"
21+
android:background="@drawable/line"/>
2722

2823
</RelativeLayout>
2924

3025

31-
<!-- fresco:backgroundImage="@color/colorRed"-->

example/src/App.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default class App extends React.Component {
99
constructor(props) {
1010
super(props)
1111
this.state = {
12-
enableQrScanner: true,
12+
enableScanner: true,
1313
barcodeFormat:BARCODE.FORMAT_ALL_FORMATS,
1414
}
1515
}
@@ -22,15 +22,15 @@ export default class App extends React.Component {
2222
<SafeAreaView style={{flex:1}}>
2323
<MlkitBarcodeView
2424
style={{width:800, height:800}}
25-
enableQrScanner={this.state.enableQrScanner}
26-
barcodeFormat={BARCODE.FORMAT_ALL_FORMATS}
25+
enableScanner={this.state.enableScanner}
26+
barcodeFormat={BARCODE.FORMAT_QR_CODE}
2727
onSuccess={(data) => {
28-
console.log("App BarCode On Success :", data);
29-
this.setState({enableQrScanner:false})
28+
console.log("BarCode On Success :", data);
29+
this.setState({enableScanner:false})
3030
}}
3131
/>
3232

33-
<TouchableOpacity style={{width:100, height:50,}} onPress={()=>this.setState({enableQrScanner:!this.state.enableQrScanner})}>
33+
<TouchableOpacity style={{width:100, height:50,}} onPress={()=>this.setState({enableScanner:!this.state.enableScanner})}>
3434
<Text style={{textAlign:'center',color:'red', fontSize:20, backgroundColor:'black'}}>{"Toggle SCanner"}</Text>
3535
</TouchableOpacity>
3636
</SafeAreaView>

0 commit comments

Comments
 (0)