在Android上,Camera采集有Camera1,Camera2,CameraX三种接口,其中Camera1 在Android 21上已经被废弃了。不过由于这套接口可以在所有设备上兼容,还是最常用的接口。本篇介绍下该接口的使用。
<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.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);
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();