Fresco编辑图片成黑白色

项目中有个需求,需要把图片转成黑白色,之后变回原图的彩色,由于图片框架使用的是facebook的fresco

,查看文档有个后处理器postprocessor,其实也比较简单

/**
	 * 显示图片灰度的后处理器
	 */
	Postprocessor redMeshPostprocessor = new BasePostprocessor() {
		@Override
		  public String getName() {
		    return "redMeshPostprocessor";
		  }

		  @Override
		  public void process(Bitmap bitmap) {
		        int width = bitmap.getWidth();         //获取位图的宽  
		        int height = bitmap.getHeight();       //获取位图的高  
		        int []pixels = new int[width * height]; //通过位图的大小创建像素点数组  
		        bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 
		        int alpha = 0xFF << 24;  
		        for(int i = 0; i < height; i++)  { 
		            for(int j = 0; j < width; j++) { 
		                int grey = pixels[width * i + j]; 
		                int red = ((grey  & 0x00FF0000 ) >> 16); 
		                int green = ((grey & 0x0000FF00) >> 8); 
		                int blue = (grey & 0x000000FF); 
		                grey = (int)((float) red * 0.3 + (float)green * 0.59 + (float)blue * 0.11); 
		                grey = alpha | (grey << 16) | (grey << 8) | grey; 
		                pixels[width * i + j] = grey; 
		            } 
		        } 
		        bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
		      }
	};

调用方式

               ImageRequest request = ImageRequestBuilder.newBuilderWithSource(currentImgUrl)
				    .setPostprocessor(redMeshPostprocessor)
				    .build();
				  PipelineDraweeController controller = (PipelineDraweeController)
				    Fresco.newDraweeControllerBuilder()
				    .setImageRequest(request)
				    .setOldController(guess_img.getController())
				    // other setters as you need
				    .build();
				  guess_img.setController(controller);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值