Skip to content
Advertisement

Java / Kotlin – best approach for fast pixel wise image operations

Overall Problem – I want to do fast image modification in Android, and my current naive solution is too slow

I’m building an Android app that will involve fast modification of streaming images. I am running it on a Samsung Galaxy S10. I’m new to Android, Java, and Kotlin, so please forgive any ignorance on my part.

I’ve successfully decoded the video such that each frame is read into a Bitmap.

As a simple test, I’ve made the following function:

JavaScript

Which outputs

JavaScript

However it’s much slower than I expected – at around 0.6us/pixel, it would take around 0.5s per image (I use 500 x 200 in this demo because when I use the full image size, the thread running the function seems to be killed before completing)

For comparison, doing the same thing in Python…:

JavaScript

… on my MacBook Air takes around 0.3ms (vs the 60ms on Galaxy 10 in Kotlin.).

So, question – what is the standard, efficient way to do such things in Android? Should I not be using native kotlin and instead something like MultiK? Or am I just doing things in an inefficient way natively?

Advertisement

Answer

So it looks like it is much faster to operate on the pixels as an array. This implementation is over 100x faster:

JavaScript

… at around 4ms per call (doing every pixel) – as opposed to around 550ms per call when using BitMap.setPixel

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement