android studio怎么使用Camera2 API
时间: 2023-05-24 12:04:48 浏览: 389
要使用Camera2 API在Android Studio中,你需要依赖以下库:
```gradle
implementation 'androidx.camera:camera-camera2:1.0.0-beta01'
```
您还需要具备相应的权限,例如`CAMERA`和`WRITE_EXTERNAL_STORAGE`。
使用Camera2 API需要遵循以下步骤:
Step 1:检查设备是否支持相机功能
您可以使用CameraManager API进行检查。例如:
```java
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String[] cameraIdList = manager.getCameraIdList();
for (String cameraId : cameraIdList) {
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
int facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing == CameraCharacteristics.LENS_FACING_FRONT) {
// 前置摄像头可用
} else if (facing == CameraCharacteristics.LENS_FACING_BACK) {
// 后置摄像头可用
}
}
} catch (Exception e) {
e.printStackTrace();
}
```
Step 2:打开相机设备
您可以使用`openCamera`方法打开相机设备。例如:
```java
manager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
}
@Override
public void onDisconnected(CameraDevice camera) {
mCameraDevice.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
mCameraDevice.close();
mCameraDevice = null;
}
}, null);
```
请注意,您需要处理相机设备的连接,断开连接和错误事件。
Step 3:创建相机会话
您需要使用`createCaptureSession`方法创建相机会话。例如:
```java
List<Surface> surfaces = new ArrayList<>();
surfaces.add(mPreviewSurface);
surfaces.add(mImageReader.getSurface());
mCameraDevice.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
mCameraCaptureSession = session;
try {
CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(mPreviewSurface);
CaptureRequest request = builder.build();
mCameraCaptureSession.setRepeatingRequest(request, null, mBackgroundHandler);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
mCameraCaptureSession = null;
}
}, null);
```
在相机会话创建后,您需要构建一个捕获请求并通过相机会话发送它。在这个例子中,您使用Preview目标并在后台线程上重复请求。
Step 4:拍照并保存照片
要进行拍照,您需要使用CaptureRequest.Builder并指定target为ImageReader的Surface。当图片可用时,您需要将其保存到文件中。例如:
```java
CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
builder.addTarget(mImageReader.getSurface());
int rotation = getWindowManager().getDefaultDisplay().getRotation();
builder.set(CaptureRequest.JPEG_ORIENTATION, getRotation(rotation));
CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
File file = new File(getExternalFilesDir(null), "photo.jpg");
Image image = mImageReader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
try {
FileOutputStream outputStream = new FileOutputStream(file);
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
outputStream.write(bytes);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
image.close();
}
}
};
mCameraCaptureSession.stopRepeating();
mCameraCaptureSession.abortCaptures();
mCameraCaptureSession.capture(builder.build(), captureCallback, mBackgroundHandler);
```
在这个例子中,您使用TEMPLATE_STILL_CAPTURE模板创建CaptureRequest.Builder,并将target设置为ImageReader的Surface。然后,您定义一个捕获回调来捕获和保存图像。最后,您停止相机会话的重复请求并使用捕获请求捕获图像。
以上是使用Camera2 API的基本步骤,您可以在此基础上进行自定义和优化。
阅读全文