Skip to content

Instantly share code, notes, and snippets.

@teppeihomma
Created August 30, 2012 03:42
Show Gist options
  • Save teppeihomma/3522079 to your computer and use it in GitHub Desktop.
Save teppeihomma/3522079 to your computer and use it in GitHub Desktop.
glReadPixelsからBitmapへの画像取得
// ByteBufferに読み込み
ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4);
gl10.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buffer);
// 上下逆なので、上下反転
byte[] tmp1 = new byte[width * 4];
byte[] tmp2 = new byte[width * 4];
int h = (int) height / 2;
for (int y = 0; y < h; y++) {
buffer.position(y * width * 4);
buffer.get(tmp1, 0, tmp1.length);
buffer.position((height - 1 - y) * width * 4);
buffer.get(tmp2, 0, tmp2.length);
buffer.position((height - 1 - y) * width * 4);
buffer.put(tmp1);
buffer.position(y * width * 4);
buffer.put(tmp2);
}
buffer.position(0);
tmp1 = tmp2 = null;
// Bitmapの作成
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
// 後片付け
buffer = null;
System.gc();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment