本讲是Android Camera专题系列的第15讲,我们介绍Android Camera2 API专题的Surface Sharing。
更多资源:
资源 | 描述 |
---|---|
在线课程 | 极客笔记在线课程 |
知识星球 | 星球名称:深入浅出Android Camera 星球ID: 17296815 |
极客笔记圈 |
Android Camera2 API课程体系
在创建Camera Capture Session前Add Share Surface
synchronized( background_camera_lock ) {
Surface preview_surface = getPreviewSurface();
if( video_recorder != null ) {
if( supports_photo_video_recording && !want_video_high_speed && want_photo_video_recording ) {
surfaces = Arrays.asList(preview_surface, video_recorder_surface, imageReader.getSurface());
captureOutputConfiguration = new OutputConfiguration(imageReader.getSurface());
mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
if (mEnablePreviewShareSurface) {
mPreviewOutputConfiguration.enableSurfaceSharing();
mPreviewOutputConfiguration.addSurface(video_recorder_surface);
} else {
recordOutputConfiguration = new OutputConfiguration(video_recorder_surface);
}
}
else {
surfaces = Arrays.asList(preview_surface, video_recorder_surface);
mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
if (mEnablePreviewShareSurface) {
mPreviewOutputConfiguration.enableSurfaceSharing();
mPreviewOutputConfiguration.addSurface(video_recorder_surface);
} else {
recordOutputConfiguration = new OutputConfiguration(video_recorder_surface);
}
}
// n.b., raw not supported for photo snapshots while video recording
}
else if( want_video_high_speed ) {
// future proofing - at the time of writing want_video_high_speed is only set when recording video,
// but if ever this is changed, can only support the preview_surface as a target
surfaces = Collections.singletonList(preview_surface);
mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
mPreviewOutputConfiguration.enableSurfaceSharing();
}
else if( imageReaderRaw != null ) {
surfaces = Arrays.asList(preview_surface, imageReader.getSurface(), imageReaderRaw.getSurface());
}
else {
surfaces = Arrays.asList(preview_surface, imageReader.getSurface());
mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
mPreviewOutputConfiguration.enableSurfaceSharing();
captureOutputConfiguration = new OutputConfiguration(imageReader.getSurface());
}
if( MyDebug.LOG ) {
Log.i(TAG, "texture: " + mSurfaceTexture);
Log.i(TAG, "preview_surface: " + preview_surface);
}
}
在创建Camera Capture Session后Add Share Surface
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
if( MyDebug.LOG ) {
Log.i(TAG, "onConfigured: " + session);
Log.i(TAG, "captureSession was: " + mCameraCaptureSession);
}
if( mCameraDevice == null ) {
if( MyDebug.LOG ) {
Log.i(TAG, "camera is closed");
}
synchronized( background_camera_lock ) {
callback_done = true;
background_camera_lock.notifyAll();
}
return;
}
synchronized( background_camera_lock ) {
mCameraCaptureSession = session;
Surface surface = getPreviewSurface();
if( MyDebug.LOG ) {
Log.i(TAG, "add surface to previewBuilder: " + surface);
}
mPreviewBuilder.addTarget(surface);
if( video_recorder != null ) {
if( MyDebug.LOG ) {
Log.i(TAG, "add video recorder surface to previewBuilder: " + video_recorder_surface);
}
mPreviewBuilder.addTarget(video_recorder_surface);
}
if (mEnablePreviewShareSurface) {
mPreviewOutputConfiguration.addSurface(mPreviewImageReader.getSurface());
try {
mCameraCaptureSession.updateOutputConfiguration(mPreviewOutputConfiguration);
mPreviewBuilder.addTarget(mPreviewImageReader.getSurface());
} catch (Exception e) {
Log.e(TAG, "updateOutputConfiguration with exception:" + e.toString());
}
}
try {
setRepeatingRequest();
}
catch(CameraAccessException e) {
if( MyDebug.LOG ) {
Log.e(TAG, "failed to start preview");
Log.e(TAG, "reason: " + e.getReason());
Log.e(TAG, "message: " + e.getMessage());
}
e.printStackTrace();
// we indicate that we failed to start the preview by setting captureSession back to null
// this will cause a CameraControllerException to be thrown below
mCameraCaptureSession = null;
}
}
synchronized( background_camera_lock ) {
callback_done = true;
background_camera_lock.notifyAll();
}
}