Skip to content

Instantly share code, notes, and snippets.

Last active Feb 17, 2022
What would you like to do?
Converting pytorch tensor / floatArray to Android Bitmap
fun floatArrayToGrayscaleBitmap (
floatArray: FloatArray,
width: Int,
height: Int,
alpha :Byte = (255).toByte(),
reverseScale :Boolean = false
) : Bitmap {
// Create empty bitmap in RGBA format (even though it says ARGB but channels are RGBA)
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val byteBuffer = ByteBuffer.allocate(width*height*4)
// mapping smallest value to 0 and largest value to 255
val maxValue = floatArray.maxOrNull() ?: 1.0f
val minValue = floatArray.minOrNull() ?: 0.0f
val delta = maxValue-minValue
var tempValue :Byte
// Define if float min..max will be mapped to 0..255 or 255..0
val conversion = when(reverseScale) {
false -> { v: Float -> ((v-minValue)/delta*255).toInt().toByte() }
true -> { v: Float -> (255-(v-minValue)/delta*255).toInt().toByte() }
// copy each value from float array to RGB channels and set alpha channel
floatArray.forEachIndexed { i, value ->
tempValue = conversion(value)
byteBuffer.put(4*i, tempValue)
byteBuffer.put(4*i+1, tempValue)
byteBuffer.put(4*i+2, tempValue)
byteBuffer.put(4*i+3, alpha)
return bmp
Copy link

GeghamHayrapetian commented Aug 11, 2020

please help me . I want to use this code but it throw this exception java.lang.IndexOutOfBoundsException: index=640000 out of bounds (limit=640000).

Copy link

Aearcoding commented Jan 22, 2021

Could you provide the code of java?

Copy link

phillies commented Jan 22, 2021

@GeghamHayrapetian Where does it throw that exception? Can you provide more context?

@Aearcoding: Sorry, I'm not a java coder, but seems like IntelliJ IDEA / Android Studio can convert kotlin to java code.

Copy link

EricJKing commented Sep 4, 2021

IndexOutOfBoundsException. LOL

Copy link

EricJKing commented Sep 5, 2021

SomeThing Like This:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cameraa, PID: 15280
java.lang.IndexOutOfBoundsException: index=264196 out of bounds (limit=264196)
at java.nio.Buffer.checkIndex(
at java.nio.DirectByteBuffer.put(
at com.example.cameraa.MainActivity.onCreate$lambda-2(MainActivity.kt:123)
at com.example.cameraa.MainActivity.$r8$lambda$2jojTiyhKvSV11AyQ-LBlWnMfGU(Unknown Source:0)
at com.example.cameraa.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(
at android.view.View.performClickInternal(
at android.view.View.access$3600(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

Copy link

phillies commented Sep 23, 2021

You have to make sure your input parameter have matching sizes. An IndexOutOfBoundsException can occur in the loop lines 26-32 if the floatArray contains more than widthxheight entries. Check floatArray.size == (width*height) before calling the function.

Copy link

akhil-rkrishnan commented Oct 20, 2021

The final bitmap is returing black and white. The area of object detected is showing in white color and rest area showing black. Could you please help with this.

Copy link

phillies commented Oct 22, 2021

The function maps the smallest value in your floatArray to 0 (black) and the largest value to 255 (white). If you want to have it the other way round, so the smallest value is white and the largest are black, then reverseScale should be true.
I assume that your object detection function delivers a float array where background is 0 and detected objects are >0, so if you want to have the detected objects in black and the background in white set reverseScale=true then calling the function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment