【问题标题】:Crop and save the image裁剪并保存图像
【发布时间】:2020-11-04 18:14:39
【问题描述】:

我根据需要使用了以下方法: 1-我想打开相机,选择图像,裁剪图像并将图像保存在imageview中 2- 在 imageview 中裁剪并保存设备内保存的图像

错误

打开相机

打开画廊

完整的项目 https://drive.google.com/file/d/1tUSTyiq5qUPpaY6z2Jd1m2tv_HRE-QuL/view?usp=sharingusp=sharing

 public class MainActivity : AppCompatActivity
    {
        ImageView imageView;
        Android.Support.V7.Widget.Toolbar toolbar;
        File file;
        Android.Net.Uri uri;
        Intent CamIntent, GalIntent, CropIntent;
        const int RequestPermissionCode = 1;



        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.Main);

            toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
            toolbar.Title = "Crop Image";
            toolbar.SetTitleTextColor(Android.Graphics.Color.White);
            SetSupportActionBar(toolbar);

       

            imageView = FindViewById<ImageView>(Resource.Id.imageView);
            int permissionCheck = (int)ContextCompat.CheckSelfPermission(this, Manifest.Permission.Camera);
            if (permissionCheck == (int)Permission.Denied)
                RequestRuntimePermission();

        }

        private void RequestRuntimePermission()
        {
            if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.Camera))
                Toast.MakeText(this, "CAMERA permission will allows us to access CAMERA app", ToastLength.Short).Show();
            else
                ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.Camera }, RequestPermissionCode);

        }

            public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater.Inflate(Resource.Menu.menu_main, menu);
            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            if (item.ItemId == Resource.Id.btn_camera)
                CameraOpen();

            else if (item.ItemId == Resource.Id.btn_gallery)

                GalleryOpen();
            return true;
        }

        private void GalleryOpen()
        {
            GalIntent = new Intent(Intent.ActionPick, MediaStore.Images.Media.ExternalContentUri);
            StartActivityForResult(Intent.CreateChooser(GalIntent, "Select image from Gallery"), 2);
        }

        private void CameraOpen()
        {
            CamIntent = new Intent(MediaStore.ActionImageCapture);
            file = new File(Android.OS.Environment.ExternalStorageDirectory, "file_image" + DateTime.Now + ".jpg");
            uri = Android.Net.Uri.FromFile(file);
            CamIntent.PutExtra(MediaStore.ExtraOutput, uri);
            CamIntent.PutExtra("return-data", true);
            StartActivityForResult(CamIntent, 0);
        }

        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            if (requestCode == 0 && resultCode == Result.Ok)
                CropImage();
            else if (requestCode == 2)
            {
                if (data != null)
                {
                    uri = data.Data;
                    CropImage();
                }
            }
            else if (requestCode == 1)
            {
                if (data != null)
                {
                    Bundle bundle = data.Extras;
                    Bitmap bitmap = (Bitmap)bundle.GetParcelable("data");
                    imageView.SetImageBitmap(bitmap);
                }
            }
        }

        private void CropImage()
        {
            try
            {
                CropIntent = new Intent("com.android.camera.action.CROP");
                CropIntent.SetDataAndType(uri, "image/*");

                CropIntent.PutExtra("crop", "true");
                CropIntent.PutExtra("outputX", 180);
                CropIntent.PutExtra("outputY", 180);
                CropIntent.PutExtra("aspectX", 3);
                CropIntent.PutExtra("aspectY", 4);
                CropIntent.PutExtra("scaleUpIfNeeded", true);
                CropIntent.PutExtra("return-data", true);

                StartActivityForResult(CropIntent, 1);
            }
            catch 
            {

            }
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            switch (requestCode)
            {
                case RequestPermissionCode:
                    {
                        if (grantResults.Length > 0 && grantResults[0] == Permission.Granted)
                            Toast.MakeText(this, "Permission Granted", ToastLength.Short).Show();
                        else
                            Toast.MakeText(this, "Permission Canceled", ToastLength.Short).Show();
                    }
                    break;
            }
        }
    }

【问题讨论】:

    标签: xamarin.android


    【解决方案1】:

    1-我要打开相机,选择图片,裁剪并保存在imageview中

    2- 在 imageview 中裁剪并保存设备内保存的图像

    如果你想做这个,我建议你可以使用XamarinAndroidImageCropper来做。

    首先,通过Manage Nuget package....安装XamarinAndroidImageCropper,然后为manifest添加权限。

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

    像这样使用 CropImageView:

    <com.theartofdev.edmodo.cropper.CropImageView
    android:id="@+id/cropImageView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cropAspectRatioX="16"
    app:cropAspectRatioY="9"
    app:cropBackgroundColor="#88AA66CC"
    app:cropBorderCornerColor="@android:color/holo_blue_bright"
    app:cropBorderCornerOffset="0dp"
    app:cropBorderCornerThickness="5dp"
    app:cropBorderLineColor="@android:color/holo_green_light"
    app:cropBorderLineThickness="1dp"
    app:cropGuidelines="on"
    app:cropGuidelinesColor="@android:color/holo_red_dark"
    app:cropInitialCropWindowPaddingRatio="0"
    app:cropSnapRadius="0dp"/>
    

    更多详细信息,请看:

    https://github.com/mikescandy/Xamarin-Cropper

    【讨论】:

    • 樱桃卜。感谢大家的回复和跟进。但是当我对整个项目进行修改时,是否可以只修改我的项目的一小部分?从设备中选择后裁剪图像并在imageview中显示
    • @monemahmed 当然,你可以通过修改这个项目来做到这一点。
    • 你好樱桃卜。是的,您添加的示例已经完成,但是裁剪后的图像如何保存?另一个问题,如何删除已切割部分的空白背景?
    • @monemahmed 您是否尝试根据您的要求修改样本?有什么问题吗?
    • 您是否尝试根据您的要求修改样本?有问题吗?是的,我参与了这个项目,但我无法保存图像或删除背景
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多