在使用 SurfaceHolder/SurfaceView 配置 CaptureSession 时,我希望 SurfaceView 可以具有任何布局大小,而我通过 surfaceView.getHolder().setFixedSize(preview_width, preview_height) 为其设置了具有相同纵横比的良好预览大小。结果应该是传入的预览缓冲区可能会缩小到布局大小。
但是在 camera2(硬件级别 LEGACY)中,配置 CaptureSession 仅在我使用具有与 streamConfigurationMap.getOutputSizes(SurfaceHolder.class) 返回的列表中的布局大小完全相同的 SurfaceView 时才有效。如果不是,则图像不会按比例缩小,但配置会引发错误。
/**
* Prerequisites:
* - The device must be opened.
* - The surface view must be ready.
*/
protected void init() {
// ...
try {
CameraCaptureSession.StateCallback cb = new CameraCaptureSession.StateCallback() {
// ...
};
// The following line will result in an error*, if the viewfinder has not the right size:
cameraDevice.createCaptureSession(Arrays.asList(viewfinder.getHolder().getSurface(), imageReaderSmall.getSurface()), cb, null);
}
catch (CameraAccessException e) {
// ...
}
}
从日志(Samsung Galaxy A3 '14,SDV v21):
05-12 ...: Output sizes for SurfaceHolder.class: [1440x1080, 1280x720, 960x720, 880x720, 960x540, 720x540, 800x480, 720x480, 640x480, 528x432, 352x288, 320x240, 176x144]
...
05-12 ... I/CameraManager: Using legacy camera HAL.
...
05-12 ... I/OpenGLRenderer: Initialized EGL, version 1.4
05-12 ... D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 4096
05-12 ... D/OpenGLRenderer: Enabling debug mode 0
05-12 ....CameraActivity: Surface created
05-12 ....CameraActivity: Surface changed 4 540x405
*) 05-12 ... E/CameraDevice-0-LE: Surface with size (w=540, h=405) and format 0x4 is not valid, size not in valid set: [1440x1080, 1280x720, 960x720, 880x720, 960x540, 720x540, 800x480, 720x480, 640x480, 528x432, 352x288, 320x240, 176x144]
05-12 ... W/CameraDevice-JV-0: Stream configuration failed
05-12 ... E/CameraCaptureSession: Session 0: Failed to create capture session; configuration failed
...
05-12 ....CameraActivity: Configure failed!
使用 Nexus 5X、SDK v23 并在 之后等待surfaceChanged()
调用surfaceHolder.setFixedSize()
,预览尺寸不在支持的输出尺寸列表中,但预览不会开始。从日志:
05-12 08:47:10.052 ....CameraActivity: Surface created
05-12 08:47:10.053 ....CameraActivity: Surface changed 4 1455x1080
05-12 08:47:10.054 ....CameraActivity: Find preview size for 1455x1080 (1.347424:1) px
05-12 08:47:10.054 ....CameraActivity: Preview size 1600x1200 px
05-12 08:47:10.070 ....CameraActivity: Surface changed 4 1600x1200
05-12 08:47:10.110 ....CameraActivity: Session started
05-12 08:47:10.163 ....CameraActivity: Surface: Surface(name=null)/@0xec338e5
结果:预览没有开始,我可以给表面视图一个背景颜色来演示它。
我怎样才能解决这个问题,并且仍然使用比使用 SurfaceTexture 更高效和向后兼容的 SurfaceView。