Android的OpenGL的拍摄截图截图、Android、OpenGL

2023-09-12 08:47:44 作者:誰也不是誰的誰

我想利用Android的OpenGL的截图。

I'm trying to take a screenshot of Android OpenGL.

在code,我发现如下:

The code I found is as follows:

nt size = width * height;
    ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
    buf.order(ByteOrder.nativeOrder());
    glContext.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
    int data[] = new int[size];
    buf.asIntBuffer().get(data);
    buf = null;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bitmap.setPixels(data, size-width, -width, 0, 0, width, height);
    data = null;

    short sdata[] = new short[size];
    ShortBuffer sbuf = ShortBuffer.wrap(sdata);
    bitmap.copyPixelsToBuffer(sbuf);
    for (int i = 0; i < size; ++i) {
        //BGR-565 to RGB-565
        short v = sdata[i];
        sdata[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
    }
    sbuf.rewind();
    bitmap.copyPixelsFromBuffer(sbuf);

    try {
        FileOutputStream fos = new FileOutputStream("/sdcard/screeshot.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        // handle
    }

我想也是从该网站一个code 链接文本

I tried also a code from that site link text

在每一种情况下,结果是一个PNG文件这是完全的黑色。 我发现有一些问题, glReadPixels 方法,但我不知道如何绕过它。

In each case the result is a png file which is completely black. I found there is some problem with glReadPixels method but I don't know how to bypass it.

推荐答案

对不起已故的答复...

Sorry for the late response...

为了执行正确的截屏你必须把你的onDrawFrame(GL10 GL)处理以下code:

In order to perform a correct screenshot You have to put into Your onDrawFrame(GL10 gl) handler the following code:

if(screenshot){                     
                int screenshotSize = width * height;
                ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
                bb.order(ByteOrder.nativeOrder());
                gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
                int pixelsBuffer[] = new int[screenshotSize];
                bb.asIntBuffer().get(pixelsBuffer);
                bb = null;
                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height);
                pixelsBuffer = null;

                short sBuffer[] = new short[screenshotSize];
                ShortBuffer sb = ShortBuffer.wrap(sBuffer);
                bitmap.copyPixelsToBuffer(sb);

                //Making created bitmap (from OpenGL points) compatible with Android bitmap
                for (int i = 0; i < screenshotSize; ++i) {                  
                    short v = sBuffer[i];
                    sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
                }
                sb.rewind();
                bitmap.copyPixelsFromBuffer(sb);
                lastScreenshot = bitmap;

                screenshot = false;
            }

在屏幕截图级字段设置为true,当用户presses按钮来创建一个屏幕截图 或在任何其他情况下你想要的。里面的如果身体,你可以将任何截图创造code样品你找到个互联网 - 最重要的是有GL10的当前实例。例如,当你的GL10实例只保存到类变量,然后使用它的事件之外创建的截图你会结束与完全空白的图像。这就是为什么你必须采取OnDrawFrame事件处理程序,其中GL10实例是当前的内部截图。 希望它能帮助。

The "screenshot" class field is set to true whenever the user presses the button to create a screenshot or at any other circumstances You want. Inside the "if" body You may place any screenshot creating code sample You find in th internet - the most important thing is having the current instance of GL10. For example when You just save the GL10 instance to the class variable and then use it outside the event to create the screenshot You'll end up with the completely blank image. That's why You have to take a screenshot inside the OnDrawFrame event handler where the GL10 instance is the current one. Hope that it helps.

最好的问候,戈登。

 
精彩推荐
图片推荐