现在很多相机应用都有图片滤镜,实现各种效果,素描是其中一种,我们怎么实现呢?
找到个栗子->使用canvas把照片转换成素描画,作者把原理讲的很清楚了,包括以下几步:
- 去色,将图片变为灰度图,即黑白图;
- 反相,得到每个像素的补色,具体效果就像照片的底片;
- 高斯模糊,把反相后的像素值平均一下;
- 颜色减淡,将第1步中的像素和第3步得到的像素值进行计算。
去色
public static int[] discolor(Bitmap bitmap) {
int picHeight = bitmap.getHeight();
int picWidth = bitmap.getWidth();
int[] pixels = new int[picWidth * picHeight];
bitmap.getPixels(pixels, 0, picWidth, 0, 0, picWidth, picHeight);
for (int i = 0; i < picHeight; ++i) {
for (int j = 0; j < picWidth; ++j) {
int index = i * picWidth + j;
int color = pixels[index];
int r = (color & 0x00ff0000) >> 16;
int g = (color & 0x0000ff00) >> 8;
int b = (color & 0x000000ff);
int grey = (int) (r * KR + g * KG + b * KB);
pixels[index] = grey << 16 | grey << 8 | grey | 0xff000000;
}
}
return pixels;
}
反相
public static int[] reverseColor(int[] pixels) {
int length = pixels.length;
int[] result = new int[length];
for (int i = 0; i < length;