views:

72

answers:

3

I am trying to convert my SurfaceView (camera preview) into a Bitmap for face detection on the fly. I am receiving a not null image but when I display it onto a view it appears to be plain black. Any ideas what might be the reason and how to proceed?

(I believe it is difficult but not impossible to extract the bitmap from a SurfaceView - but nobody has posted any solution)

class BackgroundView extends SurfaceView implements SurfaceHolder.Callback {


        public BackgroundView(Context context) {
            super(context);

                    // ...

            setDrawingCacheEnabled(true);
        }


        // ...
    }

    private Runnable update = new Runnable() {
        public void run() {

                    // Following statement is sending a black/blank image
            faceView.updateFaces(backgroundView.getDrawingCache());
            mHandler.postDelayed(update, (long) (1000));
        }
    };
A: 

I'm having a similar problem trying to get the video frames from a VideoView. I've tried all kinds of combinations of these flags:

   vids[i] = new VideoView(this);
   vids[i].setDrawingCacheEnabled(true);
   vids[i].setWillNotCacheDrawing(false);
   vids[i].setWillNotDraw(false);

... (later in another View's draw() loop)

curFrame = vids[0].getDrawingCache();
if (curFrame != null) {
   canvas.drawBitmap(curFrame, null, new RectF(10,y,50,y+50), null);
}

But the "curFrame" bitmap image, even though not null, has a width and height of -1 in the debugger. It may be some kind of DRM implementation or something, or just a limitation of the decoder, but I don't think it's possible to get the video pixels. You may have better luck with your camera - have you tried playing around with setWillNotCacheDrawing()? Let me know if it works because that was my fallback plan!

phreakhead
A: 

I got it to work with using the PreviewCallback:

public void onPreviewFrame(byte[] _data, Camera _camera) {

    // data = byte array of the camera preview

}

Sameer Segal
A: 

This may help as well:

The Ketai library offers a nice camera/grabber class that wraps up all the mess for you, although it uses Processing.org for Android. Here's the code if you want to see how they do it:

http://ketai.googlecode.com/svn/trunk

h-t-t-p-:-/-/processingandroid.org/Ketai

phreakhead