Member-only story
Processing Images Fast With Native Code in Android
See how native code is better for your app’s performance
Performance is a feature for most of the software products out there, but there are few programs that are more performance-sensitive than the others. I work on an Android Camera App and my team gives very high priority to performance.
[Google found that] the page with 10 results took 0.4 seconds to generate. The page with 30 results took 0.9 seconds. Half a second delay caused a 20% drop in traffic. Half a second delay killed user satisfaction.
I don’t need to oversell the importance of performance
to my manager or to you, but I came across this snippet which strengthens the performance is a feature construct - it's good to know fact.
If you are writing applications that process large images captured with a camera or an existing image on the device you need to be extra careful. These days cameras on phones are easily equipped with high-resolution sensors. It’s easy to find 13MP (MegaPixels), 24MP, 48MP, or even 108MP cameras now being shipped on Android devices.
Let’s look at a 13MP image. It has 13,000,000 pixels. If you wanted to do just one simple computation on the image let’s say increase the exposure of the image i.e. for every pixel
image(x, y) = std::clamp(alpha * image(x, y) + beta, 0, 255);
You need to do it 13 million times.
The smartphones these days are also equipped with multicore, SIMD-supported CPUs, so there are ways to do it faster than serialized 13 million iterations but at the same time the types of algorithms we want to run are usually much more complex than the one I just stated.
In my experience, it’s both easier and better to handle these complex image processing operations with native code very particularly to keep it performant.
This is a very basic article demonstrating how to do image processing with native code in Android. I’ll also show by an example that the performance of a very simple and unoptimized C++ code comes very close to fairly optimized Java code for the same problem statement. If you are looking for “Fast image processing using Java Native Interface or JNI in Android” — I…