Android OpenGL保存黑色图像

时间:2016-10-08 19:56:49

标签: android bitmap opengl-es screenshot

当我写这样的代码时..

@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;
        }

它保存了唯一的黑色图像..我无法理解我丢失的东西就像这样使用..谢谢

1 个答案:

答案 0 :(得分:0)

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;
    }