繁体   English   中英

android - 如何使用 ImageCropper.Forms 拍照和裁剪?

[英]android - How to take a photo and crop using ImageCropper.Forms?

我会在这里寻求帮助。我正在使用 xamarin 并希望:

  1. 点击按钮
  2. 开放式摄像头
  3. 拍照
  4. 裁剪照片
  5. 在图库中创建文件夹
  6. 将裁剪的照片保存在图库中的新文件夹中

我正在关注教程 -参考: https://github.com/stormlion227/ImageCropper.Forms

目前,当我点击按钮时,相机会打开,但不会裁剪或保存照片。 由于相机打开,这意味着我的设置和权限是正确的。

为什么此代码不能从 github 工作,我该如何裁剪照片? 请让我知道我在这里缺少什么。

进行一些调试后,以下代码未运行。 我这个问题与属性有关

 Device.BeginInvokeOnMainThread(() =>
                    {
                        ImageURL.Source = ImageSource.FromFile(imageFile);
                    });

查看后端

    public partial class AddCardPage : ContentPage
    {
        public AddCardPage()
        {
            InitializeComponent();
            CrossMedia.Current.Initialize();
        }

        protected async void TapGestureRecognizerCommand(object sender, EventArgs e)
        {
            try
            {
                await CrossMedia.Current.Initialize();


                await new ImageCropper()
                {
                    PageTitle = "Test Title",
                    AspectRatioX = 1,
                    AspectRatioY = 1,
                    CropShape = ImageCropper.CropShapeType.Rectangle, //Cropt shape
                    SelectSourceTitle = "Select source",
                    TakePhotoTitle = "Take Photo",
                    PhotoLibraryTitle = "Photo Library",
                    Success = (imageFile) =>
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            ImageURL.Source = ImageSource.FromFile(imageFile);
                        });
                    }
                }.Show(this);
            }
            catch (Exception ex)
            {
                //System.Diagnostics.Debug.WriteLine("CameraException:>" + ex);
            }
        }//end of method

    }//end of class
}

其他信息:我下载了 2 个 nuget: ImageCropper.Forms.Fix.v7Xam.Media.Plugin

您使用的插件太旧了。 您可以改用ImageCropper.Forms.Fix.v2

在您的 MainActivity 中添加以下代码:

   Stormlion.ImageCropper.Droid.Platform.Init();
   protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Stormlion.ImageCropper.Droid.Platform.OnActivityResult(requestCode, resultCode, data);
    }

在AndroidManifest.xaml标签中添加代码:

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity" android:theme="@style/Base.Theme.AppCompat" />

后面的代码:

 new ImageCropper()
        {
            //                PageTitle = "Test Title",
            //                AspectRatioX = 1,
            //                AspectRatioY = 1,
            Success = (imageFile) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    image.Source = ImageSource.FromFile(imageFile);
                });
            }
        }.Show(this);

当你点击按钮时,它会弹出一个 window。 如果要拍照,请选择Take Photo 请注意,您需要添加CAMERA权限。

<uses-permission android:name="android.permission.CAMERA" />

在此处输入图像描述

或者您可以 select 从设备中裁剪图像。

在此处输入图像描述

OutPut:

https://imgur.com/okWKHfk

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM