第16讲 Deferred Surface实战 - Android Camera2 API

本讲是Android Camera专题系列的第16讲,我们介绍Android Camera2 API专题的Deferred Surface。

更多资源:

资源 描述
在线课程 极客笔记在线课程
知识星球 星球名称:深入浅出Android Camera
星球ID: 17296815
Wechat 极客笔记圈

Android Camera2 API课程体系

Android Camera2 API课程体系

Deferred Surface回顾

Deferred Surface请参阅链接:Deferred Surface详解

Deferred Surface实战

创建OutputConfiguration的方式

synchronized( background_camera_lock ) {
    Surface preview_surface = null;
    if (!mUsePreviewDeferredSurface) {
        preview_surface = getPreviewSurface();
    }
    if( video_recorder != null ) {
        if( supports_photo_video_recording && !want_video_high_speed && want_photo_video_recording ) {
            if (!mUsePreviewDeferredSurface && !mEnablePreviewShareSurface) {
                surfaces = Arrays.asList(preview_surface, video_recorder_surface, imageReader.getSurface());
            } else {
                captureOutputConfiguration = new OutputConfiguration(imageReader.getSurface());
                if (preview_surface != null) {
                    mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
                } else {
                    mPreviewOutputConfiguration = new OutputConfiguration(
                            new android.util.Size(mPreviewWidth, mPreviewHeight),
                            SurfaceTexture.class);
                }
                if (mEnablePreviewShareSurface) {
                    mPreviewOutputConfiguration.enableSurfaceSharing();
                    mPreviewOutputConfiguration.addSurface(video_recorder_surface);
                } else {
                    recordOutputConfiguration = new OutputConfiguration(video_recorder_surface);
                }
            }
        }
        else {
            if (!mUsePreviewDeferredSurface && !mEnablePreviewShareSurface) {
                surfaces = Arrays.asList(preview_surface, video_recorder_surface);
            } else {
                if (preview_surface != null) {
                    mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
                } else {
                    mPreviewOutputConfiguration = new OutputConfiguration(
                            new android.util.Size(mPreviewWidth, mPreviewHeight),
                            SurfaceTexture.class);
                }
                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
        if (!mUsePreviewDeferredSurface && !mEnablePreviewShareSurface) {
            surfaces = Collections.singletonList(preview_surface);
        } else {
            if (preview_surface != null) {
                mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
            } else {
                mPreviewOutputConfiguration = new OutputConfiguration(
                        new android.util.Size(mPreviewWidth, mPreviewHeight),
                        SurfaceTexture.class
                );
                if (mEnablePreviewShareSurface) {
                    mPreviewOutputConfiguration.enableSurfaceSharing();
                }
            }
        }
    }
    else if( imageReaderRaw != null ) {
        surfaces = Arrays.asList(preview_surface, imageReader.getSurface(), imageReaderRaw.getSurface());
    }
    else {
        if (!mUsePreviewDeferredSurface && !mEnablePreviewShareSurface) {
            surfaces = Arrays.asList(preview_surface, imageReader.getSurface());
        } else {
            if (preview_surface != null) {
                mPreviewOutputConfiguration = new OutputConfiguration(preview_surface);
            } else {
                mPreviewOutputConfiguration = new OutputConfiguration(
                        new android.util.Size(mPreviewWidth, mPreviewHeight),
                        SurfaceTexture.class
                );
                if (mEnablePreviewShareSurface) {
                    mPreviewOutputConfiguration.enableSurfaceSharing();
                }
                captureOutputConfiguration = new OutputConfiguration(imageReader.getSurface());
            }
        }
    }
    if( MyDebug.LOG ) {
        Log.i(TAG, "texture: " + mSurfaceTexture);
        Log.i(TAG, "preview_surface: " + preview_surface);
    }
}

调用addSurface,然后finalizeOutputConfigurations

if (mUsePreviewDeferredSurface) {
    mPreviewOutputConfiguration.addSurface(surface);
    try {
        List<OutputConfiguration> finalizeOutputConfigs = new ArrayList<>();
        finalizeOutputConfigs.add(mPreviewOutputConfiguration);
        mCameraCaptureSession.finalizeOutputConfigurations(finalizeOutputConfigs);
    } catch (Exception e) {
        Log.e(TAG, "finalizeOutputConfigurations with exception:" + e.toString());
    }
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程

Android Camera2 API