2015-12-31 21 views
0

我做下面让已被设置为GLSurfaceView对象的位图图像:如何从GLSurfaceView得到位图图像

glView.setDrawingCacheEnabled(true); 
glView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
     View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
glView.layout(0, 0, glView.getMeasuredWidth(), glView.getMeasuredHeight()); 

glView.buildDrawingCache(true); 
Bitmap tmpbm = Bitmap.createBitmap(glView.getDrawingCache()); 
glView.setDrawingCacheEnabled(false); 

glView.getDrawingCache()正在恢复我null在上述情况下,和因此它在行中崩溃Bitmap tmpbm = Bitmap.createBitmap(glView.getDrawingCache()); 为什么我从那里变为空,以及如何解决此问题?另外,是否有不同的/更好的方式来实现我的目标?任何帮助将不胜感激。

+0

见http://stackoverflow.com/questions/27817577/android-take-screenshot-of-surface-view-shows-black-screen 。只要你从'onDrawFrame()'调用它,@Helmi答案中的代码应该工作。 – fadden

回答

0

试试这个方法:

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl) 
     throws OutOfMemoryError { 
    int bitmapBuffer[] = new int[w * h]; 
    int bitmapSource[] = new int[w * h]; 
    IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer); 
    intBuffer.position(0); 

    try { 
     gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, intBuffer); 
     int offset1, offset2; 
     for (int i = 0; i < h; i++) { 
      offset1 = i * w; 
      offset2 = (h - i - 1) * w; 
      for (int j = 0; j < w; j++) { 
       int texturePixel = bitmapBuffer[offset1 + j]; 
       int blue = (texturePixel >> 16) & 0xff; 
       int red = (texturePixel << 16) & 0x00ff0000; 
       int pixel = (texturePixel & 0xff00ff00) | red | blue; 
       bitmapSource[offset2 + j] = pixel; 
      } 
     } 
    } catch (GLException e) { 
     return null; 
    } 

    return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888); 
} 

here

+0

你可以告诉我,我应该传递给函数'private Bitmap createBitmapFromGLSurface(int x,int y,int w,int h,GL10 gl)'作为参数吗? 'gl'我从'onDrawFrame'函数本身获得。同样,我也有'w'和'h'的值(图像的宽度和高度)。但是'x'和'y'的值应该是什么? –

+0

x&y代表读取像素时的开始点。如果你想获得所有表面,你可以使用0。否则使用你需要的部分。 – Helmi