capacitor-camera-module 0.0.36 → 0.0.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -73,6 +73,9 @@ import java.util.concurrent.Executors;
73
73
  import java.util.concurrent.ExecutorService;
74
74
  import java.io.File;
75
75
 
76
+ import androidx.exifinterface.media.ExifInterface;
77
+ import java.io.FileInputStream;
78
+
76
79
  @CapacitorPlugin(
77
80
  name = "CameraModule",
78
81
  permissions = {
@@ -599,6 +602,7 @@ public class CameraModulePlugin extends Plugin {
599
602
  ) {
600
603
  try {
601
604
  Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
605
+ bitmap = rotateBitmapIfRequired(bitmap, photoFile);
602
606
  Bitmap resized = resizeBitmap(bitmap, 1024);
603
607
 
604
608
  ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -639,71 +643,69 @@ public class CameraModulePlugin extends Plugin {
639
643
  @PluginMethod
640
644
  public void startBarcodeScan(PluginCall call) {
641
645
 
642
- if (previewView == null) {
643
- call.reject("Preview not started");
644
- return;
645
- }
646
-
647
- if (getPermissionState("camera") != PermissionState.GRANTED) {
648
- call.reject("Camera permission not granted");
649
- return;
650
- }
651
-
652
- if (cameraProvider == null) {
653
- call.reject("Camera not initialized");
654
- return;
655
- }
646
+ getActivity().runOnUiThread(() -> {
656
647
 
657
- if (isScanning) {
658
- call.reject("Already scanning");
659
- return;
660
- }
648
+ if (previewView == null) {
649
+ call.reject("Preview not started");
650
+ return;
651
+ }
661
652
 
662
- barcodeScanner = BarcodeScanning.getClient();
653
+ if (getPermissionState("camera") != PermissionState.GRANTED) {
654
+ call.reject("Camera permission not granted");
655
+ return;
656
+ }
663
657
 
664
- call.setKeepAlive(true);
658
+ if (cameraProvider == null) {
659
+ call.reject("Camera not initialized");
660
+ return;
661
+ }
665
662
 
666
- scanCall = call;
667
- isScanning = true;
663
+ if (isScanning) {
664
+ call.reject("Already scanning");
665
+ return;
666
+ }
668
667
 
669
- imageAnalysis =
670
- new ImageAnalysis.Builder()
671
- .setBackpressureStrategy(
672
- ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
673
- )
674
- .build();
668
+ barcodeScanner = BarcodeScanning.getClient();
675
669
 
676
- imageAnalysis.setAnalyzer(
677
- barcodeExecutor,
678
- this::processBarcode
679
- );
670
+ call.setKeepAlive(true);
671
+ scanCall = call;
672
+ isScanning = true;
680
673
 
681
- cameraProvider.unbindAll();
674
+ imageAnalysis =
675
+ new ImageAnalysis.Builder()
676
+ .setBackpressureStrategy(
677
+ ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST
678
+ )
679
+ .build();
682
680
 
683
- Preview preview = new Preview.Builder().build();
684
- preview.setSurfaceProvider(previewView.getSurfaceProvider());
681
+ imageAnalysis.setAnalyzer(
682
+ barcodeExecutor,
683
+ this::processBarcode
684
+ );
685
685
 
686
- imageCapture =
687
- new ImageCapture.Builder()
688
- .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
689
- .build();
686
+ cameraProvider.unbindAll();
690
687
 
691
- if (cameraSelector == null) {
692
- cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
693
- }
688
+ Preview preview = new Preview.Builder().build();
689
+ preview.setSurfaceProvider(previewView.getSurfaceProvider());
694
690
 
695
- camera = cameraProvider.bindToLifecycle(
696
- getActivity(),
697
- cameraSelector,
698
- preview,
699
- imageCapture,
700
- imageAnalysis
701
- );
691
+ imageCapture =
692
+ new ImageCapture.Builder()
693
+ .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
694
+ .build();
702
695
 
703
- call.resolve();
696
+ camera = cameraProvider.bindToLifecycle(
697
+ getActivity(),
698
+ cameraSelector,
699
+ preview,
700
+ imageCapture,
701
+ imageAnalysis
702
+ );
704
703
 
704
+ call.resolve();
705
+ });
705
706
  }
706
707
 
708
+
707
709
  private void processBarcode(ImageProxy imageProxy) {
708
710
 
709
711
  if (!isScanning) {
@@ -842,6 +844,46 @@ public class CameraModulePlugin extends Plugin {
842
844
  );
843
845
  }
844
846
 
847
+ private Bitmap rotateBitmapIfRequired(Bitmap bitmap, File photoFile) throws Exception {
848
+
849
+ ExifInterface exif = new ExifInterface(photoFile.getAbsolutePath());
850
+
851
+ int orientation = exif.getAttributeInt(
852
+ ExifInterface.TAG_ORIENTATION,
853
+ ExifInterface.ORIENTATION_NORMAL
854
+ );
855
+
856
+ Matrix matrix = new Matrix();
857
+
858
+ switch (orientation) {
859
+ case ExifInterface.ORIENTATION_ROTATE_90:
860
+ matrix.postRotate(90);
861
+ break;
862
+ case ExifInterface.ORIENTATION_ROTATE_180:
863
+ matrix.postRotate(180);
864
+ break;
865
+ case ExifInterface.ORIENTATION_ROTATE_270:
866
+ matrix.postRotate(270);
867
+ break;
868
+ default:
869
+ return bitmap;
870
+ }
871
+
872
+ Bitmap rotated = Bitmap.createBitmap(
873
+ bitmap,
874
+ 0,
875
+ 0,
876
+ bitmap.getWidth(),
877
+ bitmap.getHeight(),
878
+ matrix,
879
+ true
880
+ );
881
+
882
+ bitmap.recycle();
883
+ return rotated;
884
+ }
885
+
886
+
845
887
 
846
888
 
847
889
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "capacitor-camera-module",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "description": "Plugin to request permissiones view camera take phots",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",