// 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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 最近公司開發(fā)的一款型號客戶要求相機拍照分辨率最大支持到3600萬像素,相機模組硬件支持的最大分辨率為400萬,驅(qū)...
- 一、Camera 架構(gòu) 架構(gòu)簡圖: ? 根據(jù)架構(gòu)簡圖可以看到,實際上 Camera 的架構(gòu)與 An...
- 版權(quán)聲明:本文為博主原創(chuàng)文章,遵循CC 4.0 BY-SA[http://creativecommons.org/...
- 這篇文章主要介紹基于虹軟人臉識別SDK,適配Camera1、Camera2、CameraX。文章分下面幾點展開。 ...
- Android Camera2入門 Android Camera2入門系列1 - Camera2在textureV...