繁体   English   中英

Android 上的 OpenGL - 将位图转换为纹理,并保存纹理

[英]OpenGL on Android - Convert a Bitmap to a Texture, and save a Texture

我和我的团队目前正在开发一个 Android 应用程序来进行快速实时和非实时图像处理。

我们面临两个问题:

首先,我们想将 Bitmap 转换为 Texture 以使用 OpenGL 着色器处理图片,然后将其转换回 Bitmap。 我们尝试了一些不成功的实现,例如在 SurfaceTexture 和 Renderer 中使用 GLUtils.texImage2D 函数。

我们的第二个问题是我们目前不知道如何在我们的实时相机活动中保存纹理。 我们使用 OnFrameAvailableListener 来处理图像。 但就目前而言,我们无法保留原始纹理。

我们希望有人可以为我们的问题提供答案。 提前致谢 !

第一

位图到纹理

资源:

http://www.learnopengles.com/android-lesson-four-introducing-basic-texturing/

    // 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();

纹理到位图:

资源:

如何在Android中将opengl纹理转换回位图?

第二

我链接的SO链接中还有一个保存选项。

这是 Mohamed 答案的 Kotlin 版本

/**
 * Load Texture from Bitmap
 **/

fun loadTexture(context: Context, resourceId: Int) : Int {

    val textureHandle : IntArray = IntArray(1)

    GLES20.glGenTextures(1, textureHandle, 0)

    if (textureHandle[0] != 0)
    {
        val options : BitmapFactory.Options = BitmapFactory.Options()
        
        options.inScaled = false   // No pre-scaling

        // Read in the resource
        
        val 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 RuntimeException("Error loading texture.")

    }

    return textureHandle[0]

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM