Camera2 TakePhoto error CaptureRequest contains unconfigured Input/Output Surface

hossein tavakoli 471 Reputation points
2021-06-16T20:04:53.233+00:00

I use Camera2forms

when I take pictures with a camera ( front or rear) in work, but when I switch camera and I want to take photo there is an error
'CaptureRequest contains unconfigured Input/Output Surface!'

code for switch camera :

            _camera.ReOpenCamera((_currentElement.Camera == CustomViews.CameraOptions.Front) ? true : false);  

code for open camera :

public void ReOpenCamera(bool IsFrontCameraOn)  
        {  
            if (!IsFrontCameraOn)  
            {  
                CloseCamera();  
                OpenCamera(_cameraTexture.Width, _cameraTexture.Height);  
            }  
            else  
            {  
                CloseCamera();  
                OpenFrontCamera(_cameraTexture.Width, _cameraTexture.Height);  
            }  
  
            StartPreview();  
        }  
  
        public void OpenCamera(int width, int height)  
        {  
            if (_context == null || OpeningCamera)  
            {  
                return;  
            }  
  
            OpeningCamera = true;  
  
            SetUpCameraOutputs(width, height);  
  
            _manager.OpenCamera("0", _cameraStateListener, null);  
  
        }  
  
        public void OpenFrontCamera(int width, int height)  
        {  
            if (_context == null || OpeningCamera)  
            {  
                return;  
            }  
  
            OpeningCamera = true;  
  
            SetUpCameraOutputs(width, height);  
  
            _manager.OpenCamera("1", _cameraStateListener, null);  
  
        }  
  
        // Closes the current {@link CameraDevice}.  
        private void CloseCamera()  
        {  
            try  
            {  
  
                if (null != _previewSession)  
                {  
                    _previewSession.Close();  
                    _previewSession = null;  
                }  
                if (null != CameraDevice)  
                {  
                    CameraDevice.Close();  
                    CameraDevice = null;  
                }  
                if (null != _imageReader)  
                {  
                    _imageReader.Close();  
                    _imageReader = null;  
                }  
            }  
            catch (InterruptedException e)  
            {  
                throw new RuntimeException("Interrupted while trying to lock camera closing.", e);  
            }  
            finally  
            {  
            }  
        }  

picture

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,486 Reputation points Microsoft Vendor
    2021-06-17T08:58:48.03+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Based on your error, The reason for this error is that we are capturing a capture (that is, when taking a picture), cameraCaptureSession.capture(createCaptureImageRequest(), cameraCaptureSessionCaptureCallback, null); the captureRequest parameter requires addTarget a surface, and the surface must be cameraDevice.createCaptureSession(previewSurfaceList , CameraCaptureSessionStateCallback, null); a subset of this previewSurfaceList, otherwise an exception will be reported

    I find a similar thread based on your error when using Camera2.

    https://stackoverflow.com/questions/55206571/how-do-i-get-the-camera2-api-to-work-a-second-time

    After change the front camera, I use following code in CameraViewServiceRenderer.cs,

       public void TakePicture()  
               {  
                   if (_camera._cameraId == "0")  
                   {  
                       _camera.LockFocus();  
                   }  
                   else  
                   {  
                       _camera.CaptureStillPicture();  
                   }  
                     
               }  
    

    Then open the CameraDroid.cs, I achieve this function.

       public void CaptureStillPicture()  
               {  
                   try  
                   {  
                        
                       if ( null == CameraDevice)  
                       {  
                           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 = CameraDevice.CreateCaptureRequest(CameraTemplate.StillCapture);  
         
                       stillCaptureBuilder.AddTarget(_imageReader.Surface);  
         
                       // Use the same AE and AF modes as the preview.  
                       stillCaptureBuilder.Set(CaptureRequest.ControlAfMode, (int)ControlAFMode.ContinuousPicture);  
                       SetAutoFlash(stillCaptureBuilder);  
         
                       // Orientation  
                       IWindowManager windowManager = Context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();  
                       var rotation = windowManager.DefaultDisplay.Rotation;  
                       // int rotation = (int)_context.WindowManager.DefaultDisplay.Rotation;  
                       stillCaptureBuilder.Set(CaptureRequest.JpegOrientation, (int)rotation);  
         
                       _previewSession.StopRepeating();  
                       _previewSession.AbortCaptures();  
         
                       try  
                       {  
                           _previewSession.Capture(stillCaptureBuilder.Build(), new CameraCaptureStillPictureSessionCallback(), null);  
                       }  
                       catch (System.Exception e)  
                       {  
                           throw;  
                       }  
         
                   }  
                   catch (CameraAccessException e)  
                   {  
                       e.PrintStackTrace();  
                   }  
               }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.