使用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");
}
}));
}