Skip to content

Instantly share code, notes, and snippets.

@massimilianochiodi
Created April 15, 2022 08:58
Show Gist options
  • Save massimilianochiodi/7892538b20de324f8553a0fc43a138a5 to your computer and use it in GitHub Desktop.
Save massimilianochiodi/7892538b20de324f8553a0fc43a138a5 to your computer and use it in GitHub Desktop.
Convert bitmap image in B/N (Java)
public Bitmap convertibianconero(Bitmap bmp) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
int alpha = 0xFF << 24; // ?bitmap?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) (red * 0.3 + green * 0.59 + blue * 0.11);
grey = alpha | (grey << 16) | (grey << 8) | grey;
pixels[width * i + j] = grey;
}
}
Bitmap newBmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
return newBmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment