views:

541

answers:

1

I am using the previewCallback from the camera to try and grab images. Here is the code I am using

private Camera.PreviewCallback mPrevCallback = new Camera.PreviewCallback() 
{
        public void onPreviewFrame( byte[] data, Camera Cam ) {
                Log.d("CombineTestActivity", "Preview started");
                Log.d("CombineTestActivity", "Data length = " 
                        + data.length );
                currentprev = BitmapFactory.decodeByteArray( data, 0, 
                        data.length );

               if( currentprev == null )
                   Log.d("CombineTestActivity", "currentprev is null" );

                Log.d("CombineTestActivity", "Preview Finished" );

        }
};

the length of the data always comes otu the same as 576000.

Also I have tried changing the parameters of the camera so the image comes back as different formats. Here is what it looks like when I do that.

mCamera = Camera.open();
camParam = mCamera.getParameters();
camParam.setPreviewFormat( ImageFormat.RGB_565 );
mCamera.setParameters( camParam );
    mCamera.setPreviewCallback( mPrevCallback );

However both when I change the preview format and when I leave it as its default of NV21, BitmapFactory.decodeByteArray comes back as null. I have also tried changing the preview format to JPEG type. I even get a debug statement in the ddms, this is what I get

"D/skia (14391): --- SkImageDecoder::Factory returned null"

+2  A: 

I'm trying to do the same thing. Based on the discussions here and here, it sounds like people have not had luck getting decodeByteArray() to handle NV21 format as of Android 2.1/2.2. It definitely doesn't work in my emulator or on my Droid Incredible, although I think this calls native code so it may work on some phones depending on the drivers?

As an alternative, you can try to decode the NV21 yourself in Java (see link above for an example), although this is apparently too slow to be useful for most cases. I haven't had much luck trying to get CameraPreview to send a different format either, and I would expect this to be problematic for trying to write code that is portable across different hardware. If you wrote the NV21 decode methods in NDK you might get the framerate up a bit.

Apparently there are stability problems due to race conditions in trying to process the CameraPreview too, although I haven't confirmed this issue myself. I think you might avoid this and also get your framerate up a bit by using the buffered preview callback method setPreviewCallbackWithBuffer() that was added in Android 2.1/2.2. This was added in 2.1 but was left hidden until 2.2. To use it in 2.1 you need to hack around the hiding.

Some people have suggested using MediaRecorder instead of CameraPreview. Unfortunately, MediaRecorder appears to have even less provided for getting preview frames than CameraPreview, so I can't recommend that route.

Chinasaur