捕捉GLSurfaceView屏幕为位图 [英] Capture screen of GLSurfaceView to bitmap

查看:1131
本文介绍了捕捉GLSurfaceView屏幕为位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够捕捉 GLSurfaceView 的图像在特定时刻。我有以下的code:

I need to be able to capture an image of a GLSurfaceView at certain moment in time. I have the following code:

relative.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(relative.getDrawingCache());
relative.setDrawingCacheEnabled(false);
Log.v(TAG, "Screenshot height: " + screenshot.getHeight());
image.setImageBitmap(screenshot); 

GLSurfaceView 包含在一个 RelativeLayout的,但我也有尝试它直接使用 GLSurfaceView 来尝试和捕捉图像。有了这个,我觉得屏幕捕捉透明图像,也就是什么也没有。任何帮助将AP preciated。

The GLSurfaceView is contained within a RelativeLayout, but I have also tries it straight using the GLSurfaceView to try and capture the image. With this I think the screen captures a transparent image, i.e. nothing there. Any help will be appreciated.

推荐答案

对不起,我的英文不好。

Sorry for my poor english.

SurfaceView GLSurfaceView 打孔,以允许显示其表面。换句话说,它们具有透明区域。

SurfaceView and GLSurfaceView punch holes in their windows to allow their surfaces to be displayed. In other words, they have transparent areas.

所以你不能调用拍摄图像 GLSurfaceView.getDrawingCache()

So you cannot capture an image by calling GLSurfaceView.getDrawingCache().

如果您想从 GLSurfaceView 的图像,你应该调用 gl.glReadPixels() GLSurfaceView.onDrawFrame()

If you want to get an image from GLSurfaceView, you should invoke gl.glReadPixels() in GLSurfaceView.onDrawFrame().

我修改 createBitmapFromGLSurface 方法,并调用它的 onDrawFrame()

I patched createBitmapFromGLSurface method and call it in onDrawFrame().

(原code可能是从 SKULD 的code)

(The original code might be from skuld's code.)

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
        throws OutOfMemoryError {
    int bitmapBuffer[] = new int[w * h];
    int bitmapSource[] = new int[w * h];
    IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
    intBuffer.position(0);

    try {
        gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer);
        int offset1, offset2;
        for (int i = 0; i < h; i++) {
            offset1 = i * w;
            offset2 = (h - i - 1) * w;
            for (int j = 0; j < w; j++) {
                int texturePixel = bitmapBuffer[offset1 + j];
                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;
                bitmapSource[offset2 + j] = pixel;
            }
        }
    } catch (GLException e) {
        return null;
    }

    return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}

这篇关于捕捉GLSurfaceView屏幕为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆