Forum Xamarin.Forms

Cross Platform Camera / Photo Picker

MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
edited April 2017 in Xamarin.Forms

Summary

Please create a library to leverage the device's native camera app and give the user access to select a photo from their photo gallery.

API Changes

Create a class called Camera, or CameraApp. It should have a method called OpenCamera(). It should have an event called PictureTaken. The app should then be able to pick up the file as long as they have access to the file.

Create a class called PhotoPicker which opens the app for selecting photos on the device. This should have an event called PictureSelected. The app should then be able to pick up the file as long as they have access to the file.

Intended Use Case

A user wants to take or select a photo.

Open · Last Updated

Posts

  • AceCoderLauraAceCoderLaura AUMember ✭✭

    Using the task-await pattern would also be acceptable.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    Yes. The task based async pattern would be preferable to the event driven pattern.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    I don't think this is needed, just use the MediaPlugin: https://github.com/jamesmontemagno/MediaPlugin

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited April 2017

    This is not a supported library by Xamarin.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    Also, the samples don't compile at all.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭✭

    Well, it kind of is, since James Montemagno works for Xamarin.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    So, then I guess that I would ask the Xamarin Forms teams, and @JamesMontemagno to come to an agreement whereby this library is added to the core Xamarin Forms library.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    Also, I haven't been able to get taking photos working on UWP. I get a NotImplementedException.

  • JesseBoyntonJesseBoynton USMember

    Gets my vote for inclusion! @JamesMontemagno what do you think?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    This library will need some cleanup before it becomes part of the core Xamarin Forms library, but can we can get some comments from the Xamarin Forms team?

    If we don't hear something soon, I'm going to have to fork the repo and maintain our own version of the library in order to use this functionality.

    This is basic functionality. Xamarin Forms users shouldn't have to create their own library every time they want to do something as basic as use a camera.

  • Britton_BeckhamBritton_Beckham USMember ✭✭

    I agree with @MelbourneDeveloper in that you'd think XF would have this packaged as part of base library. One of the arguments from a different thread for not adding something (I think it was a checkbox control) was that it wasn't supported on all platforms. There are a lot of peripheral usage scenarios used be devs today that would be very nice in XF that are common to all platforms. I am currently using Plugin.CrossMedia from @JamesMontemagno and I really like it. I actually pulled his CrossPermissions code down last night to trouble shoot an NullRef issue (turns out it was a deprecated method from Xing.Net library). I think if XF started with the CrossMedia/CrossPermissions library and incorporated (and expanded) the functionality, the would have a very nice launching point to get this into XF. Thanks guys!

  • kenneth.leekenneth.lee USMember ✭✭✭

    I second Britton, @JamesMontemagno 's Plugin.CrossMedia works wonders and when I first used it i always wondered why it wasn't packaged together with XF. There probably is some stuff that might need to be cleaned up but I too would love to see this in XF.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited May 2017

    @Britton_Beckham and @kenneth.lee,

    I haven't heard anything from @JamesMontemagno , so I am combining two libraries here:

    https://github.com/MelbourneDeveloper/Adapt.Presentation
    https://github.com/MelbourneDeveloper/Adapt.Presentation.git

    The library will include code from James' MediaPlugin (https://github.com/jamesmontemagno/MediaPlugin) and, this library https://github.com/Studyxnet/FilePicker-Plugin-for-Xamarin-and-Windows

    The reason I am doing this is to split up the libraries in to a clearer separation of platforms, implement the functionality that is not yet implemented on all platforms, and remove static methods that attempt to cover up dependency injection. I will also be pulling in bits and pieces of other libraries floating around on the internet that should be a part of the Xamarin Forms core library but aren't.

    Note: I'm only at the early stages. I've tested the code in my own solution, but I have not got working samples inside this repo yet. I will keep people posted on this, and I will be welcoming pull requests once the initial design is bedded down.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited May 2017

    As of right now, I have a working sample for Android of this library. It's ended up being a combination of several of James' repos. This is some sample code:

        private async void TakePhotoButton_Clicked(object sender, EventArgs e)
        {
            const string defaultFileName = "New Photo.jpg";
            var media = App.PresentationFactory.CreateMedia(App.CurrentPermissions);
            var filePicker = App.PresentationFactory.CreateFilePicker();
    
            if (!media.IsCameraAvailable || !media.IsTakePhotoSupported)
            {
                await DisplayAlert("No Camera", ":( No camera available.", "OK");
                return;
            }
    
            using (var mediaFile = await media.TakePhotoAsync(new StoreCameraMediaOptions
            {
                PhotoSize = PhotoSize.Medium,
                Directory = "Adapt Sample App",
                Name = defaultFileName
            }))
            {
                using (var readFileStream = mediaFile.GetStream())
                {
                    var fileTypes = new Dictionary<string, IList<string>>();
                    fileTypes.Add("Jpeg Image", new List<string> { ".jpg" });
    
                    using (var fileData = await filePicker.PickAndOpenFileForWriting(fileTypes, defaultFileName))
                    {
                        var readBuffer = new byte[readFileStream.Length];
                        await readFileStream.ReadAsync(readBuffer, 0, (int)readFileStream.Length);
                        fileData.FileStream.Write(readBuffer, 0, readBuffer.Length);
                    }
                }
            }
        }
    

    This sample code is from a .NET Standard library and sits across all platforms. It uses dependency injection with the factory pattern so that objects are created on the fly by the platform specific version of the code. This is instead of trying to put a static layer over the top of the dependency injection.

    You can clone the repo here:
    https://github.com/MelbourneDeveloper/Adapt.Presentation.git

    The commit that I just tested on is:
    5a8915ad73c8c3e64e232f3e76540a17e86482c4

Sign In or Register to comment.