Skip to content

Instantly share code, notes, and snippets.

@Ayyagaries
Created April 1, 2019 13:52
Show Gist options
  • Save Ayyagaries/86025301ff2cea6f71cef176943eb2ab to your computer and use it in GitHub Desktop.
Save Ayyagaries/86025301ff2cea6f71cef176943eb2ab to your computer and use it in GitHub Desktop.
bitmapTo8BitImage
private byte[] bitmapTo8BitImage(Bitmap bitmap) {
int[] pixels = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0,
bitmap.getWidth(), bitmap.getHeight());
ByteBuffer eightByteBuffer = ByteBuffer.allocate(pixels.length);
for (int i = 0; i < pixels.length; i++) {
int p = pixels[i];
int red = Color.red(p);
int green = Color.green(p);
int blue = Color.blue(p);
byte p1Rgb = (byte) (((int) Math.round(red / 255.0 * 7.0) << 5) |
((int) Math.round(green / 255.0 * 7.0) << 2) |
((int) Math.round(blue / 255.0 * 3.0)));
eightByteBuffer.put(p1Rgb);
}
return eightByteBuffer.array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment