将相机预览和 GLSurfaceView 捕获到文件中

发布于 2024-10-30 23:34:03 字数 645 浏览 5 评论 0原文

我正在尝试对相机预览和叠加的 GLSurfaceView 进行“拍照”。 我通过camera.takePicture() 和PictureCallback() 使相机预览元素正常工作,但现在需要包含GLSurfaceView 元素,或者单独捕获当前屏幕并将两个位图合并到一个文件中。 我尝试使用下面的代码从 SurfaceView 中获取图像,但这只会导致空位图。

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'm trying 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮凉 2024-11-06 23:34:03

我也做过类似的事情,虽然有点复杂,但并不算太糟糕。

就我而言,我使用相机预览帧,将其解码为位图。然后从该位图中获取画布,将其传递给我想要在图片顶部绘制的视图(表面视图或其他视图)上调用draw()。

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

您将需要使用自己的 View 实现来处理画布大小将发生变化的事实,并且您需要在应用程序正常运行时发生的对 draw() 的调用与调用时发生的调用之间重新调整大小手动进行,因为图片大小的画布几乎肯定会与绘制到屏幕上的内容不同。

此外,我使用预览帧而不是捕获的图片的主要原因是内存限制。很少有手机支持小尺寸的图片,但所有手机都支持合理尺寸的预览帧。将全尺寸相机图片转换为位图可能会占用太多内存。在堆小于 24MB 的设备上,我可以接受大约 600 x 480 的图像和在其上绘制的大约 4 个视图,但它会变得紧张。

在您的情况下,您可能需要缩小位图才能将画布从它传递到视图。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文