简体   繁体   English

使用Xamarin Forms nuget Xam.Plugin.Media用相机拍照

[英]Taking a photo with camera using Xamarin Forms nuget Xam.Plugin.Media

I just used NUGET and installed Xam.Plugin.Media so that I can take photos using my mobile app. 我只是使用NUGET并安装了Xam.Plugin.Media,以便可以使用移动应用程序拍照。

I wrote the following code in the click event of the button as per the sample code in the xamarin component website: 我按照xamarin组件网站中的示例代码在按钮的click事件中编写了以下代码:

private async void btnTake1_Clicked(object sender, EventArgs e)
        {
            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                await DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
                return;
            }

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

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

            if (file == null)
                return;

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

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

        }

All went well and the Solution is building successfully, I am running only UWP now. 一切顺利,解决方案正在成功构建,我现在仅运行UWP。 But when I click the button it breaks at some location in 但是当我单击按钮时,它在

if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();

Can somebody help me with this? 有人可以帮我吗? Thanks in advance. 提前致谢。 Sarin Gopalan 萨林·戈帕兰(Sarin Gopalan)

As per your comment the following applies to Xamarin.Forms and UWP 根据您的评论,以下内容适用于Xamarin.Forms和UWP

In your forms project, wherever it is you want to dispaly the gallery picker (we are doing it from a button click) you would use something like the below: 在您的Forms项目中,无论您要在何处显示图库选择器(我们通过单击按钮进行操作),都可以使用类似以下的内容:

private async void ChooseExistingClicked(object sender, EventArgs e)
{
    bool hasPermission = false;

    try
    {
        await CrossMedia.Current.Initialize();
        hasPermission = CrossMedia.Current.IsPickPhotoSupported;
    }
    catch (Exception genEx)
    {
        var Error = genEx;
    }

    if (!hasPermission)
    {
        await DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
        return;
    }

    var file = await CrossMedia.Current.PickPhotoAsync();

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

You also need to set the permissions up correctly. 您还需要正确设置权限。 So in your UWP project, you will see a file called 'Package.appxmanifext' double click on this, and select 'Capabilities' and make sure that 'Picture Library' in the list is ticked. 因此,在您的UWP项目中,您将看到一个名为“ Package.appxmanifext”的文件,双击该文件,然后选择“功能”,并确保已选中列表中的“图片库”。

That's it, that should be all you need to do. 就是这样,这应该是您需要做的所有事情。

EDIT: As requested below are the places you would need to set permissions for the native platforms to access the photo gallery. 编辑:根据下面的请求,您将需要为本地平台设置访问照片库的权限。

In your iOS project you will need to open the code of the 'Info.plist' file which will show you an XML sheet. 在您的iOS项目中,您需要打开“ Info.plist”文件的代码,该文件将显示XML表格。 you need to add: 您需要添加:

<key>NSCameraUsageDescription</key>
    <string></string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This is used to upload an image of concern along with your report</string>
    <key>NSMicrophoneUsageDescription</key>
    <string></string>

anywhere in between the tags. 标签之间的任何位置。

For your Android project you need to right click the project and select 'properties' this will open a new window, and on the left you need to select 'Android Manifest' then ensure that 'CAMERA', 'READ_EXTERNAL_STORAGE', and 'WRITE_EXTERNAL_STORAGE'. 对于您的Android项目,您需要右键单击该项目并选择“属性”,这将打开一个新窗口,在左侧,您需要选择“ Android清单”,然后确保“ CAMERA”,“ READ_EXTERNAL_STORAGE”和“ WRITE_EXTERNAL_STORAGE” 。 I believe those are the main one's for both gallery and camera access. 我相信这些是画廊和摄影机访问的主要工具。

Have you tried to InitializeComponent like the documentation on github? 您是否尝试过像github上的文档一样初始化InitializeComponent?

await CrossMedia.Current.Initialize();

https://github.com/jamesmontemagno/MediaPlugin https://github.com/jamesmontemagno/MediaPlugin

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

相关问题 Xam.Plugin.Media错误Xamarin表单 - Xam.Plugin.Media Error Xamarin Forms VS Xamarin.Forms (Android):相机和存储访问被 Xam.Plugin.Media 拒绝 - VS Xamarin.Forms (Android): Camera and Storage access denied with Xam.Plugin.Media Xamarin Forms 和 xam.plugin.media:无法解析对 Xamarin.Essentials.Permissions 的引用 - Xamarin Forms and xam.plugin.media: could not resolve reference to Xamarin.Essentials.Permissions Xamarin Xam.Plugin.Media无法计算出如何访问相机 - Xamarin Xam.Plugin.Media can't work out how to access camera Xamarin.forms-Xam.Plugin.Media投掷错误此组件的可移植版本中未实现此功能 - Xamarin.forms - Xam.Plugin.Media throwing error This functionality is not implemented in the portable version of this assembly 如何使用 plugin.media for xamarin.forms 正确调整照片大小? - How to correctly resize photo using plugin.media for xamarin.forms? 添加 Xam.Plugin.Connectivity 后的 Xamarin Forms GooglePlayconsole 在签署 APK 用户功能时发出警告 - Xamarin Forms After Adding Xam.Plugin.Connectivity GooglePlayconsole raise warning on signing APK user features Xamarin Forms - 媒体插件 - 手机上的空缩略图 - Xamarin Forms - Media Plugin - Empty thumbnails on phone 在 xamarin.forms 中拍照后手机挂起 - Phone Hangs after taking Photo in xamarin.forms Cordova相机插件:拍照时不显示照片 - Cordova camera plugin: Photo is not displayed when taking a picture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM