android opengl 獲取bitmap 數據

視頻截圖或遊戲界面截圖 要求獲取位圖數據 ,然後本地保存圖片。

方式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 

後面兩種沒有測試,先收藏起來。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章