Android bitmap image / graphic reporting incorrect / wrong pixel size / dimensions

Is a bitmap image / graphic you've loaded in your Android code reporting its dimensions different from what you know it really is - even though all you've done with it is BitmapFactory.decodeResource?

Did you put your image in the drawable directory? (thinking drawable-hdpi, drawable-mdpi and drawable-ldpi were for different resolution graphics - but 'drawable' has no dpi reference in the name, so why would items loaded from here have any resolution specific measures applied?)

Well you (and I) were wrong. Images loaded from the drawable directory are automatically resized, depending on the resolution you're working in - not always what you want (nor would expect when there are other drawable directories which explicitly reference different dpi levels).

To prevent this, create a new directory called 'drawable-nodpi' (at the same level as the drawable directory) and move your images / graphics into there. You still use the same R.drawable.whatever to reference them - but they won't have any automatic scaling applied.

If Eclipse gives you any trouble, use Project > Clean... (which seems to rescan the drawable directories).

Tagged .

NV21 / YUV to greyscale (or grayscale) conversion for Android camera preview

This was very helpful (and took a while to find): NV21 / YUV to greyscale (or grayscale) conversion for Android.

If you only need grayscale information, the first (width * height) bytes of the preview are provided as unsigned byte intensities.

If you call print on the byte array entries directly the values are incorrect (they are presumed to represent signed integers); you'll need to convert each byte value to an unsigned integer yourself:

int value = (int) _data[i] & 0xFF;

(this may not be the most optimal way, but it seems to work).

The specifics of the format are detailed at YUV pixel formats at FOURCC.org (you'll need to read the NV12 entry as well) but these didn't make sense until I had read the post in the first link.

Tagged , and .

Most used blog post tags

Blog entries by month

  1. January 2012 (1)
  2. August 2011 (2)
  3. March 2011 (1)
  4. February 2011 (1)
  5. November 2010 (1)
  6. October 2010 (1)
  7. August 2010 (2)
  8. January 2009 (1)
  9. December 2008 (1)
  10. November 2008 (1)
  11. October 2008 (2)
  12. September 2008 (3)