Numair
Numair

Reputation: 1062

Byte array image to bitmap

I'm trying to convert the byte array image to bitmap, but after conversion it gives me black image, why?

// Camera arg conversion to Bitmap
                Bitmap cameraBitmap = BitmapFactory.decodeByteArray(arg0, 0,
                        arg0.length);
                 Bitmap background = Bitmap.createBitmap(cameraBitmap.getWidth(),
                 cameraBitmap.getHeight(), Bitmap.Config.ARGB_8888);

I want the camera image converted to bitmap. Any suggestion. Thanks in advance!!

07-17 02:22:18.149: E/AndroidRuntime(398): FATAL EXCEPTION: main
07-17 02:22:18.149: E/AndroidRuntime(398): java.lang.IllegalArgumentException: only support ImageFormat.NV21 and ImageFormat.YUY2 for now
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.graphics.YuvImage.<init>(YuvImage.java:82)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.exercise.AndroidCamera.AndroidCamera$4.onPictureTaken(AndroidCamera.java:225)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.hardware.Camera$EventHandler.handleMessage(Camera.java:320)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.os.Looper.loop(Looper.java:123)
07-17 02:22:18.149: E/AndroidRuntime(398):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 02:22:18.149: E/AndroidRuntime(398):  at java.lang.reflect.Method.invokeNative(Native Method)
07-17 02:22:18.149: E/AndroidRuntime(398):  at java.lang.reflect.Method.invoke(Method.java:521)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 02:22:18.149: E/AndroidRuntime(398):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 02:22:18.149: E/AndroidRuntime(398):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 6717

Answers (1)

Viktor Latypov
Viktor Latypov

Reputation: 14467

Unfortunately, the camera's image format is not ARGB_8888. Most likely it's something like YUV420p or maybe even the JPEG-compressed data.

First of all, determine what is your case.

You have two options: for YUV (which is an interleaved format) use some conversion format and for JPEG create a memorystream for your arg0 array and read the Bitmap from it.

There are similar questions here: BitmapFactory null issue in android

And there is even a solution here: Android byte[] to image in Camera.onPreviewFrame

EDIT: you just have to mangle the bytes in your array a little.

This wiki article explains how to convert the YUV422/420 to YUY2, required by the Android API. Search for the "Y'UV422 can also be expressed in YUY2 FourCC format code" substring there.

Upvotes: 3

Related Questions