我和我的团队目前正在开发一款 Android 应用程序,以进行快速实时和非实时图像处理。
我们面临两个问题:
首先,我们希望将位图转换为纹理,以便使用 OpenGL 着色器处理图片,然后将其转换回位图。我们尝试了一些不成功的实现,例如在 SurfaceTexture 和渲染器中使用 GLUtils.texImage2D 函数。
我们的第二个问题是,我们目前不知道如何在实时相机 Activity 中保存纹理。我们使用 OnFrameAvailableListener 来处理图像。但就目前而言,我们无法保留原始纹理。
我们希望有人能为我们的问题提供答案。提前致谢 !
最佳答案
第一
位图到纹理
来源:
http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/
public static int loadTexture(final Context context, final int resourceId){
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
if (textureHandle[0] != 0)
{
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scaling
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
if (textureHandle[0] == 0)
{
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}
纹理到位图:
来源:
How do you convert opengl texture back to bitmap in android?
第二
我链接的 SO 链接中还有一个保存选项。
关于java - Android 上的 OpenGL - 将位图转换为纹理,并保存纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41982577/