Android:使用 SurfaceView 内容创建位图

发布于 2024-10-13 16:57:17 字数 241 浏览 13 评论 0原文

我担心这个问题已经有了不幸的答案,但以防万一...我正在使用 SurfaceView 来对位图进行一些图像处理(灯光和颜色修改),并且我需要导入修改后的位图(即SurfaceView 的内容)在一个新的位图中,以便我可以将其保存为图像文件。

我环顾四周,似乎可以从 View.getDrawingCache() 获取位图,但它不适用于 SurfaceView。我得到的只是一个空的位图。

有什么办法解决这个问题吗?

谢谢

I'm afraid to already have the unfortunate answer to this question but just in case... I'm using a SurfaceView to do some image processing with bitmaps (lights and colors modifications) and I would need to import the modified bitmap (i.e. the content of the SurfaceView) in a new bitmap so that I can save it as an image file.

I've been looking around and it seems that it's possible to get a bitmap from View.getDrawingCache() but it doesn't work with SurfaceView. All I get is an empty bitmap.

Is there any solution to this?

Thanks

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

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

发布评论

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

评论(4

戒ㄋ 2024-10-20 16:57:17

对于 API 级别 >=24,请使用 PixelCopy API
这是 ImageView 中的一个示例:

public void DrawBitmap(){
    Bitmap surfaceBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
    PixelCopy.OnPixelCopyFinishedListener listener = copyResult -> {
        // success/failure callback
    };

    PixelCopy.request(YourSurfaceView, surfaceBitmap,listener,getHandler());
    // visualize the retrieved bitmap on your imageview
    setImageBitmap(plotBitmap);
    }
}

For API levels >=24, use the PixelCopy API.
Here's an example inside an ImageView:

public void DrawBitmap(){
    Bitmap surfaceBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
    PixelCopy.OnPixelCopyFinishedListener listener = copyResult -> {
        // success/failure callback
    };

    PixelCopy.request(YourSurfaceView, surfaceBitmap,listener,getHandler());
    // visualize the retrieved bitmap on your imageview
    setImageBitmap(plotBitmap);
    }
}
尸血腥色 2024-10-20 16:57:17

您可以将 SurfaceView 绘制到由位图支持的 Canvas 上吗?

    // be sure to call the createBitmap that returns a mutable Bitmap
    Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b);
    yourSurfaceView.draw(c);

Can you draw your SurfaceView onto a Canvas that's backed by a Bitmap?

    // be sure to call the createBitmap that returns a mutable Bitmap
    Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b);
    yourSurfaceView.draw(c);
愛放△進行李 2024-10-20 16:57:17

没有简单的方法来捕获SurfaceView的帧,因此您可以考虑使用TextureView而不是SurfaceView:可以检索Bitmap使用textureView.getBitmap()方法。

There is no simple way to capture frames of SurfaceView, so you can consider using TextureView instead of SurfaceView: the Bitmap can be retrieved using textureView.getBitmap() method.

深海少女心 2024-10-20 16:57:17

只需获取 SurfaceView 的源代码并修改它即可访问底层位图。

Just grab the source code for SurfaceView and modify it to give you access to the underlying bitmap.

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