Created
July 16, 2019 13:29
-
-
Save obeshor/23c4e10f56692cfefdd9c5a47a01b7f8 to your computer and use it in GitHub Desktop.
This file contains 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 fun convertBitmapToByteBuffer(bitmap: Bitmap): ByteBuffer { | |
val byteBuffer = ByteBuffer.allocateDirect(4 * INPUT_SIZE * INPUT_SIZE * PIXEL_SIZE) | |
byteBuffer.order(ByteOrder.nativeOrder()) | |
val intValues = IntArray(INPUT_SIZE * INPUT_SIZE) | |
bitmap.getPixels(intValues, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height) | |
var pixel = 0 | |
for (i in 0 until INPUT_SIZE) { | |
for (j in 0 until INPUT_SIZE) { | |
val `val` = intValues[pixel++] | |
byteBuffer.putFloat((((`val`.shr(16) and 0xFF) - IMAGE_MEAN) / IMAGE_STD)) | |
byteBuffer.putFloat((((`val`.shr(8) and 0xFF) - IMAGE_MEAN) / IMAGE_STD)) | |
byteBuffer.putFloat((((`val` and 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