解释camera api2步骤与最简单的完整demo,包括xml
时间: 2024-01-19 11:03:45 浏览: 92
Camera API2是Android系统提供的相机框架,它允许应用程序完全控制相机设置和图像捕获过程。以下是使用Camera API2的基本步骤:
1. 获取相机管理器:使用getSystemService()方法获取相机管理器(CameraManager)实例。
2. 获取相机设备ID:通过CameraManager的getCameraIdList()方法获取相机设备ID,这个ID可以用于打开特定的相机设备。
3. 打开相机:使用CameraManager.openCamera()方法打开相机设备。在打开相机时需要传入一个CameraDevice.StateCallback回调,以便在相机打开成功或者失败时得到通知。
4. 创建预览会话:在相机打开成功后,创建一个预览会话(CameraCaptureSession)。预览会话可以用于展示相机捕获的实时预览图像,也可以用于拍照。
5. 配置预览:创建一个CaptureRequest.Builder实例,用于设置预览请求参数。其中,可以设置预览尺寸、预览帧率、自动对焦模式等等。
6. 开始预览:使用CaptureSession.setRepeatingRequest()方法开始预览。
下面是一个最简单的完整demo,包括XML:
activity_main.xml:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/texture_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
```
MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private TextureView mTextureView;
private CameraDevice mCameraDevice;
private CameraCaptureSession mCaptureSession;
private CaptureRequest.Builder mPreviewRequestBuilder;
private final TextureView.SurfaceTextureListener mSurfaceTextureListener =
new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return true;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
}
};
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice cameraDevice) {
mCameraDevice = cameraDevice;
createPreviewSession();
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
cameraDevice.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice cameraDevice, int error) {
cameraDevice.close();
mCameraDevice = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView = findViewById(R.id.texture_view);
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
@Override
protected void onResume() {
super.onResume();
if (mTextureView.isAvailable()) {
openCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
@Override
protected void onPause() {
closeCamera();
super.onPause();
}
private void openCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
String cameraId = cameraManager.getCameraIdList()[0];
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 0);
return;
}
cameraManager.openCamera(cameraId, mStateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (mCaptureSession != null) {
mCaptureSession.close();
mCaptureSession = null;
}
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
}
private void createPreviewSession() {
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mTextureView.getWidth(), mTextureView.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
try {
mPreviewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Collections.singletonList(previewSurface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
mCaptureSession = cameraCaptureSession;
try {
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
```
这个demo展示了如何在TextureView上预览相机捕获的实时图像。在打开相机时,我们通过getCameraIdList()方法获取可用的相机设备ID列表,然后使用openCamera()方法打开相机设备。在相机打开成功后,我们创建了一个预览会话,并且设置了CaptureRequest.Builder的预览请求参数。最后,通过setRepeatingRequest()方法开始预览。
阅读全文