android调用系统相机自动录像
时间: 2023-10-13 19:04:31 浏览: 110
Android 实现调用系统照相机拍照和录像的功能
在 Android 应用程序中调用系统相机自动录像,需要使用 Camera2 API 和 MediaRecorder 类。下面是一个简单的示例代码:
1. 在你的应用程序中声明一个 SurfaceView:
```
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在你的 Activity 中,定义 Camera2 API 和 MediaRecorder 类的成员变量:
```
private CameraManager cameraManager;
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession captureSession;
private SurfaceView surfaceView;
private MediaRecorder mediaRecorder;
```
3. 在 onCreate() 方法中初始化成员变量:
```
surfaceView = findViewById(R.id.surface_view);
mediaRecorder = new MediaRecorder();
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cameraId = cameraManager.getCameraIdList()[0];
```
4. 在 onStart() 方法中打开相机和预览:
```
@Override
protected void onStart() {
super.onStart();
openCamera();
}
private void openCamera() {
try {
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
cameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
camera.close();
cameraDevice = null;
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void startPreview() {
try {
Surface surface = surfaceView.getHolder().getSurface();
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
cameraDevice.createCaptureSession(Collections.singletonList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
captureSession = session;
try {
captureSession.setRepeatingRequest(builder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
captureSession = null;
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
```
5. 在一个按钮的点击事件中,开始录像:
```
Button btnRecord = findViewById(R.id.btn_record);
btnRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecording();
}
});
private void startRecording() {
try {
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setOutputFile(getOutputFile().getPath());
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.prepare();
Surface surface = mediaRecorder.getSurface();
CaptureRequest.Builder builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
builder.addTarget(surface);
List<Surface> surfaces = Arrays.asList(surface, surfaceView.getHolder().getSurface());
cameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
captureSession = session;
try {
captureSession.setRepeatingRequest(builder.build(), null, null);
mediaRecorder.start();
} catch (CameraAccessException | IllegalStateException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
captureSession = null;
}
}, null);
} catch (IOException | CameraAccessException e) {
e.printStackTrace();
}
}
```
6. 在 onStop() 方法中停止录像和相机预览:
```
@Override
protected void onStop() {
super.onStop();
stopRecording();
stopPreview();
}
private void stopRecording() {
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.reset();
}
}
private void stopPreview() {
if (captureSession != null) {
captureSession.close();
captureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
}
```
以上代码仅作为一个简单示例,实际应用中可能需要更多的配置和错误处理。请注意,自动录像可能会违反用户隐私,应慎重使用。
阅读全文