19
19
import android .view .SurfaceHolder ;
20
20
import android .view .SurfaceView ;
21
21
import android .view .View ;
22
+ import android .view .ViewGroup ;
22
23
import android .view .Window ;
23
24
import android .view .WindowInsetsController ;
24
25
import android .view .WindowManager ;
@@ -651,34 +652,41 @@ private void openCameraPreview(SurfaceHolder holder) {
651
652
if (camera != null ) {
652
653
releaseCamera ();
653
654
}
655
+
654
656
camera = android .hardware .Camera .open ();
655
657
android .hardware .Camera .Parameters parameters = camera .getParameters ();
656
- DisplayMetrics metrics = getResources ().getDisplayMetrics ();
657
- int targetSize = Math .min (metrics .widthPixels , metrics .heightPixels );
658
- android .hardware .Camera .Size optimalSize = getOptimalPreviewSize (parameters .getSupportedPreviewSizes (), targetSize , targetSize );
658
+
659
+ android .hardware .Camera .Size optimalSize = getOptimalPreviewSize (parameters .getSupportedPreviewSizes (), 4.0 / 4.0 );
659
660
if (optimalSize != null ) {
660
661
parameters .setPreviewSize (optimalSize .width , optimalSize .height );
661
662
}
663
+
662
664
camera .setParameters (parameters );
663
- android .hardware .Camera .CameraInfo camInfo = new android .hardware .Camera .CameraInfo ();
664
- android .hardware .Camera .getCameraInfo (android .hardware .Camera .CameraInfo .CAMERA_FACING_BACK , camInfo );
665
+
665
666
setCameraDisplayOrientation (android .hardware .Camera .CameraInfo .CAMERA_FACING_BACK , camera );
667
+
666
668
camera .setPreviewDisplay (holder );
667
669
camera .startPreview ();
668
- android .hardware .Camera .Size previewSize = parameters .getPreviewSize ();
669
- float ratio = (float ) previewSize .width / previewSize .height ;
670
- int surfaceWidth = binding .cameraPreview .getWidth ();
671
- int surfaceHeight = binding .cameraPreview .getHeight ();
672
670
673
- if (surfaceWidth / (float ) surfaceHeight > ratio ) {
674
- surfaceWidth = (int ) (surfaceHeight * ratio );
671
+ float targetRatio = 4f / 3f ;
672
+ int parentWidth = binding .cameraPreview .getWidth ();
673
+ int parentHeight = binding .cameraPreview .getHeight ();
674
+
675
+ int newWidth , newHeight ;
676
+
677
+ if ((float ) parentWidth / parentHeight > targetRatio ) {
678
+ newHeight = parentHeight ;
679
+ newWidth = (int ) (parentHeight * targetRatio );
675
680
} else {
676
- surfaceHeight = (int ) (surfaceWidth / ratio );
681
+ newWidth = parentWidth ;
682
+ newHeight = (int ) (parentWidth / targetRatio );
677
683
}
678
- binding .cameraPreview .getLayoutParams ().width = surfaceWidth ;
679
- binding .cameraPreview .getLayoutParams ().height = surfaceHeight ;
680
- binding .cameraPreview .requestLayout ();
681
684
685
+ ViewGroup .LayoutParams params = binding .cameraPreview .getLayoutParams ();
686
+ params .width = newWidth ;
687
+ params .height = newHeight ;
688
+ binding .cameraPreview .setLayoutParams (params );
689
+ binding .cameraPreview .requestLayout ();
682
690
683
691
} catch (Exception e ) {
684
692
Toast .makeText (this , "Camera error: " + e .getMessage (), Toast .LENGTH_LONG ).show ();
@@ -702,56 +710,42 @@ private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera c
702
710
int rotation = displayRotation ;
703
711
int degrees = 0 ;
704
712
switch (rotation ) {
705
- case Surface .ROTATION_0 :
706
- degrees = 0 ;
707
- break ;
708
- case Surface .ROTATION_90 :
709
- degrees = 90 ;
710
- break ;
711
- case Surface .ROTATION_180 :
712
- degrees = 180 ;
713
- break ;
714
- case Surface .ROTATION_270 :
715
- degrees = 270 ;
716
- break ;
713
+ case Surface .ROTATION_0 : degrees = 0 ; break ;
714
+ case Surface .ROTATION_90 : degrees = 90 ; break ;
715
+ case Surface .ROTATION_180 : degrees = 180 ; break ;
716
+ case Surface .ROTATION_270 : degrees = 270 ; break ;
717
717
}
718
+
718
719
int result ;
719
720
if (info .facing == android .hardware .Camera .CameraInfo .CAMERA_FACING_FRONT ) {
720
721
result = (info .orientation + degrees ) % 360 ;
721
722
result = (360 - result ) % 360 ;
722
723
} else {
723
724
result = (info .orientation - degrees + 360 ) % 360 ;
724
725
}
726
+
725
727
camera .setDisplayOrientation (result );
726
728
}
727
729
728
- private android .hardware .Camera .Size getOptimalPreviewSize (List <android .hardware .Camera .Size > sizes , int targetWidth , int targetHeight ) {
730
+ private android .hardware .Camera .Size getOptimalPreviewSize (List <android .hardware .Camera .Size > sizes , double targetRatio ) {
729
731
final double ASPECT_TOLERANCE = 0.1 ;
730
- double targetRatio = (double ) targetWidth / targetHeight ;
731
- if (sizes == null ) return null ;
732
732
android .hardware .Camera .Size optimalSize = null ;
733
733
double minDiff = Double .MAX_VALUE ;
734
- int targetDim = Math .min (targetWidth , targetHeight );
735
734
736
735
for (android .hardware .Camera .Size size : sizes ) {
737
736
double ratio = (double ) size .width / size .height ;
738
737
if (Math .abs (ratio - targetRatio ) < ASPECT_TOLERANCE ) {
739
- if (Math .abs (size .height - targetDim ) < minDiff ) {
738
+ if (Math .abs (size .height - 720 ) < minDiff ) {
740
739
optimalSize = size ;
741
- minDiff = Math .abs (size .height - targetDim );
740
+ minDiff = Math .abs (size .height - 720 );
742
741
}
743
742
}
744
743
}
745
744
746
- if (optimalSize == null ) {
747
- minDiff = Double .MAX_VALUE ;
748
- for (android .hardware .Camera .Size size : sizes ) {
749
- if (Math .abs (size .height - targetDim ) < minDiff ) {
750
- optimalSize = size ;
751
- minDiff = Math .abs (size .height - targetDim );
752
- }
753
- }
745
+ if (optimalSize == null && !sizes .isEmpty ()) {
746
+ optimalSize = sizes .get (0 );
754
747
}
748
+
755
749
return optimalSize ;
756
750
}
757
751
0 commit comments