程序员

使用createcapturesession(SessionConfiguration)创建捕获会话

作者:admin 2021-05-15 我要评论

使用createcapturesession(SessionConfiguration)创建捕获会话 最近在学习camera2的时候发现原本的 createCaptureSession(NonNull List outputs, NonNull Camera...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

使用createcapturesession(SessionConfiguration)创建捕获会话

最近在学习camera2的时候发现原本的

createCaptureSession(@NonNull List outputs,
@NonNull CameraCaptureSession.StateCallback callback, @Nullable Handler handler)

    @Deprecated
    public abstract void createCaptureSession(@NonNull List<Surface> outputs,
            @NonNull CameraCaptureSession.StateCallback callback, @Nullable Handler handler)
            throws CameraAccessException;

已不推荐使用,android开发者文档那边也没有更新,因此写一篇博客记录一下新的方法

public void createCaptureSession(
            SessionConfiguration config) throws CameraAccessException {
        throw new UnsupportedOperationException("No default implementation");
    }

让我们先看一下SessionConfiguration的源码

    /**
     * Create a new {@link SessionConfiguration}.
     *
     * @param sessionType The session type.
     * @param outputs A list of output configurations for the capture session.
     * @param executor The executor which should be used to invoke the callback. In general it is
     *                 recommended that camera operations are not done on the main (UI) thread.
     * @param cb A state callback interface implementation.
     *
     * @see #SESSION_REGULAR
     * @see #SESSION_HIGH_SPEED
     * @see CameraDevice#createCaptureSession(List, CameraCaptureSession.StateCallback, Handler)
     * @see CameraDevice#createCaptureSessionByOutputConfigurations
     * @see CameraDevice#createReprocessableCaptureSession
     * @see CameraDevice#createConstrainedHighSpeedCaptureSession
     */
    public SessionConfiguration(@SessionMode int sessionType,
            @NonNull List<OutputConfiguration> outputs,
            @NonNull @CallbackExecutor Executor executor,
            @NonNull CameraCaptureSession.StateCallback cb) {
        mSessionType = sessionType;
        mOutputConfigurations = Collections.unmodifiableList(new ArrayList<>(outputs));
        mStateCallback = cb;
        mExecutor = executor;
    }

是不是很熟悉,没错,output是输出流,也就是我们以前所使用的surface output;cb则是状态回调借口,也就是我们以前的callback;executor则是应用于执行回调的执行器,用于执行Intent的工作,我们再看源码。

public interface Executor {

    /**
     * Executes the given command at some time in the future.  The command
     * may execute in a new thread, in a pooled thread, or in the calling
     * thread, at the discretion of the {@code Executor} implementation.
     *
     * @param command the runnable task
     * @throws RejectedExecutionException if this task cannot be
     * accepted for execution
     * @throws NullPointerException if command is null
     */
    void execute(Runnable command);
}

就是一个执行命令的线程而已。

最后附上我自己的代码,希望对大家能有所帮助。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    cameraDevice.createCaptureSession(new SessionConfiguration(
                            SessionConfiguration.SESSION_REGULAR, outputs,
                            new Executor() {
                                @Override
                                public void execute(Runnable command) {
                                    command.run();
                                }
                            },
                            new CameraCaptureSession.StateCallback() {
                                @Override
                                public void onConfigured(@NonNull CameraCaptureSession session) {
                                    cameraCaptureSession = session;
                                    Log.i(TAG, "onConfigured: ====>CreateCameraCaptureSession Successful");

                                    try {
                                        builder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                                        builder.addTarget(mySurface);
                                        cameraCaptureSession.setRepeatingRequest(builder.build(), null, null);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }

                                @Override
                                public void onConfigureFailed(@NonNull CameraCaptureSession session) {
                                    Log.e(TAG, "onConfigureFailed: ====>Create CameraCaptureSession Failed");
                                }
                            }));
                }

在这里插入图片描述

;原文链接:https://blog.csdn.net/qq_42158560/article/details/115467129

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • “鸿蒙设备开发”选“址” --&gt;

    “鸿蒙设备开发”选“址” --&gt;

  • Nextcloud是如何成为终极开源生产力套

    Nextcloud是如何成为终极开源生产力套

  • 手把手教你用Pycharm连接远程Python环

    手把手教你用Pycharm连接远程Python环

  • Windows 10X镜像生成工具发布:任意PC

    Windows 10X镜像生成工具发布:任意PC

腾讯云代理商
海外云服务器