在Android中裁剪图像(裁剪意图) [英] Cropping Image in Android (Crop Intent)

查看:110
本文介绍了在Android中裁剪图像(裁剪意图)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used this code to use android's built in image crop tools. My code is the following

 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来测试我的应用

takePicture is called inside a click listener for a Button. What is done is I can open android camera take the picture and when hitting save the image is saved on my imageView. But no cropping activity appears, plus the image on imageView looks awfull. The quality is like it's pixelated. Am I doing something wrong? I used a Samsung galaxy tab 3 to test my app

使用以下答案进行编辑... Stil无法正常工作

EDIT using the answer bellow...Stil not working

 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在这里

推荐答案

你可以试试这个。

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

}

这篇关于在Android中裁剪图像(裁剪意图)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆