【问题标题】:Android OpenGL save black imageAndroid OpenGL 保存黑色图像
【发布时间】:2016-10-08 19:56:49
【问题描述】:

当我写这样的代码时..

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

它保存了唯一的黑色图像..我无法理解像这样使用我失去了什么..谢谢

【问题讨论】:

  • 解决方法:在 onDrawFrame` 中捕获Bitmap,并在单击 Button 时保存。
  • 我已经尝试过了,它正在工作..我无法每次都捕获位图,因为我的程序多次使用 onDrawFrame() 并导致性能下降..(takeScreenshot 嵌套了 for 循环).. 谢谢
  • saveBitmap() 可能会进行 glReadPixels() 调用,这需要当前的 OpenGL 上下文。所以你不能从渲染线程以外的线程调用它。例如,请参阅此处以获取解释:stackoverflow.com/questions/30094705/…。这不是同一个用例,而是同一个基本问题。
  • 非常感谢..我的外语不太好,所以我无法完全理解您在其他讨论中所写的内容,但这给了我灵感

标签: android bitmap opengl-es screenshot


【解决方案1】:
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;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多