使用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://www.uj5u.com/yidong/273743.html
標籤:其他
上一篇:關于RecycleView的overScrollMode屬性的一點探索
下一篇:Android系統原始碼編譯
