Coding for Android on Windows- Image processing - C++ Code

by Christian Tendyck

This page is the fourth chapter of the article Coding for Android on Windows.
Feel free to write comments or add content.


Image processing on an Android phone - Lowpass filter an Image - C++ code

As you have already seen in the JNI version of "HelloWorld", the C++ code looks different from what you are used to when codeing in C/C++, nut once you are used to this "new code", it's not a problem anymore.
We are just using one function that's working on the array. The result of this function is also an one dimensional array. First of all, we create a C-pointer that points on the beginning of the Java array and a two dimensional C array. The values inside the Java array are 32-digits binary numbers. The first 8 Bits are used for the transparency level of the image and the next 24 Bits for the RGB values (first 8 red, than green, than Blue). We devide these values in the separate color components and write these values inside the C array:

int temp=0;
for(int j=0; j<h; j++)
for(int i=0; i<(w*3); i=i+3)
{
temp = CPixels[i/3+j*w];
array[i][j] = (temp >> 16) & 0xff;
array[i+1][j] = (temp >> 8) & 0xff;
array[i+2][j] = (temp) & 0xff;
}

We do not take care of the transparency information anymore. The values inside the two dimensional array are now arranged in the following form:

r00 g00 b00 r01 g01 b01 r02 g02 b02
r10 g10 b10 r11 g11 b11 r12 g12 b12 ...
r20 g20 b20 r21 g21 b21 r22 g22 b22
...

That means the first three values in the first row contain the RGB values of the first pixel in the first row, the next three values contain the RGB values for the second pixel in the first row and so on.
Now that we have a C array, we can use regular C code that we are probably more used to. The filtering itself is not really complicated: We just use a filter in form of a matrix and perform a convolution. As this is not really an issue of programming for Android, I won't tell to much about it. But if you do not understand the code or have questions, don't hesitate to write a comment.
After we have filtered all the values inside the array, we have to form one value out of the RGB values again and create an array that becomes the result of this JNI function.

THE END

Previous Chapter: Image Processing - Java Code


Back to Coding for Android on Windows

Alumni Liaison

Prof. Math. Ohio State and Associate Dean
Outstanding Alumnus Purdue Math 2008

Jeff McNeal