android camera2点击监听,使用Android camera2 API打开/关闭闪光灯不起作用
时间: 2024-02-28 07:54:41 浏览: 151
要使用 Android camera2 API 打开/关闭闪光灯,需要先进行如下步骤:
1. 获取 CameraManager 实例
2. 获取可用的相机列表
3. 选择需要使用的相机
4. 创建一个 CameraDevice.StateCallback 实例
5. 打开相机
6. 获取 CameraCharacteristics 实例
7. 获取闪光灯是否可用的信息
8. 创建一个 CaptureRequest.Builder 实例
9. 设置闪光灯状态
10. 构建 CaptureRequest 实例
11. 创建一个 CameraCaptureSession.StateCallback 实例
12. 开始预览
下面是一个简单的示例代码,可以在点击按钮时打开/关闭闪光灯:
```
private CameraManager mCameraManager;
private String mCameraId;
private CameraDevice mCameraDevice;
private CameraCaptureSession mCaptureSession;
private CaptureRequest.Builder mPreviewRequestBuilder;
private SurfaceTexture mSurfaceTexture;
private Surface mPreviewSurface;
private boolean mIsFlashOn = false;
// 点击按钮时调用
public void toggleFlash() {
if (mCameraDevice == null) {
return;
}
// 切换闪光灯状态
mIsFlashOn = !mIsFlashOn;
try {
// 创建一个新的 CaptureRequest.Builder 实例
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// 设置闪光灯状态
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, mIsFlashOn ? CaptureRequest.FLASH_MODE_TORCH : CaptureRequest.FLASH_MODE_OFF);
// 构建 CaptureRequest 实例
CaptureRequest previewRequest = mPreviewRequestBuilder.build();
// 发送请求
mCaptureSession.setRepeatingRequest(previewRequest, null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
// 在 onCreate() 方法中调用
private void setupCamera() {
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
// 获取可用的相机列表
String[] cameraIds = mCameraManager.getCameraIdList();
// 选择需要使用的相机
for (String cameraId : cameraIds) {
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_BACK) {
mCameraId = cameraId;
break;
}
}
// 创建一个 CameraDevice.StateCallback 实例
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
};
// 打开相机
mCameraManager.openCamera(mCameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
// 开始预览
private void startPreview() {
try {
// 获取 CameraCharacteristics 实例
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(mCameraId);
// 获取闪光灯是否可用的信息
Boolean flashAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
// 创建一个 CaptureRequest.Builder 实例
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// 设置闪光灯状态
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
// 获取 SurfaceTexture 实例
mSurfaceTexture = new SurfaceTexture(0);
// 获取 Surface 实例
mPreviewSurface = new Surface(mSurfaceTexture);
// 添加 Surface 到 CaptureRequest.Builder 实例中
mPreviewRequestBuilder.addTarget(mPreviewSurface);
// 创建一个 CameraCaptureSession.StateCallback 实例
CameraCaptureSession.StateCallback callback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
mCaptureSession = session;
try {
// 构建 CaptureRequest 实例
CaptureRequest previewRequest = mPreviewRequestBuilder.build();
// 发送请求
mCaptureSession.setRepeatingRequest(previewRequest, null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
session.close();
mCaptureSession = null;
}
};
// 创建一个 CameraCaptureSession 实例
mCameraDevice.createCaptureSession(Collections.singletonList(mPreviewSurface), callback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
```
值得注意的是,闪光灯是否可用的信息可以通过 CameraCharacteristics.FLASH_INFO_AVAILABLE 获取。如果该值为 false,则说明该相机不支持闪光灯。
阅读全文