在Android中裁剪图像(裁剪意图)

6

我使用了这个代码来使用安卓内置的图像裁剪工具。我的代码如下:

 public void takePicture(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null){
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
        takePictureIntent.putExtra("crop", "true");
        takePictureIntent.putExtra("aspectX", 0);
        takePictureIntent.putExtra("aspectY", 0);
        takePictureIntent.putExtra("outputX", 200);
        takePictureIntent.putExtra("outputY", 150);
        takePictureIntent.putExtra("return-data", true);

        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageViewImage.setImageBitmap(imageBitmap);

    }
}

在按钮的点击监听器中调用takePicture。我可以打开安卓相机拍照,保存图片后显示在我的imageView上。但是没有出现裁剪界面,imageView上的图片看起来很糟糕。它的质量就像被像素化了一样。我做错了什么吗?我使用三星galaxy tab 3测试我的应用程序。

编辑:使用下面的答案……仍然不工作

 protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Log.d("onActivityResult", "Inside on activity for result");
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        imageViewImage.setImageBitmap(imageBitmap);
        fileUri = getImageUri(this, imageBitmap);χ
        cropImage();
    }else if (requestCode == REQUEST_IMAGE_CROP && resultCode == RESULT_OK){
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap)extras.get("data");
        imageViewImage.setImageBitmap(imageBitmap);


    }
}

 public void takePicture(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null){

        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}

 public void cropImage() {
    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");

        cropIntent.setDataAndType(fileUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);
        cropIntent.putExtra("outputX", 128);
        cropIntent.putExtra("outputY", 128);
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, REQUEST_IMAGE_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

LocCat here


Android没有CROP Intent。有许多适用于Android的图像裁剪库,请使用其中之一。 - CommonsWare
1个回答

8
你可以尝试这个。
private void doCrop(Uri picUri) {
    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");

        cropIntent.setDataAndType(picUri, "image/*");           
        cropIntent.putExtra("crop", "true");           
        cropIntent.putExtra("aspectX", 1);
        cropIntent.putExtra("aspectY", 1);           
        cropIntent.putExtra("outputX", 128);
        cropIntent.putExtra("outputY", 128);           
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent, CROP_PIC_REQUEST_CODE);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}

从位图获取Uri

public Uri getImageUri(Context inContext, Bitmap inImage) {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
  String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
  return Uri.parse(path);
}

声明

final int CROP_PIC_REQUEST_CODE = 1;

然后只需

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CROP_PIC_REQUEST_CODE) {
        if (data != null) {
            Bundle extras = data.getExtras();
            Bitmap bitmap= extras.getParcelable("data");
            yourImageView.setImageBitmap(bitmap);
        }
    }

}

你需要传递保存的图像URI,这将打开一个裁剪意图(如果可用),然后你可以执行裁剪。 - Murtaza Khursheed Hussain
已更新,附带部分logCat日志。 - Apostolos
@MurtazaKhursheedHussain 我有一个问题想问,为什么我们需要实现imageUri方法?这只是在使用sharedpreferences期间吗?如果能得到任何解释,我会非常感激。 - Ajay P. Prajapati
1
我们获取的位图将被保存在设备的相册中。因此,我们需要完整的路径或URI来进一步使用它。 - Murtaza Khursheed Hussain
3
此答案中推荐的意图操作不是标准 Android 的一部分。它在某些设备上无法正常工作,请参见 https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html。 - satur9nine
显示剩余6条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接