我有一个在已添加的 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 content = m_rootActivity.getWindow().getDecorView().getRootView();
任何人都有一个关于如何在 openGL ES 上获取屏幕截图的解决方案,而无需使用我在其他解决方案中看到的 DrawFrame 方法。
也许传递渲染器的引用?任何帮助,将不胜感激。
更新:
我正在探索从 onDrawFrame 渲染位图(GLSurfaceView 截图时显示黑屏 https://stackoverflow.com/questions/21753519/display-black-screen-while-capture-screenshot-of-glsurfaceview)
但是,我想知道是否有更好的解决方案,因为我无法访问渲染器或表面视图。我可以传递他们的参考,但想要一个解决方案,我们可以像前面提到的那样捕获整个视图。
看到这个问题 https://stackoverflow.com/questions/22574817/how-to-display-blurred-bitmap-of-screenshot-using-opengl-es-2-0-on-android.
您可以通过以下方式获取屏幕截图:
@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...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)