简体   繁体   English

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

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

ill take any help here.. I am using xamarin and would like to:我会在这里寻求帮助。我正在使用 xamarin 并希望:

  1. Click on button点击按钮
  2. Open Camera开放式摄像头
  3. Take photo拍照
  4. Crop Photo裁剪照片
  5. Create Folder in Gallery在图库中创建文件夹
  6. Save Cropped photo in new folder in Gallery将裁剪的照片保存在图库中的新文件夹中

I am following tutorial - Ref: https://github.com/stormlion227/ImageCropper.Forms我正在关注教程 -参考: https://github.com/stormlion227/ImageCropper.Forms

Currently, When I tap on button, Camera opens up, but it doesnt crops or saves photo.目前,当我点击按钮时,相机会打开,但不会裁剪或保存照片。 Since Camera opens up, that means my set up and perrmission are correct.由于相机打开,这意味着我的设置和权限是正确的。

Why this code isnt working from github and how can I crop photo?为什么此代码不能从 github 工作,我该如何裁剪照片? Please let me know what am I missing here.请让我知道我在这里缺少什么。

after doing some debugging, following code is not getting runned.进行一些调试后,以下代码未运行。 i this issue is with properties我这个问题与属性有关

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

view back-end查看后端

    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
}

Other Info: I downloaded 2 nugets: ImageCropper.Forms.Fix.v7 and Xam.Media.Plugin其他信息:我下载了 2 个 nuget: ImageCropper.Forms.Fix.v7Xam.Media.Plugin

The plugin you used is too old.您使用的插件太旧了。 You could use ImageCropper.Forms.Fix.v2 instead.您可以改用ImageCropper.Forms.Fix.v2

Add the code below in your MainActivity:在您的 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);
    }

Add the code in tag of AndroidManifest.xaml:在AndroidManifest.xaml标签中添加代码:

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

Code behind:后面的代码:

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

When you click the button, it would pop up a window.当你点击按钮时,它会弹出一个 window。 If you want to take photo, choose the Take Photo .如果要拍照,请选择Take Photo Please note, you need to add the CAMERA permission.请注意,您需要添加CAMERA权限。

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

在此处输入图像描述

Or you could select the image from the device to crop.或者您可以 select 从设备中裁剪图像。

在此处输入图像描述

OutPut: OutPut:

https://imgur.com/okWKHfk https://imgur.com/okWKHfk

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

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