imgData = ByteBuffer.allocateDirect((if (isQuantized) 1 else 4) * IMG_SIZE * IMG_SIZE * 3) .apply { order(ByteOrder.nativeOrder()) rewind() } val pixels = IntArray(IMG_SIZE * IMG_SIZE) val createScaledBitmap = Bitmap.createScaledBitmap(bitmap, IMG_SIZE, IMG_SIZE, false) createScaledBitmap.apply { getPixels(pixels, 0, width, 0, 0, width, height) } pixels.forEach { value -> imgData.add(value shr 16 and 0xFF) imgData.add(value shr 8 and 0xFF) imgData.add(value and 0xFF) } return imgData } private fun ByteBuffer.add(pixel: Int) = if (isQuantized) put(pixel.toByte()) else putFloat((pixel - MEAN) / STD)