如何在 iOS xamarin 表单中获取自定义渲染器相机的捕获图像

How to get captured image of custom renderer camera in iOS xamarin forms

我们在应用程序中使用自定义相机。我已经使用下面提到的 link 实现了自定义相机。单击捕获按钮时,相机预览停止并且工作正常但我如何获取捕获的图像数据并且我想将图像保存在图库和 return 路径中。我使用 PictureCallback 在 android 中获取了捕获的图像数据。请帮我解决这个问题。

自定义相机 Link: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view

看看this blog。 为了获取 AVCapturePhotoCaptureDelegate 中的图像数据,我添加了 AVCapturePhotoOutput photoOutput。这是一个例子:

public class UICameraPreview : UIView
{
    AVCaptureVideoPreviewLayer previewLayer;
    CameraOptions cameraOptions;

    AVCapturePhotoOutput photoOutput;

    public event EventHandler<EventArgs> Tapped;

    public AVCaptureSession CaptureSession { get; private set; }

    public bool IsPreviewing { get; set; }

    public UICameraPreview (CameraOptions options)
    {
        cameraOptions = options;
        IsPreviewing = false;
        Initialize ();
    }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        if (previewLayer != null)
            previewLayer.Frame = Bounds;
    }

    public override void TouchesBegan (NSSet touches, UIEvent evt)
    {
        base.TouchesBegan (touches, evt);
        OnTapped ();
    }

    protected virtual void OnTapped ()
    {
        var eventHandler = Tapped;
        if (eventHandler != null) {
            eventHandler (this, new EventArgs ());
        }
    }

    void Initialize ()
    {
        CaptureSession = new AVCaptureSession ();
        previewLayer = new AVCaptureVideoPreviewLayer (CaptureSession) {
            Frame = Bounds,
            VideoGravity = AVLayerVideoGravity.ResizeAspectFill
        };

        //self.photoOutput.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)

        photoOutput = new AVCapturePhotoOutput();
        
        CaptureSession.AddOutput(photoOutput);

        photoOutput.CapturePhoto(AVCapturePhotoSettings.Create(),new myDelegate());

        var videoDevices = AVCaptureDevice.DevicesWithMediaType (AVMediaType.Video);
        var cameraPosition = (cameraOptions == CameraOptions.Front) ? AVCaptureDevicePosition.Front : AVCaptureDevicePosition.Back;
        var device = videoDevices.FirstOrDefault (d => d.Position == cameraPosition);

        if (device == null) {
            return;
        }

        NSError error;
        var input = new AVCaptureDeviceInput (device, out error);
        CaptureSession.AddInput (input);
        Layer.AddSublayer (previewLayer);
        CaptureSession.StartRunning ();
        IsPreviewing = true;
    }
}

public class myDelegate : AVCapturePhotoCaptureDelegate {

    public override void DidFinishCapture(AVCapturePhotoOutput captureOutput, AVCaptureResolvedPhotoSettings resolvedSettings, NSError error)
    {
        base.DidFinishCapture(captureOutput, resolvedSettings, error);
    }

    public override void DidFinishProcessingPhoto(AVCapturePhotoOutput output, AVCapturePhoto photo, NSError error)
    {
        base.DidFinishProcessingPhoto(output, photo, error);

        if (error == null)
        {
            var photodata = photo.FileDataRepresentation;
        }
    }
}

我还没有将所有 swift 代码都翻译成 c#,如果您有任何问题,请随时问我。