Android OpenGL保存黑色图像



当我写这样的代码…

@Override
public void onDrawFrame(GL10 gl) {
    /* codes */
    saveBitmap(takeScreenshot(gl));
}

它工作完美(截图并保存位图到sd卡)

当我想使用Button作为触发器时

    Button btn;
        @Override
        public void onCreate{
           btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    saveBitmap(takeScreenshot(currGL10));
                }
            });
        }
        @Override
        public void onDrawFrame(GL10 gl) {
            /* codes */
            currGL10 = gl;
        }

它保存唯一的黑色图像..我不明白我这样使用就失去了什么。谢谢你

use this code its work fine .
    @Override
    public void onDrawFrame(GL10 arg0) {
        imageBitmap = takeScreenshot(arg0);
    }
**take a screenshot of open GlSurfaceView**
public Bitmap takeScreenshot(GL10 mGL) {
        final int mWidth = b_width;
        final int mHeight = b_height;
        IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
        IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
        mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
        // Convert upside down mirror-reversed image to right-side up normal
        // image.
        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));
            }
        }
        Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        mBitmap.eraseColor(Color.argb(0, 255, 255, 255));
        mBitmap.copyPixelsFromBuffer(ibt);
        return mBitmap;
    }

最新更新