简体   繁体   中英

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

ill take any help here.. I am using xamarin and would like to:

  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

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? 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

The plugin you used is too old. You could use ImageCropper.Forms.Fix.v2 instead.

Add the code below in your 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:

<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. If you want to take photo, choose the Take Photo . Please note, you need to add the CAMERA permission.

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

在此处输入图像描述

Or you could select the image from the device to crop.

在此处输入图像描述

OutPut:

https://imgur.com/okWKHfk

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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