11package com .reactnativemlkitbarcode ;
22
33import android .annotation .SuppressLint ;
4- import android .graphics .Color ;
54import android .media .Image ;
65import android .os .Handler ;
76import android .os .Looper ;
87import android .util .DisplayMetrics ;
98import android .util .Log ;
10- import android .util .Size ;
11- import android .view .Gravity ;
12- import android .view .ViewGroup ;
13- import android .widget .LinearLayout ;
149
1510import androidx .annotation .NonNull ;
16- import androidx .appcompat .app .AppCompatActivity ;
1711import androidx .camera .core .AspectRatio ;
18- import androidx .camera .core .CameraFilter ;
1912import androidx .camera .core .CameraSelector ;
2013import androidx .camera .core .ImageAnalysis ;
2114import androidx .camera .core .ImageProxy ;
2215import androidx .camera .core .Preview ;
2316import androidx .camera .lifecycle .ProcessCameraProvider ;
2417import androidx .camera .view .PreviewView ;
2518import androidx .fragment .app .Fragment ;
26- import androidx .fragment .app .FragmentActivity ;
27- import androidx .lifecycle .LifecycleOwner ;
2819
29- import com .facebook .react .bridge .Callback ;
3020import com .facebook .react .bridge .ReactContext ;
3121import com .facebook .react .bridge .WritableMap ;
3222import com .facebook .react .bridge .WritableNativeMap ;
3323import com .facebook .react .modules .core .DeviceEventManagerModule ;
34- import com .facebook .react .uimanager .events .RCTEventEmitter ;
3524import com .google .android .gms .tasks .OnCanceledListener ;
3625import com .google .android .gms .tasks .OnCompleteListener ;
3726import com .google .android .gms .tasks .OnFailureListener ;
@@ -53,8 +42,9 @@ public class MlKitBarcodeDecoder implements ImageAnalysis.Analyzer{
5342
5443 private final double RATIO_4_3_VALUE = 4.0 / 3.0 ;
5544 private final double RATIO_16_9_VALUE = 16.0 / 9.0 ;
45+
5646 private Fragment frag ;
57- private PreviewView previewView ;
47+ private PreviewView previewView = null ;
5848 private BarcodeScanner scanner ;
5949 private ProcessCameraProvider cameraProvider ;
6050 private ImageAnalysis imageAnalysis ;
@@ -63,29 +53,23 @@ public class MlKitBarcodeDecoder implements ImageAnalysis.Analyzer{
6353
6454 private ReactContext reactContext ;
6555
56+ private int barcodeFormat = 0 ; // All
57+
6658 public MlKitBarcodeDecoder (Fragment frag , ReactContext reactContext ){
6759 this .frag = frag ;
6860 this .reactContext = reactContext ;
6961 }
7062
71- public PreviewView createScannerView (int width , int height ){
72- previewView = new PreviewView (frag .getActivity ());
73- LinearLayout .LayoutParams layoutParams = new LinearLayout .LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
74- layoutParams .weight = width ;
75- layoutParams .height = height ;
76- layoutParams .gravity = Gravity .CENTER ;
77- previewView .setLayoutParams (layoutParams );
78- previewView .setBackgroundColor (Color .RED );
79- return previewView ;
63+ public PreviewView createScannerView (){
64+ this .previewView = new PreviewView (frag .getActivity ());
65+ return this .previewView ;
8066 }
8167
82- public void updateSize (int width , int height ){
83- // Log.e(TAG, "updateSize: W:"+width+" H:"+height);
84- previewView .getLayoutParams ().width = width ;
85- previewView .getLayoutParams ().height = height ;
68+ public void setBarCodeFormat (int barcodeFormat ){
69+ Log .e (TAG , "setBarCodeFormat: " +barcodeFormat );
70+ this .barcodeFormat = barcodeFormat ;
8671 }
8772
88-
8973 /////////////////
9074 protected void startCamera () {
9175 createBarCodeScanner ();
@@ -109,8 +93,8 @@ protected void startCamera() {
10993 cameraProvider = processCameraProvider .get ();
11094
11195 cameraProvider .unbindAll ();
112- cameraProvider .bindToLifecycle (frag .getActivity (), cameraSelector , preview ,imageAnalysis );
113- preview .setSurfaceProvider (previewView .getSurfaceProvider ());
96+ cameraProvider .bindToLifecycle (frag .getActivity (), cameraSelector , preview , imageAnalysis );
97+ preview .setSurfaceProvider (this . previewView .getSurfaceProvider ());
11498
11599 } catch (ExecutionException e ) {
116100 Log .e (TAG , "startCamera: ExecutionException :" +e .toString ());
@@ -134,20 +118,18 @@ protected void stopAll(){
134118 cameraProvider .unbindAll ();
135119 cameraProvider .shutdown ();
136120 }
137-
138- // Log.e(TAG, "stopAll:....");
139121 }
140122
141123 private Preview getPreview () {
142124 return new Preview .Builder ()
143125 .setTargetAspectRatio (aspectRatio ())
144- .setTargetRotation (previewView .getDisplay ().getRotation ())
126+ .setTargetRotation (this . previewView .getDisplay ().getRotation ())
145127 .build ();
146128 }
147129
148130 private int aspectRatio () {
149131 DisplayMetrics displayMetrics = new DisplayMetrics ();
150- previewView .getDisplay ().getRealMetrics (displayMetrics );
132+ this . previewView .getDisplay ().getRealMetrics (displayMetrics );
151133 double previewRatio = (double ) Math .max (displayMetrics .widthPixels , displayMetrics .heightPixels ) / Math .min (displayMetrics .widthPixels , displayMetrics .heightPixels );
152134 if (Math .abs (previewRatio - RATIO_4_3_VALUE ) <= Math .abs (previewRatio - RATIO_16_9_VALUE )) {
153135 return AspectRatio .RATIO_4_3 ;
@@ -156,10 +138,10 @@ private int aspectRatio() {
156138 }
157139
158140 private void createBarCodeScanner (){
141+ Log .e (TAG , "createBarCodeScanner: BarCode Format :" +barcodeFormat );
159142 BarcodeScannerOptions options =
160143 new BarcodeScannerOptions .Builder ()
161- .setBarcodeFormats (
162- Barcode .FORMAT_ALL_FORMATS )
144+ .setBarcodeFormats (barcodeFormat )
163145 .build ();
164146 scanner = BarcodeScanning .getClient (options );
165147 }
@@ -168,16 +150,13 @@ private void createBarCodeScanner(){
168150 @ SuppressLint ("UnsafeOptInUsageError" )
169151 @ Override
170152 public void analyze (@ NonNull ImageProxy imageProxy ) {
171- // Log.e(TAG, "analyze: ImageProxy:"+imageProxy.getImageInfo().getTimestamp());
172153 Image mediaImage = imageProxy .getImage ();
173154 if (mediaImage != null ) {
174155 InputImage image = InputImage .fromMediaImage (mediaImage , imageProxy .getImageInfo ().getRotationDegrees ());
175156 scanner .process (image ).addOnSuccessListener (new OnSuccessListener <List <Barcode >>() {
176157 @ Override
177158 public void onSuccess (List <Barcode > barcodes ) {
178159 if (barcodes .size () > 0 ){
179- // Log.e(TAG, "analyze: ImageProxy: H: "+imageProxy.getImage().getHeight()+" W :"+imageProxy.getImage().getWidth());
180- // Log.e(TAG, "onSuccess: BarCodes :"+barcodes.size());
181160 String rawValue = barcodes .get (barcodes .size ()-1 ).getRawValue ();
182161 Log .e (TAG , "onSuccess: RawValue :" +rawValue );
183162 sendEvent (rawValue );
@@ -196,7 +175,9 @@ public void onFailure(@NonNull Exception e) {
196175 @ Override
197176 public void onCanceled () {
198177 Log .e (TAG , "onCanceled: ...." );
199-
178+ if (mediaImage !=null )
179+ mediaImage .close ();
180+ imageProxy .close ();
200181 }
201182 })
202183 .addOnCompleteListener (new OnCompleteListener <List <Barcode >>() {
0 commit comments