Xamarin Community Toolkit cameraview Save file

Alessandro Caliaro 4,181 Reputation points
2021-09-01T15:52:31.337+00:00

Hi

How can I save the image captured with XamarinCommunityToolkit CameraView?

    private void cameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)
    {

    }

Here I have e.Image and e.ImageData, I would like to save this on a file.
I would also set the resolution of the image. Is it possible?
Thanks
Alessandro

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} vote

Accepted answer
  1. Kyle Wang 5,531 Reputation points
    2021-09-02T05:13:13.227+00:00

    Hi alessandrocaliaro,

    Welcome to our Microsoft Q&A platform!

    To save the image as file, you can create an interface as follows and implement it on both Android and iOS.

    public interface ISaveService  
    {  
        void SaveFile(string fileName, byte[] data);  
    }  
    

    Android:

    [assembly: Xamarin.Forms.Dependency(typeof(SaveService))]  
    namespace cameraviewDemo.Droid  
    {  
        public class SaveService: ISaveService  
        {  
            void ISaveService.SaveFile(string fileName, byte[] data)  
            {  
                string picPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryPictures);  
                string filePath = Path.Combine(picPath, fileName);  
                File.WriteAllBytes(filePath, data);  
            }  
        }  
    }  
    

    iOS:

    [assembly: Xamarin.Forms.Dependency(typeof(SaveService))]  
    namespace cameraviewDemo.iOS  
    {  
        class SaveService: ISaveService  
        {  
            void ISaveService.SaveFile(string fileName, byte[] data)  
            {  
                var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  
                var filename = Path.Combine(documents, fileName);  
                File.WriteAllBytes(filename, data);  
            }  
        }  
    }  
    

    xaml.cs:

    private void cameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)  
    {  
        byte[] image = e.ImageData;  
        DependencyService.Get<ISaveService>().wirteFile("test.jpg", image);  
    }  
    

    Here are some related links you can refer to.

    How to write a file to a Internal Storage in xamarin forms which can be found by the user
    File system access in Xamarin.iOS

    As to set the resolution, you can try to convert the e.ImageData to Bitmap and try method Bitmap.SetResolution(Single, Single) Method and Bitmap.CreateBitmap Method.

    Regards,
    Kyle


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful