转载声明:Ryan的博客文章欢迎您的转载,但在转载的同时,请注明文章的来源出处,不胜感激! :-)
http://my.oschina.net/ryanhoo/blog/86843
译者:Ryan Hoo
来源:http://www.androidworks.com/crop_large_photos_with_android
译者按:在外企工作的半年多中花了不少时间在国外的网站上搜寻资料,其中有一些相当有含金量的文章,我会陆陆续续翻译成中文,与大家共享之。初次翻译,“信达雅”三境界恐怕只到信的层次,望大家见谅!
这篇文章相当经典而实用,想当初我做手机拍照截图的时候,大多都是在网上抄来抄去的内容,从来没有人考虑过实际项目中的需求。实际上,拍照传大图片,如果用普通方式会耗用极大的内存,Android一个App原则上的16M内存限制可以一下子被耗光。Android在拍照上有一个隐藏的设计,如果拍照图片过大,只返回一张缩略图。具体到不同手机,都是不一样的。
-------------------------------------------------------------------------------------
译文:
概述
我写这篇文章是为了发表我对MediaStore裁剪图片功能的一些简要研究。基本上,如果你要写一个应用程序,使用已有的Media Gallery并允许用户在你的应用里选取TA的图片的一部分(可选功能:人脸识别)。 可以使用一个Intent做到这个,但是也存在着相应的问题,总的来说也缺少这方面的文档告诉我们怎么实现它。 另外,这篇文章基于2.1并且在Nexus One上做了测试。 Nexus One上的实现似乎被这群人写在了这里: Media Gellery for Nexus One 。反馈
这篇文章需要使用基于我的研究所写的程序。如果你对我推荐的实现方案有所改进,请让我知道。我会相应的更新这篇文章。
Intent细节
首先,让我们探讨下Intent以及它的特点。在看了一些代码示例以后,我发现我可以很轻松的使用如下的Intent调用裁剪功能:
1
2
3
4
|
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT,
null
);
intent.setType(“image/*”);
intent.putExtra(“crop”, “
true
”);
…
|
你可以在你的程序中使用使用我的代码,并且扩展它。我会将之附加在这篇文章上。
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的源码片段:
1
2
3
4
5
6
7
8
9
|
// 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();
}
|
但是还有一些条件,首先你需要有一个短暂的与此Uri相关联的文件地址,当然这不是个大问题(除非是那些没有sdcard的设备)。
下面是CropImage.java关于操作Uri的源码片段:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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));
}
|
代码示例:
我已经附上了一些代码示例,应该可以让你测试多种配置。请让我知道它对你是否有用。
代码下载: MediaStoreTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
thiz =
this
;
setContentView(R.layout.main);
mBtn = (Button) findViewById(R.id.btnLaunch);
photo = (ImageView) findViewById(R.id.imgPhoto);
mBtn.setOnClickListener(
new
OnClickListener() {
public
void
onClick(View v) {
try
{
// Launch picker to choose photo for selected contact
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT,
null
);
intent.setType(
"image/*"
);
intent.putExtra(
"crop"
,
"true"
);
intent.putExtra(
"aspectX"
, aspectX);
intent.putExtra(
"aspectY"
, aspectY);
intent.putExtra(
"outputX"
, outputX);
intent.putExtra(
"outputY"
, outputY);
intent.putExtra(
"scale"
, scale);
intent.putExtra(
"return-data"
, return_data);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
intent.putExtra(
"outputFormat"
,
Bitmap.CompressFormat.JPEG.toString()); <span style=
"color:#48465A;font-family:monospace;font-size:11px;line-height:normal;background-color:#EFEFEF;"
>
// lol, negative boolean noFaceDetection</span> intent.putExtra("noFaceDetection", !faceDetection);
if
(circleCrop) {
intent.putExtra(
"circleCrop"
,
true
);
}
startActivityForResult(intent, PHOTO_PICKED);
}
catch
(ActivityNotFoundException e) {
Toast.makeText(thiz, R.string.photoPickerNotFoundText,
Toast.LENGTH_LONG).show();
}
}
});
}
private
Uri getTempUri() {
return
Uri.fromFile(getTempFile());
}
private
File getTempFile() {
if
(isSDCARDMounted()) {
File f =
new
File(Environment.getExternalStorageDirectory(),
TEMP_PHOTO_FILE);
try
{
f.createNewFile();
}
catch
(IOException e) {
// TODO Auto-generated catch block
Toast.makeText(thiz, R.string.fileIOIssue, Toast.LENGTH_LONG)
.show();
}
return
f;
}
else
{
return
null
;
}
}
private
boolean
isSDCARDMounted() {
String status = Environment.getExternalStorageState();
if
(status.equals(Environment.MEDIA_MOUNTED))
return
true
;
return
false
;
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
switch
(requestCode) {
case
PHOTO_PICKED:
if
(resultCode == RESULT_OK) {
if
(data ==
null
) {
Log.w(TAG,
"Null data, but RESULT_OK, from image picker!"
);
Toast t = Toast.makeText(
this
, R.string.no_photo_picked,
Toast.LENGTH_SHORT);
t.show();
return
;
}
final
Bundle extras = data.getExtras();
if
(extras !=
null
) {
File tempFile = getTempFile();
// new logic to get the photo from a URI
if
(data.getAction() !=
null
) {
processPhotoUpdate(tempFile);
}
}
}
break
;
}
}
|
Thank you so much! The tutorial is great! Actually the secret of cropping photos on Android is using Uri if the photo is in large size and using Bitmap if you want but make sure that the bitmap is not too big.(You can use it for cropping avatar or other requirements with a limited size of the photo. Different phones have different limits. Normally if you want to use a bitmap, the size shouldn't be bigger than 300. Otherwise the Uri is suggested.)