I'm using a live streaming API that relies on Google's Grafika repo. I'm using Grafika EGLSurfaceBase's saveFrame method to allow the user to capture stills of his video while he streams.
https://github.com/google/grafika/blob/master/src/com/android/grafika/gles/EglSurfaceBase.java
The actual capture works BUT obviously on some camera orientations the image is flipped.
I've found a lot of questions related to inverted bitmaps taken from OpenGL texture - but most seem to refer to drawn images and rely on either:
a)flipping the texture in OpenG. But in my case, I'm working off of a live streaming API so flipping the texture to capture the image may actually flip the image capture as well on the video stream.
OR
b) flipping the bitmap after it has been generated based on a resource. In my case I don't have a resource,I'm creating the bitmap from the bytebuffer and would rather not duplicate it to flip it.
Here is the basic EGLSurfaceBase Method the API has - I will be passing the camera orientation to it but my question is:
String filename = file.toString();
int width = getWidth();
int height = getHeight();
ByteBuffer buf = ByteBuffer.allocateDirect(width * height * 4);
buf.order(ByteOrder.LITTLE_ENDIAN);
GLES20.glReadPixels(0, 0, width, height,
GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, buf);
GlUtil.checkGlError("glReadPixels");
buf.rewind();
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(filename));
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buf);
bmp.compress(Bitmap.CompressFormat.PNG, 90, bos);
bmp.recycle();
} finally {
if (bos != null) bos.close();
}
Log.d(TAG, "Saved " + width + "x" + height + " frame as '" + filename + "'");
}
My preferred solution would be to find a way to flip the image prior to BMP.createbitmap (or at the same time). For example, can I use a matrix to flip the reading of the pixels by glReadPixels?
Another note/thought: maybe the cost of flipping the bitmap after creation is trivial because, since this relies on user interaction, it won't happen often enough to cause a memory error?