2

We have Xamarin Forms solution and in iOS project we are trying to create photo on button click. Problem is image is very dark. It is almost black. Why is this happening? Here is a code:

var _captureSession = new AVCaptureSession();
var _captureDevice = AVCaptureDevice.GetDefaultDevice(AVMediaType.Video);
var _captureDeviceInput = AVCaptureDeviceInput.FromDevice(_captureDevice);
_captureSession.AddInput(_captureDeviceInput);
_captureSession.StartRunning();

private async void OnButtonClick()
{
    var output = new AVCaptureStillImageOutput { OutputSettings = new NSDictionary(AVVideo.CodecKey, AVVideo.CodecJPEG) };
    _captureSession.AddOutput(output);

    var buffer = await output.CaptureStillImageTaskAsync(output.Connections[0]);
    NSData data = AVCaptureStillImageOutput.JpegStillToNSData(buffer);

    UIImage image = UIImage.LoadFromData(data);
    //image = RotateImage(image);
    NSData imageData = image.AsPNG();
    byte[] byteArray = imageData.ToArray();

    IFolder folder = FileSystem.Current.LocalStorage;
    IFile file = await folder.CreateFileAsync("image.png", CreationCollisionOption.ReplaceExisting);

    using (Stream stream = await file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite))
    {
        stream.Write(image, 0, image.Length);
    }
}

Here is image: enter image description here

Uros
  • 2,099
  • 7
  • 23
  • 50

1 Answers1

2

Line:

_captureSession.AddOutput(output);

should go before button click (before StartRunning). With that change image has normal brightness and also rotation is not needed.

Uros
  • 2,099
  • 7
  • 23
  • 50
  • Thanks. This weird black magic actually worked for me. Though i still need rotation since iOS just does metadata rotation of the picture and my thumbnail renderer ignores the metadata. https://stackoverflow.com/questions/22431813/avcapturestillimageoutput-image-returning-upside-down – Menace Jul 31 '19 at 19:33