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 个 nugets: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 从设备裁剪图像。

输出:

https://imgur.com/okWKHfk