Android Bitmap 图片处理

1.Bitmap,Drawable,InputStream,Byte[ ] 之间进行转换

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
/**
* Drawable转化成Bitmap
* @param drawable drawable
* @return Bitmap
*/
public static Bitmap drawableToBitmap(Drawable drawable) {
int intrinsicWidth = drawable.getIntrinsicWidth();
int intrinsicHeight = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
Bitmap bitmap = Bitmap.createBitmap(intrinsicWidth, intrinsicHeight,config);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, intrinsicWidth, intrinsicHeight);
drawable.draw(canvas);
return bitmap;
}

/**
* Bitmap转换成Drawable
* @param resources resources
* @param bm bm
* @return
*/
public static Drawable bitmapToDrawable(Resources resources, Bitmap bm) {
Drawable drawable = new BitmapDrawable(resources, bm);
return drawable;
}

/**
* InputStream转换成Bitmap
* @param is InputStream流
* @return
*/
public static Bitmap bitmapToDrawable(InputStream is) {
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}

2.Bitmap图片压缩

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
/**
* 图片压缩
* @param image image
* @return
*/
public static Bitmap compressImage(Bitmap image) {
if (image == null) {
return null;
}
ByteArrayOutputStream bao = null;
try {
bao = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte[] bytes = bao.toByteArray();
ByteArrayInputStream isBm = new ByteArrayInputStream(bytes);
return BitmapFactory.decodeStream(isBm);
} catch (OutOfMemoryError e) {
e.printStackTrace();
} finally {
try {
if (bao != null) {
bao.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

3.将bitmap根据scale生成一张图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 将bitmap根据scale生成一张图片
*
* @param bitmap bitmap
* @param scale 等比缩放值
* @return bitmap
*/
public static Bitmap bitmapScale(Bitmap bitmap, float scale) {
Matrix matrix = new Matrix();
// 长和宽放大缩小的比例
matrix.postScale(scale, scale);
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}

4.将View控件转换为bitmap

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
public static Bitmap convertViewToBitMap(View view){
// 打开图像缓存
view.setDrawingCacheEnabled(true);
// 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
// 测量View大小
int i = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
int n = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(i, n);
// 发送位置和尺寸到View及其所有的子View
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// 获得可视组件的截图
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}

/**
* 该方法会报错,java.lang.IllegalArgumentException: width and height must be > 0
*/
public static Bitmap getBitmapFromView(View view){
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null){
bgDrawable.draw(canvas);
} else{
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}


//该方法验证可以
public static Bitmap loadBitmapFromView(View v) {
v.measure(0, 0);
int w = v.getMeasuredWidth();
int h = v.getMeasuredHeight();
if (w <= 0 || h <= 0) {
DisplayMetrics metric = new DisplayMetrics();
w = metric.widthPixels;// 屏幕宽度(像素)
h = metric.heightPixels;// 屏幕高度(像素)
}
Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bmp);
c.drawColor(Color.WHITE);
//如果不设置canvas画布为白色,则生成透明
v.layout(0, 0, w, h);
v.draw(c);
return bmp;
}

6.如何计算bitmap占用内存大小

  • 如果图片要显示下Android设备上,ImageView最终是要加载Bitmap对象的,就要考虑单个Bitmap对象的内存占用了,如何计算一张图片的加载到内存的占用呢?其实就是所有像素的内存占用总和:
  • bitmap内存大小 = 图片长度 x 图片宽度 x 单位像素占用的字节数
  • 起决定因素就是最后那个参数了,Bitmap’常见有2种编码方式:ARGB_8888和RGB_565,ARGB_8888每个像素点4个byte,RGB_565是2个byte,一般都采用ARGB_8888这种。那么常见的1080*1920的图片内存占用就是:1920 x 1080 x 4 = 7.9M