Created
April 1, 2019 13:52
-
-
Save Ayyagaries/86025301ff2cea6f71cef176943eb2ab to your computer and use it in GitHub Desktop.
bitmapTo8BitImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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