Intent Crop

本文深入探讨了Android中使用Intent实现截图和裁剪功能的详细步骤,包括Intent的附加选项及其具体含义,以及如何根据图片大小选择合适的存储方式(使用URI或Bitmap)。文章还提供了从相册和拍照时截取不同尺寸图片的示例代码,以及如何处理返回数据的说明。

call to OpenGL ES API with no current context (logged once per thread):


Intent细节

首先,让我们探讨下Intent以及它的特点。在看了一些代码示例以后,我发现我可以很轻松的使用如下的Intent调用裁剪功能:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType(“image/*”);
intent.putExtra(“crop”, “true”);

然而,这是在我缺少附加的文档,不知道这些选项的具体含义等等情况之下的选择。所以,我将我的yanj整理成一个表格 ,并写了一个演示程序,力图演示控制此功能的所有可供选项。

你可以在你的程序中使用使用我的代码,并且扩展它。我会将之附加在这篇文章上。

Exta Options Table for image/* crop:

附加选项 数据类型 描述
crop String 发送裁剪信号
aspectX int X方向上的比例
aspectY int Y方向上的比例
outputX int 裁剪区的宽
outputY int 裁剪区的高
scale boolean 是否保留比例
return-data boolean 是否将数据保留在Bitmap中返回
data Parcelable 相应的Bitmap数据
circleCrop String 圆形裁剪区域?
MediaStore.EXTRA_OUTPUT ("output") URI 将URI指向相应的file:///...,详见代码示例

现在,最令人困惑的是MediaStore.EXTRA_OUTPUT以及return-data选项。

你主要有两种方式从这个Intent中取得返回的bitmap:获取内部数据或者提供一个Uri以便程序可以将数据写入。

方法1:如果你将return-data设置为“true”,你将会获得一个与内部数据关联的Action,并且bitmap以此方式返回:(Bitmap)extras.getParcelable("data")。注意:如果你最终要获取的图片非常大,那么此方法会给你带来麻烦,所以你要控制outputX和outputY保持在较小的尺寸。鉴于此原因,在我的代码中没有使用此方法((Bitmap)extras.getParcelable("data"))。

下面是CropImage.java的源码片段:

// Return the cropped image directly or save it to the specified URI.
Bundle myExtras = getIntent().getExtras();
if (myExtras != null && (myExtras.getParcelable("data") != null || myExtras.getBoolean("return-data")))
{
    Bundle extras = new Bundle();
    extras.putParcelable("data", croppedImage);
    setResult(RESULT_OK,(new Intent()).setAction("inline-data").putExtras(extras));
    finish();
}

方法2: 如果你将return-data设置为“false”,那么在onActivityResult的Intent数据中你将不会接收到任何Bitmap,相反,你需要将MediaStore.EXTRA_OUTPUT关联到一个Uri,此Uri是用来存放Bitmap的。

但是还有一些条件,首先你需要有一个短暂的与此Uri相关联的文件地址,当然这不是个大问题(除非是那些没有sdcard的设备)。

下面是CropImage.java关于操作Uri的源码片段:

if (mSaveUri != null) {
    OutputStream outputStream = null;
    try {
        outputStream = mContentResolver.openOutputStream(mSaveUri);
        if (outputStream != null) {
            croppedImage.compress(mOutputFormat, 75, outputStream);
        }
    } catch (IOException ex) {
        // TODO: report error to caller
        Log.e(TAG, "Cannot open file: " + mSaveUri, ex);
    } finally {
        Util.closeSilently(outputStream);
    }
    Bundle extras = new Bundle();
    setResult(RESULT_OK, new Intent(mSaveUri.toString()).putExtras(extras));
}



最近遇到一问题:call to OpenGL ES API with no current context (logged once per thread)

解决Android对返回图片的大小限制,并且详细解释了裁剪图片的Intent附加数据的具体含义。

下面是一些参数

附加选项数据类型描述

cropString  发送裁剪信号

aspectXintX  方向上的比例

aspectYintY  方向上的比例

outputXin  t裁剪区的宽

outputYint  裁剪区的高

scaleboolean是否保留比例

return-databoolean是否将数据保留在Bitmap中返回dataParcelable相应的Bitmap数据

circleCropString圆形裁剪区域?

MediaStore.EXTRA_OUTPUT("output")URIURI指向相应的file:///...,详见代码示例



Intent("com.android.camera.action.CROP")对应的所有可选数据都一目了然。在了解上面个个选项的含义之后,我们将目光着眼于三个极为重要的选项:

data、MediaStore.EXTRA_OUTPUT以及return-data。

data和MediaStore.EXTRA_OUTPUT都是可选的传入数据选项,你可以选择设置data为Bitmap,或者将相应的数据与URI关联起来,你也可以选择是否返回数据(return-data: true)。

为什么还有不用返回数据的选项?如果对URI足够了解的话,应该知道URI与File相似,你所有的操作如裁剪将数据都保存在了URI中,你已经持有了相应的URI,也就无需多此一举,再返回Bitmap了。

前面已经说到,可以设置data为Bitmap,但是这种操作的限制在于,你的Bitmap不能太大。因此,我们前进的思路似乎明确了:截大图用URI,小图用Bitmap。

这篇主要让大家了解需求的来源,以及如何去思考分析并解决问题。下一篇将介绍具体的操作。

从相册截图

在这篇文章中,我将向大家展示如何从相册截图。

上一篇文章中,我就拍照截图这一需求进行了详细的分析,试图让大家了解Android本身的限制,以及我们应当采取的实现方案。

根据我们的分析与总结,图片的来源有拍照和相册,而可采取的操作有

• 使用Bitmap并返回数据

• 使用Uri不返回数据

前面我们了解到,使用Bitmap有可能会导致图片过大,而不能返回实际大小的图片,我将采用大图Uri,小图Bitmap的数据存储方式。

我们将要使用到URI来保存拍照后的图片:

private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

不难知道,我们从相册选取图片的Action为Intent.ACTION_GET_CONTENT。

根据我们上一篇文章的分析,我准备好了两个实例的Intent。

一、从相册截大图:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 600);
intent.putExtra("outputY", 300);
intent.putExtra("scale", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CHOOSE_BIG_PICTURE);

二、从相册截小图

Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 100);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, CHOOSE_SMALL_PICTURE);

三、对应的onActivityResult可以这样处理返回的数据

switch (requestCode) {
case CHOOSE_BIG_PICTURE:
 Log.d(TAG, "CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null
 if(imageUri != null){
  Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap
  imageView.setImageBitmap(bitmap);
 }
 break;
case CHOOSE_SMALL_PICTURE:
 if(data != null){
  Bitmap bitmap = data.getParcelableExtra("data");
  imageView.setImageBitmap(bitmap);
 }else{
  Log.e(TAG, "CHOOSE_SMALL_PICTURE: data = " + data);
 }
 break;
default:
 break;
}

 

private Bitmap decodeUriAsBitmap(Uri uri){
 Bitmap bitmap = null;
 try {
  bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
 } catch (FileNotFoundException e) {
  e.printStackTrace();
  return null;
 }
 return bitmap;
}

效果图:


拍照截图

上一篇博客中,我们学习到了如何使用Android相册截图。在这篇博客中,我将向大家展示如何拍照截图。

拍照截图有点儿特殊,要知道,现在的Android智能手机的摄像头都是几百万的像素,拍出来的图片都是非常大的。因此,我们不能像对待相册截图一样使用Bitmap小图,无论大图小图都统一使用Uri进行操作。

一、首先准备好需要使用到的Uri:

private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file
Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

二、使用MediaStore.ACTION_IMAGE_CAPTURE可以轻松调用Camera程序进行拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//action is capture
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_BIG_PICTURE);//or TAKE_SMALL_PICTURE

三、接下来就可以在 onActivityResult中拿到返回的数据(Uri),并将Uri传递给截图的程序。

switch (requestCode) {
case TAKE_BIG_PICTURE:
 Log.d(TAG, "TAKE_BIG_PICTURE: data = " + data);//it seems to be null
 //TODO sent to crop
 cropImageUri(imageUri, 800, 400, CROP_BIG_PICTURE);
 
 break;
case TAKE_SMALL_PICTURE:
 Log.i(TAG, "TAKE_SMALL_PICTURE: data = " + data);
 //TODO sent to crop 
 cropImageUri(imageUri, 300, 150, CROP_SMALL_PICTURE);
 
 break;
default:
 break;
}

可以看到,无论是拍大图片还是小图片,都是使用的Uri,只是尺寸不同而已。我们将这个操作封装在一个方法里面。

private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode){
 Intent intent = new Intent("com.android.camera.action.CROP");
 intent.setDataAndType(uri, "image/*");
 intent.putExtra("crop", "true");
 intent.putExtra("aspectX", 2);
 intent.putExtra("aspectY", 1);
 intent.putExtra("outputX", outputX);
 intent.putExtra("outputY", outputY);
 intent.putExtra("scale", true);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
 intent.putExtra("return-data", false);
 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
 intent.putExtra("noFaceDetection", true); // no face detection
 startActivityForResult(intent, requestCode);
}

四、最后一步,我们已经将数据传入裁剪图片程序,接下来要做的就是处理返回的数据了:

switch (requestCode) {
case CROP_BIG_PICTURE://from crop_big_picture
 Log.d(TAG, "CROP_BIG_PICTURE: data = " + data);//it seems to be null
 if(imageUri != null){
  Bitmap bitmap = decodeUriAsBitmap(imageUri);
  imageView.setImageBitmap(bitmap);
 }
 break;
case CROP_SMALL_PICTURE:
 if(imageUri != null){
  Bitmap bitmap = decodeUriAsBitmap(imageUri);
  imageView.setImageBitmap(bitmap);
 }else{
  Log.e(TAG, "CROP_SMALL_PICTURE: data = " + data);
 }
 break;
default:
 break;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值