本篇介绍
在Android上,Camera采集有Camera1,Camera2,CameraX三种接口,其中Camera1 在Android 21上已经被废弃了。不过由于这套接口可以在所有设备上兼容,还是最常用的接口。本篇介绍下该接口的使用。
Camera1 使用步骤
- 声明权限
Camera是敏感操作,需要声明权限,在新版本中也需要动态声明。
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
代码中请求权限:
private void requestPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
return;
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, CAMERA_REQUEST_CODE);
}
- 打开Camera
比如现在是使用前置摄像头:
Camera.CameraInfo info = new Camera.CameraInfo();
int numCameras = Camera.getNumberOfCameras();
for (int i =0; i < numCameras; i++) {
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCamera = Camera.open(i);
break;
}
}
if (mCamera == null) {
mCamera = Camera.open();
}
- 设置参数
设置旋转角度:
int degree = 0;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
degree = info.orientation % 360 - 180;
} else {
degree = (info.orientation + 360) % 360;
}
mCamera.setDisplayOrientation(degree);
- 设置预览格式
Camera.Parameters params = mCamera.getParameters();
List<Integer> supportedFormat = params.getSupportedPreviewFormats();
if (supportedFormat.contains(format)) {
params.setPreviewFormat(format);
}
mCamera.setParameters(params);
- 设置预览画面尺寸:
int width_param = 0;
int height_param = 0;
double last_weight = Double.MAX_VALUE;
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
for (Camera.Size size : sizes) {
if (size.height == height && size.width == width) {
width_param = size.width;
height_param = size.height;
break;
}
double weight = Math.pow(Math.abs(size.height - height),2) + Math.pow(Math.abs(size.width - width),2);
if (weight < last_weight) {
width_param = size.width;
height_param = size.height;
}
}
params.setPreviewSize(width_param, height_param);
mCamera.setParameters(params);
- 设置帧率
int minFps_param = 0;
int maxFps_param = 0;
double last_weight = Double.MAX_VALUE;
Camera.Parameters params = mCamera.getParameters();
List<int[]> ranges = params.getSupportedPreviewFpsRange();
for (int[] range : ranges) {
if (range[0] <= minFps && range[1] >= maxFps) {
minFps_param = range[0];
maxFps_param = range[1];
break;
}
double weight = Math.pow(Math.abs(range[0] - minFps),2) + Math.pow(Math.abs(range[1] - maxFps),2);
if (weight < last_weight) {
minFps_param = range[0];
maxFps_param = range[1];
}
}
params.setPreviewFpsRange(minFps_param, maxFps_param);
mCamera.setParameters(params);
- 设置渲染,本篇使用SurfaceView渲染。
mSurfaceView = findViewById(R.id.surface_view);
mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(@NonNull SurfaceHolder holder) {
}
@Override
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
}
});
mSurfaceView.setVisibility(View.VISIBLE);
try {
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
- 开启预览:
mCamera.startPreview();
9: 销毁资源
mCamera.stopPreview();
try {
mCamera.setPreviewDisplay(null);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.release();
网友评论