彩色图片的各种处理方法:灰度,黑白,底片效果

(1) 灰度

		public Bitmap switchColor(Bitmap switchBitmap){
			int width=switchBitmap.getWidth();
			int height=switchBitmap.getHeight();
			
			Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
			Canvas canvas=new Canvas(newBitmap);
			Paint paint=new Paint();
			ColorMatrix colorMatrix=new ColorMatrix();
			colorMatrix.setSaturation(0);
			ColorMatrixColorFilter filter=new ColorMatrixColorFilter(colorMatrix);
			paint.setColorFilter(filter);
			canvas.drawBitmap(switchBitmap, 0, 0,paint);
			
			return newBitmap;

http://blog.sina.com.cn/s/blog_4ab2f5c801015dfs.html

此外参照这篇博客,还可以调整灰度的效果,可以偏亮或者偏柔和

(2)黑白

		public Bitmap switchColor(Bitmap switchBitmap){
			int width=switchBitmap.getWidth();
			int height=switchBitmap.getHeight();
			
			// Turn the picture black and white
			Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
			Canvas canvas=new Canvas(newBitmap);
			canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());
			
			int current_color,red,green,blue,alpha,avg=0;
			for (int i=0;i<width;i++){
				for (int j=0;j<height;j++){ 
					current_color=switchBitmap.getPixel(i, j);
					red=Color.red(current_color);
					green=Color.green(current_color);
					blue=Color.blue(current_color);
					alpha=Color.alpha(current_color);
					avg=(red+green+blue)/3;// RGB average
					if (avg>=126){
						newBitmap.setPixel(i, j, Color.argb(alpha, 255, 255, 255));// white
					} else{	
						newBitmap.setPixel(i, j, Color.argb(alpha, 0, 0, 0));// black
					}
				}
			}
			return newBitmap;
		}
    其中avg的值可以自己设定

(3)底片效果


		public Bitmap switchInvert(Bitmap switchBitmap){
		    int width=switchBitmap.getWidth();  
		    int height=switchBitmap.getHeight();  
		      
		    Bitmap newBitmap=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);  
		    Canvas canvas=new Canvas(newBitmap);  
		    canvas.drawBitmap(switchBitmap, new Matrix(), new Paint());  
		      
		    int current_color,red,green,blue,alpha,r,g,b=0;  
		    for (int i=0;i<width;i++){  
		        for (int j=0;j<height;j++){   
		            current_color=switchBitmap.getPixel(i, j);  
		            red=Color.red(current_color);  
		            green=Color.green(current_color);  
		            blue=Color.blue(current_color); 
		            alpha=Color.alpha(current_color);  
		            //底片效果是当前像素点对应的red,green,blue三色对255做差
		            r=255-red;
		            g=255-green;
		            b=255-blue;
		            
		            newBitmap.setPixel(i, j, Color.argb(alpha, r, g, b));
		        }  
		    }  
		    return newBitmap;
		}











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值