您的位置:首页 > 移动开发 > Android开发

android opengl 获取bitmap 数据

2014-03-03 10:02 513 查看
视频截图或游戏界面截图 要求获取位图数据 ,然后本地保存图片。

方式1:测试通过

int screenshotSize = this.getWidth() * this.getHeight();
                ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
                bb.order(ByteOrder.nativeOrder());
                gl.glReadPixels(0, 0, this.getWidth(), this.getHeight(), GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
                int pixelsBuffer[] = new int[screenshotSize];
                bb.asIntBuffer().get(pixelsBuffer);
                bb = null;
                Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.RGB_565);
                bitmap.setPixels(pixelsBuffer, screenshotSize-this.getWidth(), -this.getWidth(), 0, 0, this.getWidth(), this.getHeight());
                pixelsBuffer = null;

                short sBuffer[] = new short[screenshotSize];
                ShortBuffer sb = ShortBuffer.wrap(sBuffer);
                bitmap.copyPixelsToBuffer(sb);

                //Making created bitmap (from OpenGL points) compatible with Android bitmap
                for (int i = 0; i < screenshotSize; ++i) {                  
                    short v = sBuffer[i];
                    sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
                }
                sb.rewind();
                bitmap.copyPixelsFromBuffer(sb);
                screenshotCallback.onScreenshot(bitmap);

public abstract class AsynScreenshotCallback {
    /**
     * 
     * 异步截图回调
     * @param bitmap 截图的位图对象
     * @throws
     */
    public abstract void onScreenshot(Bitmap bitmap);
}


方法2 :http://www.eoeandroid.com/thread-56476-1-1.html

方法3:http://www.oschina.net/code/snippet_729469_19233

后面两种没有测试,先收藏起来。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: