为什么AVCaptureSession的图像非常暗

时间:2017-05-16 10:33:34

标签: xamarin xamarin.ios avcapturesession

我们有Xamarin Forms解决方案,在iOS项目中,我们尝试在按钮点击上创建照片。问题是图像很暗。它几乎是黑色的。为什么会这样?这是一个代码:

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);
    }
}

这是图片: enter image description here

1 个答案:

答案 0 :(得分:1)

行:

_captureSession.AddOutput(output);

应该在点击按钮之前(在StartRunning之前)。通过该更改,图像具有正常亮度,并且也不需要旋转。