在Android平台上,开发一款能够拍照、录像以及控制闪光灯的应用是一项常见的需求。`camera2` API是Android系统提供的一种高级相机接口,它为开发者提供了更精细的控制权,以实现复杂的相机功能。在这个名为"TestCamera"的Android Studio工程中,我们将深入探讨如何使用`camera2` API来实现这些功能。
我们需要在AndroidManifest.xml文件中添加必要的权限,以允许应用访问相机和录制视频:
```xml
```
接下来,我们创建一个`CameraActivity`,在这个活动中初始化相机,并设置预览界面。这通常涉及到设置SurfaceView或TextureView作为相机的预览展示区:
```java
private CameraManager cameraManager;
private SurfaceView surfaceView;
private TextureView textureView; // 可选,根据需求选择
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
surfaceView = findViewById(R.id.surface_view);
textureView = findViewById(R.id.texture_view); // 如果使用TextureView
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0]; // 获取第一个摄像头
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {/*...*/}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
```
在`StateCallback`中,我们需要实现打开、关闭相机的逻辑,以及设置预览会话和捕获器:
```java
public class CameraStateCallback extends CameraDevice.StateCallback {
@Override
public void onOpened(@NonNull CameraDevice camera) {
camera.createPreviewSession(setupPreviewSession(camera));
}
private CameraCaptureSession.Callback setupPreviewSession(CameraDevice camera) {/*...*/}
}
```
对于拍照功能,我们需要创建一个`CaptureRequest.Builder`,设置适当的参数,然后提交请求到预览会话:
```java
private void takePicture() {
final CaptureRequest.Builder captureBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(imageReader.getSurface()); // imageReader用于保存图片
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {/*...*/};
camera.createCaptureSession(Arrays.asList(captureBuilder.addTarget(surfaceView.getHolder().getSurface())), captureCallback, null);
captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); // 开启闪光灯
camera.capture(captureBuilder.build(), captureCallback, null);
}
```
录像功能则需要用到`MediaRecorder`,配置并启动它来记录视频:
```java
private void startRecording() {
MediaRecorder mediaRecorder = new MediaRecorder();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
mediaRecorder.setVideoSize(width, height);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
private File getOutputMediaFile(int type) {/*...*/} // 创建保存视频的文件
```
控制闪光灯的状态可以通过调用`CameraCharacteristics`的`FLASH_MODE`来实现:
```java
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Integer flashMode = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
if (flashMode != null && flashMode == 1) { // 检查相机是否支持闪光灯
if (isFlashOn) {
cameraManager.setTorchMode(cameraId, false); // 关闭闪光灯
} else {
cameraManager.setTorchMode(cameraId, true); // 打开闪光灯
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
```
在实际应用中,还需要处理各种异常情况,比如权限问题、设备不支持等问题。此外,为了提供良好的用户体验,需要实现UI交互,如按钮点击事件,以触发拍照、录像和切换闪光灯操作。通过以上步骤,我们可以使用Android的`camera2` API创建一个功能完善的拍照、录像和控制闪光灯的应用。
2024-07-12 14:31:39
11.34MB
1