如何从GLSurfaceView获取位图图像

时间:2015-12-31 12:54:27

标签: android bitmap 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());,因此它在{{1}}行中崩溃 为什么我从那里得到空,我该如何解决这个问题?另外,有没有不同/更好的方法来实现我的目标?任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分: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