capacitor-camera-module 0.0.34 → 0.0.35
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.
|
@@ -578,117 +578,61 @@ public class CameraModulePlugin extends Plugin {
|
|
|
578
578
|
return;
|
|
579
579
|
}
|
|
580
580
|
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
581
|
+
try {
|
|
582
|
+
File photoFile = File.createTempFile(
|
|
583
|
+
"photo_",
|
|
584
|
+
".jpg",
|
|
585
|
+
getContext().getCacheDir()
|
|
586
|
+
);
|
|
587
|
+
|
|
588
|
+
ImageCapture.OutputFileOptions options =
|
|
589
|
+
new ImageCapture.OutputFileOptions.Builder(photoFile).build();
|
|
590
|
+
|
|
591
|
+
imageCapture.takePicture(
|
|
592
|
+
options,
|
|
593
|
+
ContextCompat.getMainExecutor(getContext()),
|
|
594
|
+
new ImageCapture.OnImageSavedCallback() {
|
|
595
|
+
|
|
596
|
+
@Override
|
|
597
|
+
public void onImageSaved(
|
|
598
|
+
@NonNull ImageCapture.OutputFileResults output
|
|
599
|
+
) {
|
|
600
|
+
try {
|
|
601
|
+
Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
|
|
602
|
+
Bitmap resized = resizeBitmap(bitmap, 1024);
|
|
603
|
+
|
|
604
|
+
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
605
|
+
resized.compress(Bitmap.CompressFormat.JPEG, 80, out);
|
|
606
|
+
|
|
607
|
+
String base64 = Base64.encodeToString(
|
|
608
|
+
out.toByteArray(),
|
|
609
|
+
Base64.NO_WRAP
|
|
610
|
+
);
|
|
611
|
+
|
|
612
|
+
bitmap.recycle();
|
|
613
|
+
resized.recycle();
|
|
614
|
+
photoFile.delete();
|
|
615
|
+
|
|
616
|
+
JSObject ret = new JSObject();
|
|
617
|
+
ret.put("base64", base64);
|
|
618
|
+
ret.put("mimeType", "image/jpeg");
|
|
619
|
+
call.resolve(ret);
|
|
620
|
+
|
|
621
|
+
} catch (Exception e) {
|
|
622
|
+
call.reject("Error processing image", e);
|
|
595
623
|
}
|
|
596
|
-
int rotation = image.getImageInfo().getRotationDegrees();
|
|
597
|
-
bitmap = rotateBitmap(bitmap, rotation);
|
|
598
|
-
image.close();
|
|
599
|
-
|
|
600
|
-
Bitmap resized = resizeBitmap(bitmap, 1024);
|
|
601
|
-
|
|
602
|
-
ByteArrayOutputStream outputStream =
|
|
603
|
-
new ByteArrayOutputStream();
|
|
604
|
-
resized.compress(
|
|
605
|
-
Bitmap.CompressFormat.JPEG,
|
|
606
|
-
80,
|
|
607
|
-
outputStream
|
|
608
|
-
);
|
|
609
|
-
|
|
610
|
-
byte[] bytes = outputStream.toByteArray();
|
|
611
|
-
String base64 =
|
|
612
|
-
Base64.encodeToString(bytes, Base64.NO_WRAP);
|
|
613
|
-
|
|
614
|
-
bitmap.recycle();
|
|
615
|
-
resized.recycle();
|
|
616
|
-
|
|
617
|
-
JSObject ret = new JSObject();
|
|
618
|
-
ret.put("base64", base64);
|
|
619
|
-
ret.put("mimeType", "image/jpeg");
|
|
620
|
-
call.resolve(ret);
|
|
621
|
-
|
|
622
|
-
} catch (Exception e) {
|
|
623
|
-
call.reject("Error capturing image", e);
|
|
624
624
|
}
|
|
625
|
-
}
|
|
626
625
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
call.reject("Capture failed", exception);
|
|
626
|
+
@Override
|
|
627
|
+
public void onError(@NonNull ImageCaptureException exception) {
|
|
628
|
+
call.reject("Capture failed", exception);
|
|
629
|
+
}
|
|
632
630
|
}
|
|
633
|
-
}
|
|
634
|
-
);
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
private Bitmap imageProxyToBitmap(ImageProxy image) {
|
|
638
|
-
Image mediaImage = image.getImage();
|
|
639
|
-
if (mediaImage == null) return null;
|
|
640
|
-
|
|
641
|
-
ByteBuffer yBuffer = image.getPlanes()[0].getBuffer();
|
|
642
|
-
ByteBuffer uBuffer = image.getPlanes()[1].getBuffer();
|
|
643
|
-
ByteBuffer vBuffer = image.getPlanes()[2].getBuffer();
|
|
644
|
-
|
|
645
|
-
int ySize = yBuffer.remaining();
|
|
646
|
-
int uSize = uBuffer.remaining();
|
|
647
|
-
int vSize = vBuffer.remaining();
|
|
648
|
-
|
|
649
|
-
byte[] nv21 = new byte[ySize + uSize + vSize];
|
|
650
|
-
|
|
651
|
-
yBuffer.get(nv21, 0, ySize);
|
|
652
|
-
vBuffer.get(nv21, ySize, vSize);
|
|
653
|
-
uBuffer.get(nv21, ySize + vSize, uSize);
|
|
654
|
-
|
|
655
|
-
YuvImage yuvImage =
|
|
656
|
-
new YuvImage(
|
|
657
|
-
nv21,
|
|
658
|
-
ImageFormat.NV21,
|
|
659
|
-
image.getWidth(),
|
|
660
|
-
image.getHeight(),
|
|
661
|
-
null
|
|
662
631
|
);
|
|
663
632
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
100,
|
|
668
|
-
out
|
|
669
|
-
);
|
|
670
|
-
|
|
671
|
-
byte[] jpegBytes = out.toByteArray();
|
|
672
|
-
return BitmapFactory.decodeByteArray(jpegBytes, 0, jpegBytes.length);
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
private Bitmap rotateBitmap(Bitmap bitmap, int rotationDegrees) {
|
|
678
|
-
if (rotationDegrees == 0) return bitmap;
|
|
679
|
-
|
|
680
|
-
Matrix matrix = new Matrix();
|
|
681
|
-
matrix.postRotate(rotationDegrees);
|
|
682
|
-
|
|
683
|
-
return Bitmap.createBitmap(
|
|
684
|
-
bitmap,
|
|
685
|
-
0,
|
|
686
|
-
0,
|
|
687
|
-
bitmap.getWidth(),
|
|
688
|
-
bitmap.getHeight(),
|
|
689
|
-
matrix,
|
|
690
|
-
true
|
|
691
|
-
);
|
|
633
|
+
} catch (Exception e) {
|
|
634
|
+
call.reject("Error creating file", e);
|
|
635
|
+
}
|
|
692
636
|
}
|
|
693
637
|
|
|
694
638
|
|
|
@@ -876,5 +820,28 @@ public class CameraModulePlugin extends Plugin {
|
|
|
876
820
|
}
|
|
877
821
|
}
|
|
878
822
|
|
|
823
|
+
private void restartNormalPreview() {
|
|
824
|
+
if (cameraProvider == null) return;
|
|
825
|
+
|
|
826
|
+
cameraProvider.unbindAll();
|
|
827
|
+
|
|
828
|
+
Preview preview = new Preview.Builder().build();
|
|
829
|
+
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
830
|
+
|
|
831
|
+
imageCapture = new ImageCapture.Builder()
|
|
832
|
+
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
|
|
833
|
+
.build();
|
|
834
|
+
|
|
835
|
+
CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;
|
|
836
|
+
|
|
837
|
+
cameraProvider.bindToLifecycle(
|
|
838
|
+
getActivity(),
|
|
839
|
+
cameraSelector,
|
|
840
|
+
preview,
|
|
841
|
+
imageCapture
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
|
|
879
846
|
|
|
880
847
|
}
|