Taking screenshot of Android OpenGL

前端 未结 3 1883
既然无缘
既然无缘 2020-12-13 04:55

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

The code I found is as follows:

nt size = width * height;
    ByteBuffer buf = ByteBuffer.allocat         


        
3条回答
  •  臣服心动
    2020-12-13 05:36

    Sorry for the late response...

    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;
                }
    

    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.

    Best regards, Gordon.

提交回复
热议问题