何为马赛克?
实现的思路就是选定一定大小的矩形区域,用其中任意一点的像素来填充整个区域,这样就实现了马赛克滤镜。
代码如下:
//马赛克 public static Bitmap Masic(Bitmap bitmap){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap result = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565); int[] inPixels = new int[width*height]; int[] outPixels = new int[width*height]; bitmap.getPixels(inPixels, 0, width, 0, 0, width, height); int index = 0; int offsetX = 0, offsetY = 0; int newX = 0, newY = 0; int size = 10; double total = size * size; double sumred = 0, sumgreen = 0, sumblue = 0; for(int row = 0; row < height; row++) { int ta = 0, tr = 0, tg = 0, tb = 0; for(int col = 0; col < width; col++) { newY = (row / size) * size; newX = (col / size) * size; offsetX = newX + size; offsetY = newY + size; for(int subRow = newY; subRow < offsetY; subRow++) { for(int subCol = newX; subCol < offsetX; subCol++) { if(subRow < 0 || subRow >= height) { continue; } if(subCol < 0 || subCol >= width) { continue; } index = subRow * width + subCol; ta = (inPixels[index] >> 24) & 0xff; sumred += (inPixels[index] >> 16) & 0xff; sumgreen += (inPixels[index] >> 8) & 0xff; sumblue += inPixels[index] & 0xff; } } index = row * width + col; tr = (int)(sumred / total); tg = (int)(sumgreen / total); tb = (int)(sumblue / total); outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb; sumred = sumgreen = sumblue = 0; // reset them... } } result.setPixels(outPixels, 0, width, 0, 0, width, height); return result; }效果如下:
效果图 原图
作者:qq_32353771 发表于2017/1/6 20:17:18 原文链接
阅读:59 评论:0 查看评论