Skip to content

Instantly share code, notes, and snippets.

@frogermcs
Created February 22, 2019 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frogermcs/d3d4a91388b903084b0d941c7d005830 to your computer and use it in GitHub Desktop.
Save frogermcs/d3d4a91388b903084b0d941c7d005830 to your computer and use it in GitHub Desktop.
private ByteBuffer convertBitmapToByteBuffer(Bitmap bitmap) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(MODEL_INPUT_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[INPUT_IMG_SIZE_WIDTH * INPUT_IMG_SIZE_HEIGHT];
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
int pixel = 0;
for (int i = 0; i < INPUT_IMG_SIZE_WIDTH; ++i) {
for (int j = 0; j < INPUT_IMG_SIZE_HEIGHT; ++j) {
final int val = intValues[pixel++];
byteBuffer.putFloat((((val >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
byteBuffer.putFloat((((val >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
byteBuffer.putFloat((((val) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
}
}
return byteBuffer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment