如何使camera2 API再次运行?

时间:2019-03-17 11:32:22

标签: xamarin android-camera2

我正在使用Xamarin / Android(不是Forms),试图将camera2basic api示例集成到我的项目中。 https://developer.xamarin.com/samples/monodroid/android5.0/Camera2Basic/

我没有更改样本中的任何内容,并且我只对使用主相机和仅拍摄快照感兴趣。

我的项目有一个MainActivity,camera2是我这样称呼的Fragment之一:

 string fragmentTag = this.Resources.GetString(Resource.String.camera_form);

 // Begin the transaction
 FragmentTransaction trans = this.FragmentManager.BeginTransaction();

 // Replace the old fragment with the new one.
 trans.Add(Resource.Id.fragment_container, camera2BasicFragment, fragmentTag);

 // Add the transaction to the back stack.
 // The tag is added so we can use PopBackStack to skip a screen on the back key
 trans.AddToBackStack(fragmentTag);

 // Don't forget to commit
 trans.Commit();

一切正常。它会拍张照片并将其保存到文件夹中。

第二次运行它时,它会显示预览,然后当我拍照时,它在此处崩溃,抛出的地方是:

  public void CaptureStillPicture()
  {
     try
     {
        var activity = Activity;
        if (null == activity || null == mCameraDevice)
        {
           return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        if (stillCaptureBuilder == null)
           stillCaptureBuilder = mCameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

        stillCaptureBuilder.AddTarget(mImageReader.Surface);

        // Use the same AE and AF modes as the preview.
        stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
        SetAutoFlash(stillCaptureBuilder);

        // Orientation
        int rotation = (int)activity.WindowManager.DefaultDisplay.Rotation;
        stillCaptureBuilder.Set(CaptureRequest.JpegOrientation, GetOrientation(rotation));

        mCaptureSession.StopRepeating();

        try
        {
           mCaptureSession.Capture(stillCaptureBuilder.Build(), new CameraCaptureStillPictureSessionCallback(this), null);
        }
        catch (System.Exception e)
        {
           throw;
        }

     }
     catch (CameraAccessException e)
     {
        e.PrintStackTrace();
     }
  }

出现此错误:

{Java.Lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
  at Java.Interop.JniEnvironment+InstanceMethods.CallIntMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00069] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeAbstractInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00014] in <286213b9e14c442ba8d8d94cc9dbec8e>:0 
  at Android.Hardware.Camera2.CameraCaptureSessionInvoker.Capture (Android.Hardware.Camera2.CaptureRequest request, Android.Hardware.Camera2.CameraCaptureSession+CaptureCallback listener, Android.OS.Handler handler) [0x00078] in <b781ed64f1d743e7881ac038e0fbdf85>:0 
  at RvsMobileApp.Activities.Camera2BasicFragment.CaptureStillPicture () [0x000b7] in C:\Source\RVS\rvs-mobile-app\src\Rvs.Mobile.App\Activities\Camera2BasicFragment.cs:807 
  --- End of managed Java.Lang.IllegalArgumentException stack trace ---
java.lang.IllegalArgumentException: CaptureRequest contains unconfigured Input/Output Surface!
    at android.hardware.camera2.CaptureRequest.convertSurfaceToStreamId(CaptureRequest.java:674)
    at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:1066)
    at android.hardware.camera2.impl.CameraDeviceImpl.capture(CameraDeviceImpl.java:936)
    at android.hardware.camera2.impl.CameraCaptureSessionImpl.capture(CameraCaptureSessionImpl.java:173)
    at md5bbb797339b35f7667da89d6634e22c37.CameraCaptureListener.n_onCaptureCompleted(Native Method)
    at md5bbb797339b35f7667da89d6634e22c37.CameraCaptureListener.onCaptureCompleted(CameraCaptureListener.java:37)
    at android.hardware.camera2.impl.CameraCaptureSessionImpl$1.lambda$onCaptureCompleted$3(CameraCaptureSessionImpl.java:640)
    at android.hardware.camera2.impl.-$$Lambda$CameraCaptureSessionImpl$1$OA1Yz_YgzMO8qcV8esRjyt7ykp4.run(Unknown Source:8)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.os.HandlerThread.run(HandlerThread.java:65)
}
    base: {Java.Lang.RuntimeException}
    JniPeerMembers: {Android.Runtime.XAPeerMembers}

起初我以为是内存泄漏,所以我确保我的片段正在杀死自己。这是按下完成按钮时结束片段的方式:

    case Resource.Id.camera_finished:
       // EventHandler<DialogClickEventArgs> nullHandler = null;
       Activity activity = Activity;
       if (activity != null)
       {
          // Send all of the data to the service
          // SendPhotosAndDataToService();

          // Call the paren activitity's back to END this Fragment
          activity.FragmentManager.BeginTransaction().Remove(this).CommitNow();
          //activity.OnBackPressed();

       }
       break;

这是我重现该错误的步骤:

启动相机(加载片段) 查看预览 拍张照片 返回主活动(封闭片段)

启动相机(加载片段) 查看预览 拍张照片!!!!

只要不拍照,我就可以根据需要多次加载和卸载片段。

我用Google搜索“ CaptureRequest包含未配置的输入/输出表面!”,但没有获得足够的信息来真正理解问题。 我认为在第一次运行后,某些东西并没有自行清理。 我已经在这个问题上工作了几天了。

2 个答案:

答案 0 :(得分:1)

还原摄像机片段时,请重新初始化 stillCaptureBuilder 。更好的是,请确保在碎片被破坏时清理 stillCaptureBuilder

答案 1 :(得分:0)

正如Alex Cohn所指出的,我在阅读本文时发现: https://hofmadresu.com/2018/09/11/android-camera2-trials-and-tribulations.html

这是一个很好的BTW资源,示例代码没有释放stillCaptureBuilder,因此可以第二次使用。

      public void CaptureStillPicture()
      {
         try
         {
            var activity = Activity;
            if (null == activity || null == mCameraDevice)
            {
               return;
            }

            // THIS WAS NOT RELEASING THE RESOURCES AND SHOULD BE REMOVED FROM THE SAMPLE!
            //// This is the CaptureRequest.Builder that we use to take a picture.
            ////if (stillCaptureBuilder == null)
            ////   stillCaptureBuilder = mCameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

// This is the proper code            
var stillCaptureBuilder = mCameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);

            stillCaptureBuilder.AddTarget(mImageReader.Surface);

            // Use the same AE and AF modes as the preview.
            stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);
            SetAutoFlash(stillCaptureBuilder);

            // Orientation
            int rotation = (int)activity.WindowManager.DefaultDisplay.Rotation;
            stillCaptureBuilder.Set(CaptureRequest.JpegOrientation, GetOrientation(rotation));

            mCaptureSession.StopRepeating();
            mCaptureSession.AbortCaptures();

            try
            {
               mCaptureSession.Capture(stillCaptureBuilder.Build(), new CameraCaptureStillPictureSessionCallback(this), null);
            }
            catch (System.Exception e)
            {
               throw;
            }

         }
         catch (CameraAccessException e)
         {
            e.PrintStackTrace();
         }
      }

我正在对此进行记录,以便所有与camera2挣扎的人都可以学习。 一如既往地谢谢你