Nicole Ramírez
Nicole Ramírez

Reputation: 345

Xamarin set camera resolution

I have done a custom camera application in Xamarin Visual Studio, it takes the pictures with a very low resolution, so I added this piece of code.

public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int w, int h)
        {
            _camera = Android.Hardware.Camera.Open();
            Android.Hardware.Camera.Parameters param = _camera.GetParameters();
            IList<Android.Hardware.Camera.Size> supportedSizes = param.SupportedPictureSizes;
            Android.Hardware.Camera.Size sizePicture = supportedSizes[0];
            param.SetPictureSize(sizePicture.Height, sizePicture.Width);
            _camera.SetParameters(param);

            var previewSize = _camera.GetParameters().PreviewSize;
            _textureView.LayoutParameters = 
                new FrameLayout.LayoutParams(h, w, GravityFlags.Center);
            try
            {
                _camera.SetPreviewTexture(surface);
                _camera.StartPreview();
            }
            catch (Java.IO.IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            _textureView.Rotation = 90.0f;
        }

In the firts line I get the Camera Parameters, then I get the Supported Picture Size after that I select the firts ([0]) and finallly I set the Picture Size,

Android.Hardware.Camera.Parameters param = _camera.GetParameters();
            IList<Android.Hardware.Camera.Size> supportedSizes = param.SupportedPictureSizes;
            Android.Hardware.Camera.Size sizePicture = supportedSizes[0];
            param.SetPictureSize(sizePicture.Height, sizePicture.Width);
            _camera.SetParameters(param);

But when I run the code this message shows up:

Unhandled Exception:

Java.Lang.RuntimeException: setParameters failed

what is wrong here ? I can not set any of the supported sizes that the function returns ? how to I select any of them for instance the first? there is something else that I have to tke into account?

Upvotes: 3

Views: 3522

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

You have the width and height parameters transposed thus supplying an invalid (non-supported) picture size.

param.SetPictureSize(sizePicture.Width, sizePicture.Height);

void setPictureSize (int width, int height)

re: https://developer.android.com/reference/android/hardware/Camera.Parameters.html

Upvotes: 3

Related Questions