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 ?

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • 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. – Chris Stratton Jul 14 '13 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. – android developer Jul 14 '13 at 18:30
  • i've removed the part i've written about halving the width since it doesn't make sense. – android developer Jul 14 '13 at 18:47
  • You might want to try the new face detection API for Android, which doesn't have these limitations: https://developers.google.com/vision/ – pm0733464 Sep 02 '15 at 14:59
  • @pm0733464 How will it work on devices that don't have the play services installed? – android developer Sep 02 '15 at 17:30
  • Since is provided as part of Google Play Services, it is not available on devices that don't have play services installed. – pm0733464 Sep 02 '15 at 19:20
  • @pm0733464 That's too bad. Should be a part of Android framework – android developer Sep 03 '15 at 05:18

1 Answers1

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);

Raghav
  • 1
  • 1