OpenGL 把纹理保存成图片

int [] oldFboId = new int[1];
                GLES20.glGetIntegerv(GLES20.GL_FRAMEBUFFER_BINDING, IntBuffer.wrap(oldFboId));

                int [] framebuffers = new int[1];
                GLES20.glGenFramebuffers(1, framebuffers, 0);
                int framebufferId = framebuffers[0];
                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, framebufferId);

                int [] renderbuffers = new int[1];
                GLES20.glGenRenderbuffers(1, renderbuffers, 0);
                int renderId = renderbuffers[0];
                GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderId);
                GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, width, height);

                GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texIn, 0);
                GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderId);
                if(GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER) != GLES20.GL_FRAMEBUFFER_COMPLETE) {
                    Log.i(TAG, "Framebuffer error");
                }

                ByteBuffer rgbaBuf = ByteBuffer.allocateDirect(width * height * 4);
                rgbaBuf.position(0);
                GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE,rgbaBuf);

                if(mPixelDataAcquireListenr != null) {
                    mPixelDataAcquireListenr.onDataAcquire(rgbaBuf,width,height);
                }

                GLES20.glDeleteRenderbuffers(1, IntBuffer.wrap(framebuffers));
                GLES20.glDeleteFramebuffers(1, IntBuffer.allocate(framebufferId));

                GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, oldFboId[0]);
    //在这处理得到的RGB数据
    private PixelDataAcquireListenr mPixelDataAcquireListenr = new PixelDataAcquireListenr() {
        @Override
        public void onDataAcquire(ByteBuffer byteBuffer,int width,int height) {
            times ++;
            if(times % 10 == 0) {
                //测试保存为png图片
                saveRgb2Bitmap(byteBuffer, "/mnt/sdcard//"+ System.nanoTime()+".png", width, height);
            }
        }
    };

你可能感兴趣的:(C/C++,OpenGL,音视频)