我如何裁剪从图库和相机加载的图像,而无需任何库使用



哪个库适合裁剪图像并从图库和相机中获取图像,然后裁剪图像并设置为图像视图

试试这个库,它在两种情况下都适合我 - https://github.com/lvillani/android-cropimage

更新

这不是完美的代码b'z,我什至在两者之间得到了项目,所以我没有时间说清楚,但你可以修改并让我知道。很高兴在这里发表您的评论。

private static final int REQUEST_CAMERA = 0;
private static final int CAMERA_REQUEST = 1;
private static final int RESULT_LOAD_IMAGE = 2;
private static final int GALLERY_KITKAT_INTENT_CALLED = 3;
private static final int CROP_IMAGE_GALLERY = 4;
private static final int CROP_IMAGE_CAMERA = 5;
private String FilePath;
private Uri ImageUri, croppedImageUri;
private File pic_file;

private void SelectImage()
{
    final CharSequence[] options = {"Take Photo", "Choose from Gallery", "Remove", "Cancel"};
    AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
    builder.setTitle("Upload Photo");
    builder.setItems(options, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (options[item].equals("Take Photo")) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                pic_file = new File(Environment.getExternalStorageDirectory(),
                        "tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                ImageUri = Uri.fromFile(pic_file);
                cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                        ImageUri);
                cameraIntent.putExtra("return-data", true);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            } else if (options[item].equals("Choose from Gallery")) {
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);

            } else if (options[item].equals("Remove")) {
                ivProfilePic.setImageResource(R.mipmap.ic_profile_pic);
                FilePath = "";
                dialog.dismiss();
            } else if (options[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

/*
 * After capture Image or Select Image from Gallary  On activityResult Call
 *
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
    {
        try {
           CropImage();
        } catch (Exception e) {
            rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
        }

    }
    else if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        try {
            ImageUri = data.getData();
            CropImage();
        } catch (Exception e) {
            rootLogger.error(Utility.createLogableFormat(Utility.getStringStackTrace(e)));
        }
    }
    else if (requestCode == CROP_IMAGE_CAMERA) {
            if (data != null) {
                // get the returned data
                Bundle extras = data.getExtras();
                if (extras != null) {
                    FilePath = ImageUri.getPath();
                    File f = new File(FilePath);
                    Bitmap bmp_post_news = decodeSampledBitmapFromResource(f.getAbsolutePath(),120,120);
                    ivProfilePic.setImageBitmap(bmp_post_news);
                }
            }
        }
        else if (requestCode == CROP_IMAGE_GALLERY) {
            if (data != null) {
                // get the returned data
                Bundle extras = data.getExtras();
                if (extras != null) {
                    FilePath = croppedImageUri.getPath();
                    Bitmap bmp_post_news = decodeSampledBitmapFromResource(FilePath,120,120);
                    ivProfilePic.setImageBitmap(bmp_post_news);

                }
            }
        }
}
public Bitmap decodeSampledBitmapFromResource(String Filepath,
                                                  int reqWidth, int reqHeight) {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(Filepath, options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(Filepath, options);
    }
public int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    int inSampleSize = 1;
    final int height = options.outHeight;
    final int width = options.outWidth;

    if (height > reqHeight || width > reqWidth) {
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }
    return inSampleSize;
}
protected void CropImage() {
    CropImageIntentBuilder cropImage = new CropImageIntentBuilder(3, 4,120,120, ImageUri);
    //cropImage.setOutlineColor(0xFF03A9F4);
    cropImage.setSourceImage(ImageUri);
    cropImage.setCircleCrop(true);
    cropImage.setOutlineCircleColor(Color.WHITE);
    cropImage.setOutputQuality(100);
    Intent intent = cropImage.getIntent(this);
    //intent.putExtra("return-data", false);
    startActivityForResult(intent, CROP_IMAGE_CAMERA);
}