Bitmap实现在android.graphics包中。但是Bitmap类的构造函数是私有的
BitmapFactory可以从一个指定文件中,利用decodeFile()解出Bitmap;
也可以定义的图片资源中,利用decodeResource()解出Bitmap。
Options的下列属性,可以指定decode的选项:
· inPreferredConfig 指定decode到内存中,手机中所采用的编码,可选值定义在Bitmap.Config中。缺省值是ARGB_8888。
· inJustDecodeBounds 如果设置为true,并不会把图像的数据完全解码,亦即decodeXyz()返回值为null,但是Options的outAbc中解出了图像的基本信息。
· inSampleSize 设置decode时的缩放比例。
利用Options的这些值就可以高效的得到一幅缩略图。
先设置inJustDecodeBounds= true,调用decodeFile()得到图像的基本信息[Step#2~4];
利用图像的宽度(或者高度,或综合)以及目标的宽度,得到inSampleSize值,再设置inJustDecodeBounds= false,调用decodeFile()得到完整的图像数据[Step#5~8]。
先获取比例,再读入数据,如果欲读入大比例缩小的图,将显著的节约内容资源。有时候还会读入大量的缩略图,这效果就更明显了。
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/**
* 获取图片在数据库的路径
* @param uri
* @return
*/
private
String getImgPath(Uri uri){
String img_path =
""
;
if
(uri !=
null
){
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = managedQuery(uri,proj,
null
,
null
,
null
);
int
actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
img_path = actualimagecursor.getString(actual_image_column_index);
}
return
img_path;
}
/**
* 获取bitmap的Uri
* @param photo
* @return
*/
private
Uri getBitMapUri(Bitmap photo){
File file = bitmapToFile(photo);
return
Uri.fromFile(file);
}
private
File bitmapToFile(Bitmap photo){
File fileDir =
new
File(Constant.USER_ICON_DIR);
if
(!fileDir.exists()) {
fileDir.mkdir();
}
else
{
if
(fileDir.isDirectory()) {
File[] childFiles = fileDir.listFiles();
for
(File file : childFiles) {
file.delete();
}
}
}
File f =
new
File(fileDir, System.currentTimeMillis() +
".jpeg"
);
FileOutputStream fOut =
null
;
try
{
if
(f.exists()) {
f.delete();
}
f.createNewFile();
fOut =
new
FileOutputStream(f);
photo.compress(Bitmap.CompressFormat.JPEG,
90
, fOut);
fOut.flush();
}
catch
(Exception e) {
}
finally
{
try
{
fOut.close();
}
catch
(IOException e) {
e.printStackTrace();
}
}
return
f;
}
/**
* 获取图片的存储角度
* @param path
* @return
*/
private
int
getBitmapDegree(String path) {
int
degree =
0
;
try
{
// 从指定路径下读取图片,并获取其EXIF信息
ExifInterface exifInterface =
new
ExifInterface(path);
// 获取图片的旋转信息
int
orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch
(orientation) {
case
ExifInterface.ORIENTATION_ROTATE_90:
degree =
90
;
break
;
case
ExifInterface.ORIENTATION_ROTATE_180:
degree =
180
;
break
;
case
ExifInterface.ORIENTATION_ROTATE_270:
degree =
270
;
break
;
}
}
catch
(IOException e) {
e.printStackTrace();
}
return
degree;
}
/**
* 旋转bitmap
* @param bm
* @param degree
* @return
*/
public
Bitmap rotateBitmapByDegree(Bitmap bm,
int
degree) {
Bitmap returnBm =
null
;
// 根据旋转角度,生成旋转矩阵
Matrix matrix =
new
Matrix();
matrix.postRotate(degree);
try
{
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm,
0
,
0
, bm.getWidth(), bm.getHeight(), matrix,
true
);
}
catch
(OutOfMemoryError e) {
}
if
(returnBm ==
null
) {
returnBm = bm;
}
if
(bm != returnBm) {
bm.recycle();
}
return
returnBm;
}
/**
* *drawable 转化为bitmap
*
* @param drawable
* @return bitmap
*/
public
static
Bitmap drawableToBitmap(Drawable drawable) {
if
(drawable
instanceof
BitmapDrawable) {
return
((BitmapDrawable) drawable).getBitmap();
}
else
if
(drawable
instanceof
NinePatchDrawable) {
// 取 drawable 的长宽
int
w = drawable.getIntrinsicWidth();
int
h = drawable.getIntrinsicHeight();
// 取 drawable 的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas =
new
Canvas(bitmap);
drawable.setBounds(
0
,
0
, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return
bitmap;
}
else
{
return
null
;
}
}
/**
*
* @param bm
* @return Byte
*/
public
byte
[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG,
100
, baos);
return
baos.toByteArray();
}
|