让您的相机支持多屏幕预览,同步更新。
2020-12-17 本文已影响0人
灿烂的黑土
* 最近有个项目做刷脸人证识别机器,有主屏和客显屏两个屏幕。在人脸识别的时候,主屏和客显屏都需要展示相机数据。开始项目使用的Camera,找了好多方法都没有搞定这个需求,最后在强大的搜索引擎和api2的引领下使用Camera2解决了此问题。
特此记录。*
一、Camera2简介
Camera2是Google在Android 5.0后推出的一个全新的相机API,Camera2和Camera没有继承关系,是完全重新设计的,且Camera2支持的功能也更加丰富,但是提供了更丰富的功能的同时也增加了使用的难度。Google的官方Demo:https://github.com/googlesamples/android-Camera2Basic
二、Camera2 VS Camera
以下分别是使用Camera2和Camera打开相机进行预览并获取预览数据的流程图。


可以看到,和Camera相比,Camera2的调用明显复杂得多,但同时也提供了更强大的功能:
- 支持在非UI线程获取预览数据
- 可以获取更多的预览帧
- 对相机的控制更加完备
- 支持更多格式的预览数据
- 支持高速连拍
但是具体能否使用还要看设备的厂商有无实现。
三、如何支持多个屏幕或者View展示同一相机数据(不了解Camera2的同学请先查看下资料)
1. 直接上干货,先看布局文件,其中texture、texture2是基于TextureView的两个用来展示数据的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="horizontal">
<com.lm.camerademo.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent" />
<com.lm.camerademo.AutoFitTextureView
android:id="@+id/texture2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_height="fill_parent" />
</LinearLayout>
<FrameLayout
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:visibility="visible">
<Button
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
android:text="拍照" />
<ImageButton
android:id="@+id/info"
android:contentDescription="描述"
style="@android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_gravity="center_vertical|right"
android:padding="20dp"
android:src="@mipmap/ic_launcher" />
</FrameLayout>
</RelativeLayout>

2. 首先不要忘记权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

3. 接下来就是Camera2的使用了。使用流程。
- 通过
context.getSystemService(Context.CAMERA_SERVICE)
获取CameraManager
. - 调用
CameraManager .open()
方法在回调中得到CameraDevice
. - 通过
CameraDevice.createCaptureSession()
在回调中获取CameraCaptureSession
. - 构建
CaptureRequest
, 有三种模式可选 预览/拍照/录像. - 通过
CameraCaptureSession
发送CaptureRequest
, capture表示只发一次请求, setRepeatingRequest表示不断发送请求. - 拍照数据可以在
ImageReader.OnImageAvailableListener
回调中获取,CaptureCallback
中则可获取拍照实际的参数和Camera当前状态.
上代码打开摄像头,其中mCameraId是摄像头ID一般就是前后两个摄像头,stateCallBack是个相机状态的监听:
// 打开摄像头
private void openCamera(int width, int height) {
setUpCameraOutputs(width, height);
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
// 打开摄像头
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.openCamera(mCameraId, stateCallback, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
// 摄像头被打开时激发该方法
@Override
public void onOpened(CameraDevice cameraDevice) {
MainActivity.this.cameraDevice = cameraDevice;
// 开始预览
createCameraPreviewSession();
}
// 摄像头断开连接时激发该方法
@Override
public void onDisconnected(CameraDevice cameraDevice) {
cameraDevice.close();
MainActivity.this.cameraDevice = null;
}
// 打开摄像头出现错误时激发该方法
@Override
public void onError(CameraDevice cameraDevice, int error) {
cameraDevice.close();
MainActivity.this.cameraDevice = null;
MainActivity.this.finish();
}
};

重点来了,我们在createCameraPreviewSession的设置展示的数据绑定界面。
private void createCameraPreviewSession() {
try {
SurfaceTexture texture = autoCompleteTextView.getSurfaceTexture();
SurfaceTexture texture2 = autoCompleteTextView2.getSurfaceTexture();
texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
texture2.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
Surface surface = new Surface(texture);
Surface surface2 = new Surface(texture2);
// 创建作为预览的CaptureRequest.Builder
previewRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
// 将textureView的surface作为CaptureRequest.Builder的目标
previewRequestBuilder.addTarget(surface);
previewRequestBuilder.addTarget(surface2);
previewRequestBuilder.addTarget(imageReader.getSurface());
// 创建CameraCaptureSession,该对象负责管理处理预览请求和拍照请求
cameraDevice.createCaptureSession(Arrays.asList(surface, surface2, imageReader.getSurface()), new CameraCaptureSession.StateCallback()
{
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
// 如果摄像头为null,直接结束方法
if (null == cameraDevice) {
return;
}
// 当摄像头已经准备好时,开始显示预览
captureSession = cameraCaptureSession;
try {
// 设置自动对焦模式
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
// 设置自动曝光模式
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
// 开始显示相机预览
previewRequest = previewRequestBuilder.build();
// 设置预览时连续捕获图像数据
captureSession.setRepeatingRequest(previewRequest,
null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Toast.makeText(MainActivity.this, "配置失败!"
, Toast.LENGTH_SHORT).show();
}
}, null
);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}

这样就完成了预览数据输出到两个surfaceview上面。上张图片。
