将彩色图片转为灰度图

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();








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值