简体   繁体   中英

BitmapFactory.decodeByteArray() returns null on binarized Bitmap

I have a byte array with series of 0 and -1 (255). It's a result of binarization using the Otsu algorithm. I used:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.RGB_565; // I have tried ARGB_8888 aswell

Bitmap out = BitmapFactory.decodeByteArray(data, 0, data.length, opt);

Unfortunately, it returns null. Just like the other questions regarding BitmapFactory.decodeByteArray() .

I have tested other methods like nested for loops, it works but it takes too long to process, especially for large images.

This is what I currently use to generate the binarized data :

ptr = 0;
while (ptr < srcData.length)
{
    monoData[ptr] = ((0xFF & srcData[ptr]) >= threshold) ? (byte) 255 : 0;
    ptr ++;
}

I hope you can lead me to a better way on solving this issue. Thanks!

565 is a representation that uses 2 bytes per pixel.

Moreover, decodeByteArray is a method that reads a compressed byte[] (see documentation).

Here is what I would do :

  • use a int[] rather than a byte[].
  • initialize like follows
  • use Bitmap.html#createBitmap() method with the int[] resulting. The config used is not really important at this point.

monoData[ptr] = ((0xFF & srcData[ptr]) >= threshold) ? 0xffffffff : 0xff000000;

nota : one thing that could have helped you : decodeByteArray has no idea of the geometry of your image, hence can of course not decode it to what you are expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM