用java写一个camera api2的拍照demo
时间: 2023-07-15 17:16:00 浏览: 124
好的,以下是一个简单的Camera2 API拍照的demo:
1. 在AndroidManifest.xml文件中添加相机权限:
```xml
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
```
2. 在布局文件中添加一个TextureView用于预览相机画面:
```xml
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
3. 在Activity中定义相机相关变量:
```java
private CameraManager cameraManager;
private String cameraId;
private CameraDevice cameraDevice;
private Size previewSize;
private CaptureRequest.Builder previewBuilder;
private CameraCaptureSession previewSession;
private ImageReader imageReader;
```
4. 在Activity中初始化相机:
```java
private void initCamera() {
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
if (characteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_BACK) {
this.cameraId = cameraId;
break;
}
}
StreamConfigurationMap map = cameraManager.getCameraCharacteristics(cameraId)
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
previewSize = map.getOutputSizes(SurfaceTexture.class)[0];
imageReader = ImageReader.newInstance(previewSize.getWidth(), previewSize.getHeight(), ImageFormat.JPEG, 1);
imageReader.setOnImageAvailableListener(imageReaderListener, null);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
return;
}
cameraManager.openCamera(cameraId, cameraStateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
```
5. 在Activity中实现相机状态回调:
```java
private CameraDevice.StateCallback cameraStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
cameraDevice = camera;
try {
SurfaceTexture texture = textureView.getSurfaceTexture();
texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
Surface surface = new Surface(texture);
previewBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
previewBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface, imageReader.getSurface()), captureStateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
cameraDevice.close();
cameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};
```
6. 在Activity中实现相机捕获状态回调:
```java
private CameraCaptureSession.StateCallback captureStateCallback = new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
previewSession = session;
try {
previewBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
previewSession.setRepeatingRequest(previewBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
};
```
7. 在Activity中实现ImageReader的回调方法,用于保存图片:
```java
private ImageReader.OnImageAvailableListener imageReaderListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireNextImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
saveImage(data);
image.close();
}
};
```
8. 在Activity中添加保存图片的方法:
```java
private void saveImage(byte[] data) {
try {
File file = new File(getExternalFilesDir(null), "photo.jpg");
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
9. 最后在Activity中调用initCamera()方法,即可启动相机预览并拍照:
```java
@Override
protected void onResume() {
super.onResume();
initCamera();
}
@Override
protected void onPause() {
super.onPause();
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
}
```
以上是一个简单的Camera2 API拍照的demo,希望能对您有所帮助。
阅读全文