我有一个在已添加的 GLSurfaceView 上运行的基本 openGL ES 20 应用程序:
GLSurfaceView view = new GLSurfaceView(this);
view.setRenderer(new OpenGLRenderer());
setContentView(view);
基本上我正在尝试使用以下方法获取屏幕截图:
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;
}
但是位图似乎是透明的。我传入的 View 是:
View content = m_rootActivity.getWindow().getDecorView().getRootView();
任何人都有一个关于如何在 openGL ES 上获取屏幕截图的解决方案,而无需使用我在其他解决方案中看到的 DrawFrame 方法。
也许传入渲染器的引用?任何帮助将不胜感激。
更新: 我正在探索从 onDrawFrame 渲染位图 ( Display black screen while capture screenshot of GLSurfaceView )
但是,我想知道是否有更好的解决方案,因为我无法访问渲染器或表面 View 。我可以传递他们的引用,但想要一个解决方案,我们可以像前面提到的那样捕获整个 View 。
最佳答案
您可以通过以下方式获取屏幕截图:
@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 - Android OpenGL ES 应用程序的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808518/