裁剪并保存图像

时间:2020-11-04 18:14:39

标签: xamarin.android

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

错误

打开相机 enter image description here

打开画廊 enter image description here

完整项目 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;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

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

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

如果要执行此操作,建议您使用 XamarinAndroidImageCropper 进行操作。

首先,通过Manage Nuget软件包安装 XamarinAndroidImageCropper ...,然后添加权限以实现清单。

<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