• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps accessing grayscale data from a photo

Hi,

I am wondering how do I go about writing code (or be directed to any existing relevant code), that allows me to average the grayscale values above a threshold (Otsu threshold) from a JPEG photo taken from an android device camera.

Thank you
 
I don't know about the camera API's, but implementing a basic filter which works on a JPEG should be easy. You can do a trivial implementation in Java by loading your JPEG image into an array of bytes (mBytes). Like this (obviously you need to replace myInputStream with something sensible, it could be an InputStream associated with a file on disk):

Code:
BufferedInputStream buffer = new BufferedInputStream(myInputStream);
ByteArrayBuffer baf = new ByteArrayBuffer(MAX_IMAGE_SIZE);
int current = 0;
while ((current = buffer.read()) != -1) {
   baf.append((byte) current);
}
mBytes = baf.toByteArray();
Then you can use this API to decode it into a Bitmap object:
Code:
Bitmap bmp = BitmapFactory.decodeByteArray(mBytes, 0, mBytes.length);
Once you have a Bitmap object you can get and set the color values of individual pixels. You should be able to iterate over all the pixels in the image and calculate your average. Then iterate over the image again replacing any values above the threshold.

You and easily view your Bitmap by in an ImageView by calling the ImageView::setImageBitmap() api.

Obviously the performance will be bad. If you need good performance (i.e. to do a live camera viewfinder etc.. You probably need to use native code and the NEON SIMD co-processor found on most modern ARM CPU's, but I can't help you with that.)
 
Back
Top Bottom