android OpenGL ES应用程序上的屏幕截图 [英] Screenshot on android OpenGL ES application

查看:581
本文介绍了android OpenGL ES应用程序上的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的openGL ES 20应用程序,该应用程序在已添加的GLSurfaceView上运行:

I have a basic openGL ES 20 application running with on a GLSurfaceView that has been added:

    GLSurfaceView view = new GLSurfaceView(this);
    view.setRenderer(new OpenGLRenderer());
    setContentView(view);

基本上,我正在尝试使用以下方法获取屏幕截图:

Basically I am trying get a screenshot with the following method:

private static Bitmap getScreenshot(View v)
    {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }

但似乎位图是透明的.我传递的视图是:

But it seems the bitmap is transparent. The view I am passing in is:

View content = m_rootActivity.getWindow().getDecorView().getRootView();

任何人都有一个解决方案,该方法可以在openGL ES上获取屏幕截图,而无需求助于我在其他解决方案中看到的DrawFrame方法.

Anyone has a solution on how to get screenshot on openGL ES without resorting into going into the DrawFrame method which I have seen in other solutions.

也许传递渲染器的引用?任何帮助将不胜感激.

Maybe pass in the reference of the renderer? Any help would be appreciated.

更新: 我正在探索从onDrawFrame渲染位图(在捕获时显示黑屏GLSurfaceView的屏幕截图)

Update: I was exploring in rendering the bitmap from the onDrawFrame (Display black screen while capture screenshot of GLSurfaceView)

但是,我想知道是否有更好的解决方案,因为我将无法访问渲染器或Surfaceview.我可以传递他们的参考,但是想要一个解决方案,我们可以像前面提到的那样捕获整个视图.

However, I was wondering if there is a better solution since I won't have access to the renderer nor the surfaceview. I can pass in their reference but would like a solution where we can just capture the entire view like what was mentioned earlier.

推荐答案

您可以通过以下方式获取屏幕截图:

You can get a screenshot with:

@Override
public void onDrawFrame(GL10 gl) {

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    // draw ...

    if (takeScreenshot) {

        int screenshotSize = width * height;
        ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
        bb.order(ByteOrder.nativeOrder());
        GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, bb);
        int pixelsBuffer[] = new int[screenshotSize];
        bb.asIntBuffer().get(pixelsBuffer);
        bb = null;

        for (int i = 0; i < screenshotSize; ++i) {
            // The alpha and green channels' positions are preserved while the      red and blue are swapped
            pixelsBuffer[i] = ((pixelsBuffer[i] & 0xff00ff00)) |    ((pixelsBuffer[i] & 0x000000ff) << 16) | ((pixelsBuffer[i] & 0x00ff0000) >> 16);
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height);
        // save bitmap...
    }
}

这篇关于android OpenGL ES应用程序上的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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