【问题标题】:Start camera preview using API2 with surfaceview and delay使用具有表面视图和延迟的 API2 启动相机预览
【发布时间】:2019-02-14 01:37:30
【问题描述】:

我在使用相机 2 API 和表面视图时遇到了问题。这是一个旧的嵌入式系统,我必须使用surfaceview,原因我在这里无法解释。

我找到了这个示例,其中显示了它是如何完成的:

https://android.googlesource.com/platform/frameworks/base/+/ee699a6/tests/Camera2Tests?autodive=0

我需要在“onCreate”之后延迟开始预览。该代码可以毫无延迟地工作。但是,如果我添加延迟,预览将永远不会出现(表面回调永远不会被调用)

这里是工作的代码片段:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfaceviewtest);

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            startCameraExec();
        }
    }, 2000);
}

但这有效

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfaceviewtest);
    startCameraExec();
}

我的 startCameraExec 只是设置了后台线程并设置了表面回调

private void startCameraExec() {
    Log.d(TAG, "Starting API2 camera...");
    // Start a background thread to manage camera requests
    mBackgroundThread = new HandlerThread("background");
    mBackgroundThread.start();
    mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
    mForegroundHandler = new Handler(getMainLooper());
    mCameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

    mSurfaceView = (SurfaceView) findViewById(R.id.mainSurfaceView);
    mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);
}

.xml 也很简单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <SurfaceView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_margin="80dp"
        android:onClick="onClickOnSurfaceView" />
</RelativeLayout>

这就是我认为的问题,如果延迟初始化,这永远不会被调用

final SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() {

... a bunch of code
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // On the first invocation, width and height were automatically set to the view's size
        if (mCameraId == null) {
            // Find the device's back-facing camera and set the destination buffer sizes
            try {
                for (String cameraId : mCameraManager.getCameraIdList()) {
                    CameraCharacteristics cameraCharacteristics =
            ... a bunch of code

有什么线索可以解决这个问题吗?

谢谢。

【问题讨论】:

  • 你是对的; mSurfaceHolderCallback 在表面创建/更改很可能已经发生之前不会添加。所以你可能不应该期望onSurfaceChanged() 被调用。这导致我们,“你为什么需要延迟?”
  • 这不是普通的安卓应用。它是一个嵌入式系统,摄像头的控制方式不同,只能通过特殊的 JNI 命令启动,并且比 Activity 布局膨胀需要更长的时间。我不得不推迟整个过程,直到相机准备好。

标签: android embedded android-camera camera-api


【解决方案1】:

您可以尝试输入延迟之后 surfaceChanged( { // if (mCameraId == null) {

但您必须跟踪获取和启动相机流所需的所有条件。如果只延迟开始预览的调用可能更安全。

【讨论】:

    【解决方案2】:

    我找到了一个更简单的解决方案。我隐藏了表面并在延迟后才使其可见。这就是我要找的。感谢您的意见。

    private void startCameraExec() {
        Log.d(TAG, "Starting API2 camera...");
    
        mSurfaceView = (SurfaceView) findViewById(R.id.mainSurfaceView);
    
        // This did the trick :)
        mSurfaceView.setVisibility(View.VISIBLE);
        mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多