Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(MnistModelConfig.MODEL_INPUT_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
int[] pixels = new int[MnistModelConfig.INPUT_IMG_SIZE_WIDTH * MnistModelConfig.INPUT_IMG_SIZE_HEIGHT];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int pixel : pixels) {
float rChannel = (pixel >> 16) & 0xFF;
float gChannel = (pixel >> 8) & 0xFF;
float bChannel = (pixel) & 0xFF;
float pixelValue = (rChannel + gChannel + bChannel) / 3 / 255.f;
byteBuffer.putFloat(pixelValue);
}
return byteBuffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.