您的位置:首页 > 其它

将彩色图片转为灰度图

2011-09-22 17:26 519 查看
1.将彩色Bitmap转换为灰色Bitmap

Bitmap grayImg = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(grayImg);

Paint paint = new Paint();
//设置颜色矩阵
ColorMatrix colorMatrix = new ColorMatrix();
//设置饱和度——变灰
colorMatrix.setSaturation(0);
//颜色滤镜
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
colorMatrix);
//将颜色矩阵应用于图片
paint.setColorFilter(colorMatrixFilter);
//绘图,bitmap为彩图,最终灰度图为grayImg
canvas.drawBitmap(bitmap, 0, 0, paint);
2.Drawable和Bitmap互相转换

①Bitmap转Drawable

Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=BitmapDrawable(bm);//BtimapDrawable是Drawable的子类,最终直接使用bd对象即可
②Drawable转Bitmap

Drawable d=xxx; //xxx根据自己的情况获取drawable,如:Drawable d = getResources().getDrawable(R.drawable.img);
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: