为什么在iOS上使用Xamarin Forms进行实时摄像机捕捉控制冻结?

问题描述 投票:1回答:1

[我从GitHub下载了Xamarin Moments的源代码,现在我正试图将CameraPage rendererCameraPage转换为Page

然后,我重构了代码,使其成为ContentView渲染器。实时预览和图像捕获的大多数实际设置来自Moments应用,并在需要/首选的地方进行了一些重构。

显示实时预览,但是当我按下按钮拍照时,应用程序毫无例外地冻结,甚至在Xcode的控制台视图中也没有。

ContentView

这是我设置实时预览的方式

//this is how it's called: 
btnTakePicture.Clicked += (s,e)=> { GetCameraImage().Wait(); };

// this method freezes
public async Task<byte[]> GetCameraImage()
{
    byte[] imageBuffer = null;

    if (captureDeviceInput != null)
    { 
        var videoConnection = stillImageOutput.ConnectionFromMediaType(AVMediaType.Video);
        Console.WriteLine("[HASFIQWRPPOA] This message shows up");

// this is where the app freezes, even though the live preview still moves.
        var sampleBuffer = await stillImageOutput.CaptureStillImageTaskAsync(videoConnection);
        Console.WriteLine("[CLKJFADSFQXW] THIS DOESN'T SHOW UP");

        // var jpegImageAsBytes = AVCaptureStillImageOutput.JpegStillToNSData (sampleBuffer).ToArray ();
        var jpegImageAsNsData = AVCaptureStillImageOutput.JpegStillToNSData(sampleBuffer);
        Console.WriteLine("[ROIAJDGNQWTG]");
        // var image = new UIImage (jpegImageAsNsData);
        // var image2 = new UIImage (image.CGImage, image.CurrentScale, UIImageOrientation.UpMirrored);
        // var data = image2.AsJPEG ().ToArray ();
        imageBuffer = jpegImageAsNsData.ToArray();
        Console.WriteLine("[FIOUJGAIDGUQ] Image buffer: "+imageBuffer.Length);
    } 

    if (imageBuffer != null && imageBuffer.Length > 100)
    {
        using (var ms = new MemoryStream(imageBuffer))
        {
            var uiimg = UIImage.LoadFromData(NSData.FromStream(ms)); 
            this.Add(new UIImageView(uiimg)); 
        }
    }

    return imageBuffer;
}
xamarin xamarin.ios xamarin.forms
1个回答
1
投票

我遇到了这个问题,结果发现我陷入僵局,因为同时使用// This method runs fine and the camera preview is started as expected public void SetupLiveCameraStream() { try { // add a UIView to the renderer liveCameraStream = new UIView() { Frame = new CGRect(0f, 0f, Element.Width, Element.Height), }; this.Add(liveCameraStream); // find a camera var captureDevice = AVCaptureDevice.DefaultDeviceWithMediaType(AVMediaType.Video); if (captureDevice != null) { Console.WriteLine("[ZKSDJGWEHSY] Capture device found"); // not the case on simulator captureSession = new AVCaptureSession(); videoPreviewLayer = new AVCaptureVideoPreviewLayer(captureSession) { Frame = liveCameraStream.Bounds }; liveCameraStream.Layer.AddSublayer(videoPreviewLayer); ConfigureCameraForDevice(captureDevice); captureDeviceInput = AVCaptureDeviceInput.FromDevice(captureDevice); var dictionary = new NSMutableDictionary(); dictionary[AVVideo.CodecKey] = new NSNumber((int)AVVideoCodec.JPEG); stillImageOutput = new AVCaptureStillImageOutput() { OutputSettings = new NSDictionary() }; captureSession.AddInput(captureDeviceInput); captureSession.AddOutput(stillImageOutput); captureSession.StartRunning(); Console.WriteLine("[OIGAJGUWRJHWY] Camera session started"); } else { Console.WriteLine("[OASDFUJGOR] Could not find a camera device"); } } catch (Exception x) { Console.WriteLine("[QWKRIFQEAHJF] ERROR:" + x); } } / asyncawait。猜测您可能会遇到与Task.Result的用法类似的情况。

代码的两个部分:

Task.Wait()

和:

btnTakePicture.Clicked += await (s,e) => { GetCameraImage().Wait(); };
© www.soinside.com 2019 - 2024. All rights reserved.