/**
* 判断图片颜色是深色还是浅色
* @param src
* @return true 图片为浅色
*/
public static boolean getBitmapPixColor(Bitmap src) {
if (null == src) {
return false;
}
int R = 0, G = 0, B = 0;
int pixelColor;
int height = src.getHeight();
int width = src.getWidth();
//获取RGB值
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixelColor = src.getPixel(x, y);
R = Color.red(pixelColor);
G = Color.green(pixelColor);
B = Color.blue(pixelColor);
}
}
//将获取的RGB值进行计算,由192来区分为深色/浅色
int grayLevel = (int) (R * 0.299 + G * 0.587 + B * 0.114);
return grayLevel > 192;
}
/**
*
* @param drawable
* @return
*/
public static Bitmap drawableToBitmap(Drawable drawable)
{
//声明将要创建的bitmap
Bitmap bitmap = null;
//获取图片宽度
int width = drawable.getIntrinsicWidth();
//获取图片高度
int height = drawable.getIntrinsicHeight();
//图片位深,PixelFormat.OPAQUE代表没有透明度,RGB_565就是没有透明度的位深,否则就用ARGB_8888。详细见下面图片编码知识。
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
//创建一个空的Bitmap
bitmap = Bitmap.createBitmap(width,height,config);
//在bitmap上创建一个画布
Canvas canvas = new Canvas(bitmap);
//设置画布的范围
drawable.setBounds(0, 0, width, height);
//将drawable绘制在canvas上
drawable.draw(canvas);
return bitmap;
}
网友评论