简体   繁体   English

Xamarin.forms 如何添加拍照功能或从图库中选择照片

[英]Xamarin.forms how I can add ability to take photo or select it from Gallery

I am Using Xamarin.Forms , how I can add ability to take photo or select it from Gallery.我正在使用Xamarin.Forms ,如何添加拍照功能或从图库中选择它。 I Find a lot of solutions, but no one of them is completed.我找到了很多解决方案,但没有一个是完整的。

the Xamarin Forms Media plugin will do this, and includes sample code Xamarin Forms Media 插件将执行此操作,并包含示例代码

takePhoto.Clicked += async (sender, args) =>
        {

          if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.PhotosSupported)
          {
            DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
            return;
          }

          var file = await CrossMedia.Current.TakePhotoAsync(new Media.Plugin.Abstractions.StoreCameraMediaOptions
            {

              Directory = "Sample",
              Name = "test.jpg"
            });

          if (file == null)
            return;

          DisplayAlert("File Location", file.Path, "OK");

          image.Source = ImageSource.FromStream(() =>
          {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
          }); 
        };

This can now be easily achieved using Xamarin Essentials MediaPicker.现在可以使用 Xamarin Essentials MediaPicker 轻松实现这一点。

Here is the link to the docs and sample code for Android, iOS and UWP..这是 Android、iOS 和 UWP 的文档和示例代码的链接。

https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android

        var profile = new Image { };
        profile.Source = "profile.png";
        profile.HorizontalOptions = LayoutOptions.StartAndExpand;
        profile.VerticalOptions = LayoutOptions.StartAndExpand;

        var profiletap = new TapGestureRecognizer();

        profiletap.Tapped += async (s, e) =>
        {
           var file = await CrossMedia.Current.PickPhotoAsync();
            if (file == null)
                return;
              await DisplayAlert("File Location", file.Path, "OK");

            im = ImageSource.FromStream(() =>
            {
               var stream = file.GetStream();
                //file.Dispose();
                return stream;

            });

            profile.Source = im;


    //   await Navigation.PushModalAsync(new PhotoPage(im));
        };

        profile.GestureRecognizers.Add(profiletap);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 能够拍摄或 Select 照片来自 Xamarin 中的图库 - Ability to Take or Select Photo from Gallery in Xamarin 如何截取我的 Xamarin.Forms 应用程序的部分屏幕截图并将其保存到我的库中? - How can I take a partial screenshot of my Xamarin.Forms app and save it into my gallery? 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML 如何从 Xamarin.Forms 获得连续的地理定位? - How can I get a continous geolication from Xamarin.Forms? 如何以 xamarin 形式从图库中选择图像? - How to select an image from gallery in xamarin forms? 如何在10秒后正确创建自己的功能以Xamarin形式拍摄照片? - How can I correctly create my own function to take a photo in Xamarin forms after 10 seconds? 如何在xamarin.forms中将UIImageView添加到UIAlertView? - How do I add UIImageView to UIAlertView in xamarin.forms? 从 Xamarin.Forms 中 JSON 文件中的 URL 加载照片 - Photo loading from URL in JSON file in Xamarin.Forms 如何在Xamarin.Forms中为ToolbarItem创建自定义渲染器? - How can I make a custom renderer for ToolbarItem in Xamarin.Forms? 如何在Xamarin.Forms中为轮播实现活动指示器? - How can I implement an Activity Indicator for a Carousel in Xamarin.Forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM