Share via

capture image by front camera in Camera2Basic xamarin

hossein tavakoli 471 Reputation points
2021-04-04T12:54:25.64+00:00

I useing Camera2basic.

when I use rear camera it capture a picture correctly, but when I switch on front camera capture picture not working.

button switch camera code :
this code working correctly show front camera view.

public void OnClick(View v)  
        {  
            if (v.Id == Resource.Id.picture)  
            {  
                string CAMERA_FRONT = "1";  
                string CAMERA_BACK = "0";  
                if (WhatCameraIsOn.Equals(CAMERA_FRONT))  
                {  
                    WhatCameraIsOn = CAMERA_BACK;  
                    CloseCamera();  
                    OpenCamera(mTextureView.Width, mTextureView.Height);  
                }  
                else if (WhatCameraIsOn.Equals(CAMERA_BACK))  
                {  
                    WhatCameraIsOn = CAMERA_FRONT;  
                    CloseCamera();  
                    OpenCameraFront(mTextureView.Width, mTextureView.Height);  
                }  
             }  
         }  

Button Picture Click code:
this code take picture only by rear camera.

        public void OnClick(View v)  
        {  
            if (v.Id == Resource.Id.picture)  
            {  
                    TakePicture();  
             }  
         }  

How can I take picture by front camera
here is my app picture

Developer technologies | .NET | Xamarin
0 comments No comments

Answer accepted by question author

JarvanZhang 23,971 Reputation points
2021-04-05T09:16:36.737+00:00

Hello,​

Welcome to our Microsoft Q&A platform!

Hi, hosseintavakoli. To take picture with front camera, try to replace the mCaptureSession.Capture(..) command with CaptureStillPicture() method.

   private void LockFocus()  
   {  
       try  
       {  
           // This is how to tell the camera to lock focus.  
           mPreviewRequestBuilder.Set(CaptureRequest.ControlAfTrigger, (int)ControlAFTrigger.Start);  
           // Tell #mCaptureCallback to wait for the lock.  
           mState = STATE_WAITING_LOCK;  
           //mCaptureSession.Capture(mPreviewRequestBuilder.Build(), mCaptureCallback,mBackgroundHandler);  
           CaptureStillPicture();  
       }  
       catch (CameraAccessException e)  
       {  
           e.PrintStackTrace();  
       }  
   }  

Best Regards,

Jarvan Zhang


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.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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