android 图片过滤,android 图片转为bitmap,黑白镜过滤

图片转bitmap

1.获取图片资源

Bitmap bitmap= BlackWhite(BitmapFactory.decodeResource(getResources(), R.mipmap.test, null));

2.sd卡(要获取权限 文件和读写权限)

String fileName=Environment.getExternalStorageDirectory().getAbsolutePath()+"/tencent/MicroMsg/WeiXin/test.jpeg";

Bitmap bit = BitmapFactory.decodeFile(fileName);

下面是黑白镜过滤的方法

public static Bitmap BlackWhite(Bitmap bitmap) {

int w = bitmap.getWidth();

int h = bitmap.getHeight();

Bitmap resultBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);

int color = 0;

int a, r, g, b, r1, g1, b1;

int[] oldPx = new int[w * h];

int[] newPx = new int[w * h];

bitmap.getPixels(oldPx, 0, w, 0, 0, w, h);

for (int i = 0; i < w * h; i++) {

color = oldPx[i];

r = Color.red(color);

g = Color.green(color);

b = Color.blue(color);

a = Color.alpha(color);

//黑白矩阵

r1 = (int) (0.33 * r + 0.59 * g + 0.11 * b);

g1 = (int) (0.33 * r + 0.59 * g + 0.11 * b);

b1 = (int) (0.33 * r + 0.59 * g + 0.11 * b);

//检查各像素值是否超出范围

if (r1 > 255) {

r1 = 255;

}

if (g1 > 255) {

g1 = 255;

}

if (b1 > 255) {

b1 = 255;

}

newPx[i] = Color.argb(a, r1, g1, b1);

}

resultBitmap.setPixels(newPx, 0, w, 0, 0, w, h);

return resultBitmap;

}

下面这个方法也是可以的

public static Bitmap getGrayBitmap(Bitmap bm){

Bitmap bitmap = null;

//获取图片的宽和高

int width = bm.getWidth();

int height = bm.getHeight();

//创建灰度图片

bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

//创建画布

Canvas canvas = new Canvas(bitmap);

//创建画笔

Paint paint = new Paint();

//创建颜色矩阵

ColorMatrix matrix = new ColorMatrix();

//设置颜色矩阵的饱和度:0代表灰色,1表示原图

matrix.setSaturation(0);

//颜色过滤器

ColorMatrixColorFilter cmcf = new ColorMatrixColorFilter(matrix);

//设置画笔颜色过滤器

paint.setColorFilter(cmcf);

//画图

canvas.drawBitmap(bm, 0,0, paint);

return bitmap;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值