开发者

Capture camera preview and GLSurfaceView to a file

I'm trying开发者_C百科 to 'take a photo' of both the camera preview, and an overlayed GLSurfaceView. I have the camera preview element working, via camera.takePicture() and PictureCallback(), but now need to either include the GLSurfaceView elements, or capture the current screen seperately and merge the two bitmaps into one file. I have tried to grab an image from the surfaceView using the code below, but this just results in a null bitmap.

public Bitmap grabImage() {

    this.setDrawingCacheEnabled(true);
     Bitmap b = null;
     try {

     b = this.getDrawingCache(true);
     if (b==null) {

         b = this.getDrawingCache(true);
     }
     } catch (Exception e) {
         e.printStackTrace();
     }
     this.setDrawingCacheEnabled(false);
     return b;
 }

I would appreciate any thoughts/ snippets on this. Many thanks in advance.


I've done something similar and it was a bit convoluted but not too terrible.

In my case I'm using camera preview frame which I decode to a bitmap. Then get a canvas from that bitmap pass it to call to draw() on the views (surfaceview or otherwise) that I want drawn over top of the picture.

            Bitmap bm;
            MySurfaceViewImpl sv;
            Canvas c = new Canvas(bm);
            sv.draw(c);

You will need to use your own View implementation to handle the fact that the canvas size is going to change and you'll need to rescale things between the calls to draw() that happen in the normal running of your app and the when you call it manually as the canvas from the picture size is almost certainly going to be different than what's being drawn to the screen.

Also, the primary reason I'm using preview frames rather than captured pictures is due to memory limits. Very few phones support smallish sized pictures but all support reasonable sizes for preview frames. Getting a full size camera picture into a bitmap is probably too much memory. On devices with less than 24MB heap, I'm ok with about a 600 x 480 image and about 4 views that get drawn on top of that but it gets tight.

In your case, you'll probably need to scale the bitmap down to be able to pass a canvas from it to a view.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜