6

我使用此代码来使用 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);
}

定位猫在这里

4

1 回答 1

9

你可以试试这个。

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

}
于 2014-12-04T12:37:05.623 回答