public Bitmap SavePixels(GL10 gl){
int b[] = new int[Width * Height];
IntBuffer ib = IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(0, 0, Width, Height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
// The bytes within the ints are in the wrong order for android, but convert into a
// bitmap anyway. They're also bottom-to-top rather than top-to-bottom. We'll fix
// this up soon using some fast API calls.
Bitmap glbitmap = Bitmap.createBitmap(b, Width, Height, Bitmap.Config.ARGB_4444);
ib = null; // we're done with ib
b = null; // we're done with b, so allow the memory to be freed
// To swap the color channels, we'll use a ColorMatrix/ColorMatrixFilter. From the Android docs:
//
// This is a 5x4 matrix: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
// When applied to a color [r, g, b, a] the resulting color is computed as (after clamping):
//
// R' = a*R + b*G + c*B + d*A + e;
// G' = f*R + g*G + h*B + i*A + j;
// B' = k*R + l*G + m*B + n*A + o;
// A' = p*R + q*G + r*B + s*A + t;
//
// We want to swap R and B, so the coefficients will be:
// R' = B => 0,0,1,0,0
// G' = G => 0,1,0,0,0
// B' = R => 1,0,0,0,0
// A' = A => 0,0,0,1,0
final float[] cmVals = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cmVals))); // our R<->B swapping paint->
Bitmap bitmap = Bitmap.createBitmap(Width, Height, Config.ARGB_4444); // the bitmap we're going to draw onto
Canvas canvas = new Canvas(bitmap); // we draw to the bitmap through a canvas
canvas.drawBitmap(glbitmap, 0, 0, paint); // draw the opengl bitmap onto the canvas, using the color swapping paint
glbitmap = null; // we're done with glbitmap, let go of its memory
// the image is still upside-down, so vertically flip it
Matrix matrix = new Matrix();
matrix.preScale(1.0f, -1.0f); // scaling: x = x, y = -y, i.e. vertically flip
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // new bitmap, using the flipping matrix
}
沒有留言:
張貼留言