iosxamarin.formsxamarin.iosios-camera

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


We are using a custom camera in our application. I have implemented the custom camera using the below-mentioned link. When clicking the capture button the camera preview is stopped and it's working fine but how can I get the captured image data and I want to save the image in the gallery and return the path. I have got the captured image data in android by using PictureCallback. Please help me with this.

Custom Camera Link: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/view


Solution

  • Have a look at this blog. To get the image data in the AVCapturePhotoCaptureDelegate, I added AVCapturePhotoOutput photoOutput. Here is an example:

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

    I haven't translated all of the swift code to c#, feel free to ask me if you have any questions.