1

background

i'm developing an app that at some point it has a huge bitmap and it needs to use face detection on it, so i use the FaceDetector API

the problem

the face detection API has restrictions about its input bitmap:

  1. the bitmap config must be Config.RGB_565 (written here)
  2. the bitmap width must be even (written here)

what i've done

since i need to have the original bitmap later, i've stored it into a file.

right after that i recycle the original bitmap, and load a temporary one from the file, having the right config, but sadly, it won't always work since i need to use an even width, and the original bitmap doesn't always have an even width.

i've tried telling the face detection API to use "width-1" as the width, but it didn't work. it said "java.lang.IllegalArgumentException: bitmap size doesn't match initialization". i don't even understand why it needs such a weird width restriction and why it needs the parameters of the width and height if it already has the bitmap...

the question

what should i do?

is it possible to make it work with odd-width bitmaps , and maybe ignore a whole column of pixels?

the reason i ask this is because the bitmap is quite large, and if i clone it to a new one that has a similar size (with an even width), i can easily get OOM on some devices.

i know i can scale using JNI (similar to a post i've written some time ago,here) , but i hope i can find a better, less hardcore-way to achieve the same thing.

if there is no other way, maybe android has a JNI functions that could help ?

7
  • Halving the size won't make the width even - in fact it will turn many even widths odd. The even width requirement is probably to enable some access optimization in the engine. It seems like you either want to achieve compliance at the source, or re-interpret the bitmap while reading it back from disk, so that you don't have the old and new versions in memory at once, though also downsampling at the same time might be a good idea if the result would still be sufficient for the algorithm to operate one. Jul 14, 2013 at 14:39
  • @ChrisStratton i meant i should halve the size only if the width is odd. if you take a width of 13 and halve it, you should get 6 , but even then, on some cases you will get an odd number (like from 11 to 5) ... in any case, i know it's a bad solution (and making a bitmap that is very similar in size to the original one is even worse in terms of memory usage) so that's why i ask here if there is a good alternative. Jul 14, 2013 at 18:30
  • i've removed the part i've written about halving the width since it doesn't make sense. Jul 14, 2013 at 18:47
  • You might want to try the new face detection API for Android, which doesn't have these limitations: developers.google.com/vision
    – pm0733464
    Sep 2, 2015 at 14:59
  • @pm0733464 How will it work on devices that don't have the play services installed? Sep 2, 2015 at 17:30

1 Answer 1

0

This can be used for converting the bitmap in the required format (The bitmap must be in 565 format).

Bitmap mFaceBitmap = bitmapImage.copy(Bitmap.Config.RGB_565, true); bitmapImage.recycle(); // If you want to free memory ASAP

And to alter the width, you can try something like

mFaceBitmapWidth = mFaceBitmap.getWidth(); mFaceBitmap.setWidth(mFaceBitmapWidth - 1);

3
  • Pretty sure it will create a new bitmap, thus using double the memory can can trigger OOM. May 31, 2015 at 6:21
  • @androiddeveloper you can call bimapImage.recycle(); that will free the memory ASAP.
    – Raghav
    May 31, 2015 at 9:43
  • I know, but this still means that till you call it, you have 2 huge bitmaps, that can cause OOM. May 31, 2015 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.