Skip to content

Instantly share code, notes, and snippets.

@xiazdong
Last active May 16, 2017 02:02
Show Gist options
  • Save xiazdong/2bf5aa59575326bda29178a5a344304b to your computer and use it in GitHub Desktop.
Save xiazdong/2bf5aa59575326bda29178a5a344304b to your computer and use it in GitHub Desktop.
Pixelate Algorithm
public class PixelateUtil {
/**
* 普通图像->像素图,zoneWidth为像素图的大像素的宽度
*/
public static Bitmap pixelate(Bitmap bitmap, int zoneWidth) {
return pixelate(bitmap, zoneWidth, 0, 0, bitmap.getWidth(), bitmap.getHeight());
}
/**
* 普通图->像素图,left、top、right、bottom可指定打马赛克区域
*/
public static Bitmap pixelate(Bitmap bitmap, int zoneWidth, int left, int top, int right, int bottom) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap result = bitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = left; i < right; i += zoneWidth) {
for (int j = top; j < bottom; j += zoneWidth) {
int color = bitmap.getPixel(i, j);
paint.setColor(color);
int gridRight = Math.min(w, i + zoneWidth);
int gridBottom = Math.min(h, j + zoneWidth);
canvas.drawRect(i, j, gridRight, gridBottom, paint);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment