【问题标题】:Cropping Image in Android (Crop Intent)在 Android 中裁剪图像(裁剪意图)
【发布时间】:2015-02-02 08:18:13
【问题描述】:

我用这个code 来使用android 的内置图像裁剪工具。我的代码如下

 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 在按钮的点击监听器中被调用。完成的是我可以打开 android 相机拍照,并在点击保存时将图像保存在我的 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 image crop android-crop


    【解决方案1】:

    你可以试试这个。

    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);
            }
        }
    
    }
    

    【讨论】:

    • 这会取代我的 takePicture 功能吗? picUri 代表什么?我还没有保存图片。
    • 您需要传递保存图像 uri,这将打开一个裁剪意图(如果可用),您可以执行裁剪。
    • 是的,图片默认保存在哪里?我从 Activity 接收到 Result 后,我​​只是将它放在 ImageView 上,但没有保存在任何地方。我知道它已保存在我的图库中,但仍无法将其保存到我想要的位置
    • 我们获得的位图正在保存在您设备的图库中。因此我们需要完整的路径或 URI 来进一步使用它
    • 此答案中推荐的意图操作不是标准 Android 的一部分。它不适用于某些设备,请参阅commonsware.com/blog/2013/01/23/…
    猜你喜欢
    • 2012-01-16
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 2022-01-23
    相关资源
    最近更新 更多