Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

implementation of front camera capture

matutematute Member ✭✭

I need to take a screenshot of the front camera without user interaction and save it to the device. Here someone recommended some samples links but they are made in java. Some code that works plase?

Best Answer

Answers

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @matute this should help:
    https://forums.xamarin.com/discussion/comment/203165/#Comment_203165

    Camera cam = null;
    var cameraInfo = new Camera.CameraInfo();
    
    for (var i = 0; i < Camera.NumberOfCameras; i++) {
        Camera.GetCameraInfo(i, cameraInfo);
    
        if (cameraInfo.Facing == CameraFacing.Front)
            cam = Camera.Open(i);                    
    }
    
  • jezhjezh Member, Xamarin Team Xamurai

    For this, you can check the official sample here: Xamarin.Android - Camera2 Basic Sample

    If we want to swich to the front camera, we just need to change into the following code:

    if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Back)))
    {
        continue;
    }
    

    So the full code is like this:

            private void SetUpCameraOutputs(int width, int height)
            {
                var activity = Activity;
                var manager = (CameraManager)activity.GetSystemService(Context.CameraService);
                try
                {
                    for (var i = 0; i < manager.GetCameraIdList().Length; i++)
                    {
                        var cameraId = manager.GetCameraIdList()[i];
                        CameraCharacteristics characteristics = manager.GetCameraCharacteristics(cameraId);
    
                        // We want to use a front facing camera in this sample.
                        var facing = (Integer)characteristics.Get(CameraCharacteristics.LensFacing);
    
                        if (facing != null && facing == (Integer.ValueOf((int)LensFacing.Back)))
                        {
                            continue;
                        }
    
                     // other code
                    }
               }
            }
    

    Note: Above code is in the function SetUpCameraOutputs of class Camera2BasicFragment.cs.

  • matutematute Member ✭✭

    Thank´s guys!, i made it work finally with old API (camera). Now i ´m going to try with the new API (camera2).

  • matutematute Member ✭✭

    Yes, thank´s a lot!

Sign In or Register to comment.