有偿回答
andriod 创建一个自定义egl环境,并生成一个公共的SurfaceTexture,用这个SurfaceTexture绑定相机,在需要预览的时候动态添加多个Surface 并把SurfaceTexture上渲染的内容复制到添加的 Surface中(有可能有多个),实现在不关闭相机的情况下动态预览,或者录像,或者推流
知道的联系我有偿回答
具体联系 1286630069@qq.com
有偿回答
andriod 创建一个自定义egl环境,并生成一个公共的SurfaceTexture,用这个SurfaceTexture绑定相机,在需要预览的时候动态添加多个Surface 并把SurfaceTexture上渲染的内容复制到添加的 Surface中(有可能有多个),实现在不关闭相机的情况下动态预览,或者录像,或者推流
知道的联系我有偿回答
具体联系 1286630069@qq.com
阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程
你想创建一个自定义的 EGL 环境,并生成一个公共的 SurfaceTexture,用于绑定相机,然后在需要预览时动态添加多个 Surface。
下面是一个基本的示例代码,使用 Kotlin 语言实现:
import android.content.Context
import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.opengl.EGL14
import android.opengl.EGLConfig
import android.opengl.EGLContext
import android.opengl.EGLDisplay
import android.opengl.EGLSurface
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.opengl.GLUtils
import android.util.Log
import java.nio.IntBuffer
class CustomEGLCamera(private val context: Context) {
private val eglDisplay: EGLDisplay
private val eglContext: EGLContext
private val eglSurface: EGLSurface
private val surfaceTexture: SurfaceTexture
private val camera: Camera
init {
eglDisplay = EGL14.eglGetDisplay(null)
val config = EGLConfig()
EGL14.eglChooseConfig(eglDisplay, intArrayOf(EGL14.EGL_RED_SIZE, EGL14.EGL_GREEN_SIZE, EGL14.EGL_BLUE_SIZE, EGL14.EGL_ALPHA_SIZE, EGL14.EGL_DEPTH_SIZE, EGL14.EGL_STENCIL_SIZE, 0), intArrayOf(), 1, config)
eglContext = EGL14.eglCreateContext(eglDisplay, config, EGL14.EGL_NO_CONTEXT, intArrayOf(EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, 0))
eglSurface = EGL14.eglCreateWindowSurface(eglDisplay, config, context.window, null)
surfaceTexture = SurfaceTexture(eglSurface)
camera = Camera.open()
}
fun startPreview() {
camera.setPreviewTexture(surfaceTexture)
camera.startPreview()
}
fun addSurface(surface: EGLSurface) {
EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT or GLES20.GL_DEPTH_BUFFER_BIT)
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, surfaceTexture.id)
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, surface.width, surface.height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null)
EGL14.eglSwapBuffers(eglDisplay, eglSurface)
}
fun stopPreview() {
camera.stopPreview()
camera.release()
}
}
在上面的代码中,我们首先创建了一个 EGL 显示、上下文和表面,然后使用 SurfaceTexture 绑定相机。然后,在需要预览时,我们使用 addSurface 方法将多个 Surface 添加到 EGL 环境中,并使用 GLES20 库将图像渲染到这些表面上。
请注意,这只是一个基本示例代码,你可能需要根据你的具体需求进行修改和优化。