我正在使用一个依赖于 Google Grafika 仓库的直播流 API。我使用 Grafika EGLSurfaceBase 的 saveFrame 方法,允许用户在流媒体过程中捕捉视频帧。
https://github.com/google/grafika/blob/master/src/com/android/grafika/gles/EglSurfaceBase.java
实际捕获工作正常,但显然在某些相机方向上图像会翻转。 我找到了很多与从OpenGL纹理中获取反转位图相关的问题,但大多数似乎是涉及绘制图像并依赖于以下两种方法之一: a)在OpenG中翻转纹理。但在我的情况下,我正在使用实时流API工作,因此翻转纹理以捕获图像可能实际上也会翻转视频流中的图像捕获。 或者 b)在生成基于资源的位图后翻转位图。在我的情况下,我没有资源,我正在从字节缓冲区创建位图,并且不想复制它来翻转它。 这是API具有的基本EGLSurfaceBase方法-我将把相机方向传递给它,但我的问题是: 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 + "'");
}
我希望找到一种在BMP.createbitmap之前(或同时)翻转图像的方法。例如,我能否使用矩阵通过glReadPixels翻转像素的读取?
另一个注意点/想法:也许在创建后翻转位图的成本很小,因为由于依赖用户交互,它不会经常发生足以引起内存错误?