Xamarin: How to Capture Image DATA (not only for display) on portable app

599 views Asked by At

I need to capture an image from my camera using xamarin.forms portable, and obtain access to the image byte[] data for image processing purposes.

How can this be done?

I have the working code that captures the image and simply shows it, using xlabs

public async Task<MediaFile> TakePicture()
    {
        Setup ();

        ImageSource = null;

        return await _Mediapicker.TakePhotoAsync (new CameraMediaStorageOptions {
            DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400
        }).ContinueWith (t => {
            if (t.IsFaulted)
            {
                Status = t.Exception.InnerException.ToString();
            }
            else if (t.IsCanceled)
            {
                Status = "Canceled";
            }
            else
            {
                MediaFile mediaFile = t.Result;
                ImageSource = ImageSource.FromStream(() => mediaFile.Source);

                return mediaFile;
            }

            return null;
        }, _scheduler);
    }

and

    private async void buttonTakePicture_Clicked() {
        await cameraViewModel.TakePicture();
        imageView.Source = cameraViewModel.ImageSource;
    }

clicking the button launches cameraViewModel.TakePicture() which in turn uses xlabs to actually take the picture on the device.

How can I alter the code to also give me the image raw data (or use a different code altogether)?

Thanks

2

There are 2 answers

2
Lucas Moura Veloso On

You can take mediaFile.Path and pass it as a parameter for a DependencyService:

//For Android
public byte[] ByteArrayFromStream(string path)
{
    var bitmap = BitmapFactory.DecodeFile(path);

    byte[] bitmapData;
    using(stream = new MemoryStream())
    {
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
        bitmapData = stream.ToArray();
    }

    return bitmapData;
}

//For iOS
public byte[] ByteArrayFromStream(string path)
{
    UIImage originalImage = UIImage.FromFile(path);
    return originalImage.AsPNG().ToArray();
}

To learn more about DependencyService you can access the documentation at: https://developer.xamarin.com/guides/xamarin-forms/dependency-service/

1
Jay Patel On

This will help you to get byte[ ] in PCL :

public async Task<MediaFile> TakePicture()
{
    Setup ();
    ImageSource = null;
    return await _Mediapicker.TakePhotoAsync (new CameraMediaStorageOptions {
        DefaultCamera = CameraDevice.Front, MaxPixelDimension = 400
    }).ContinueWith (t => {
        if (t.IsFaulted)
        {
            Status = t.Exception.InnerException.ToString();
        }
        else if (t.IsCanceled)
        {
            Status = "Canceled";
        }
        else
        {
            MediaFile mediaFile = t.Result;
            using (MemoryStream ms = new MemoryStream())
            {
                mediaFile.Source.CopyTo(ms);
                byte[] attchmentbytes = ms.ToArray();
            }
            ImageSource = ImageSource.FromStream(() => mediaFile.Source);
            return mediaFile;
        }
        return null;
    }, _scheduler);
}