// Import necessary packages
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
// Define class that implements SurfaceHolder.Callback
public class CustomCamera implements SurfaceHolder.Callback {
// Declare variables
private Camera mCamera;
private SurfaceHolder mHolder;
// Constructor
public CustomCamera(SurfaceView surfaceView) {
// Get SurfaceHolder from SurfaceView
mHolder = surfaceView.getHolder();
// Set this class as the callback for SurfaceHolder events
mHolder.addCallback(this);
}
// Implement SurfaceHolder.Callback methods
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Open camera and set preview display
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Set camera parameters for preview size and aspect ratio
Camera.Parameters parameters = mCamera.getParameters();
Camera.Size previewSize = getOptimalPreviewSize(parameters.getSupportedPreviewSizes(), 4, 3);
parameters.setPreviewSize(previewSize.width, previewSize.height);
mCamera.setParameters(parameters);
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// Release camera resources
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
// Define method to get optimal preview size based on aspect ratio
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int width, int height) {
final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double) width / height;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
// Try to find a size that matches the target aspect ratio and has the closest height
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// If no size matches the target aspect ratio, choose the one with the closest aspect ratio
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Camera.Size size : sizes) {
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) < minDiff) {
optimalSize = size;
minDiff = Math.abs(ratio - targetRatio);
}
}
}
return optimalSize;
}
}
// To get a 4:3 aspect ratio, we need to find the optimal preview size with a width-to-height ratio of 4:3.
// We can do this by iterating through the list of supported preview sizes and finding the one with the closest height to the target height (which is 3/4 of the target width).
// If there is no size with a 4:3 aspect ratio, we choose the one with the closest aspect ratio.
// We then set the camera parameters to use this preview size.
Android camera1 支持4:3
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 最近公司开发的一款型号客户要求相机拍照分辨率最大支持到3600万像素,相机模组硬件支持的最大分辨率为400万,驱...
- 一、Camera 架构 架构简图: 根据架构简图可以看到,实际上 Camera 的架构与 An...
- 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA[http://creativecommons.org/...
- 这篇文章主要介绍基于虹软人脸识别SDK,适配Camera1、Camera2、CameraX。文章分下面几点展开。 ...
- Android Camera2入门 Android Camera2入门系列1 - Camera2在textureV...