views:

416

answers:

2

I am writing an app to capture the camera preview frames and convert it to bitmap in Android. Here is my code:

   Camera.PreviewCallback previewCallback = new Camera.PreviewCallback()  
    { 
            public void onPreviewFrame(byte[] data, Camera camera)  
            { 
                    try 
                    { 
                            BitmapFactory.Options opts = new BitmapFactory.Options(); 
                            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//,opts); 
                    } 
                    catch(Exception e) 
                    {

                    } 
            } 

    }; 

    mCamera = Camera.open();
    mCamera.setPreviewCallback(previewCallback); 

After I start preview, the callback got called with data, but the bitmap is null.

What did I do wrong when convert the byte array to BitMap?

A: 

Same question as http://stackoverflow.com/questions/3338235/bitmapfactory-decodebytearray-is-returning-null (unanswered ATOW)

OJW
A: 

Hi Hongwei,

Have you tried decoding the preview frame data to RGB before you use BitmapFactory? The default format is YUV which I'm not sure is compatible with BitmapFactory. Dave Manpearl's decode method can be found here:

http://stackoverflow.com/questions/1893072/getting-frames-from-video-image-in-android

Let me know if it works.

Cheers,

Paul

pwon0600