Question

I am trying to create bitmap class using bitmapfactory. I get camera preview image in YUV format and decode manually to grayscale image. When i try to create bitmap object via BitmapFactory it returns null.

        try {
            for (int y = 0; y < frameHeight; y++){
                for (int x = 0; x < frameWidth; x++){

                    byte grey = YUVData[y * frameWidth + x];
                    convertedData[y * stride + 3 * x] = grey;
                    convertedData[y * stride + 3 * x + 1] = grey;
                    convertedData[y * stride + 3 * x + 2] = grey;

                }
            }

            Bitmap bitmap =(Bitmap) BitmapFactory.decodeByteArray(convertedData, 0, convertedData.length);
Was it helpful?

Solution

BitmapFactory is used to create an image from a image file that was encoded using JPEG or PNG. If you just toss raw data at it, the function has no idea what encoding you're using. RGB? YUV? 24-bit? 32-bit?

AFAIK there's no native way to create an image from YUV data, you'd at least have to convert it to RGB first. Here is an answer that demonstrates it: Getting frames from Video Image in Android.

Of course, you can use the NDK to create a native conversion function, which will be a lot faster. Or you could scan the documentation for a YUV converter, but the only thing I found in a quick scan is YuvImage, which will only allow you to convert this to a JPEG stream.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top