使用 SurfaceTexture 和 OpenGL 修改相机输出

2023-12-01

我试图通过 openGL 过滤器运行来自相机硬件的流,然后将其显示在 GLSurfaceView 中来过滤来自相机硬件的流。当 openGL 去渲染该帧时,LogCat 反复吐出错误:

[unnamed-3314-0] updateTexImage:清除 GL 错误:0x502

0x502 是一个通用的 openGL 错误,并不能真正帮助我找到问题。这是代码如何工作的序列(或者至少应该像我脑海中看到的那样工作),我已将我的代码复制到下面。我希望其他人能够看到我的问题所在。

  1. 创建新的 MyGLSurfaceView。这也会在内部创建新的 MyGL20Renderer 对象。此 MyGLSurfaceView 设置为内容视图。
  2. 一旦 MyGLSurfaceView 完成膨胀/初始化,此完成事件就会触发渲染器创建 DirectVideo 绘制对象,该对象编译/链接定义的着色器并将它们添加到 openGL 程序中。然后它创建一个新的 openGL 纹理对象,然后使用纹理对象 ID 回调到 MainActivity。
  3. 当从渲染器调用 MainActivity 方法时,它会使用传递的 openGL 纹理对象创建一个新的 SurfaceTexture 对象。然后它将自己设置为表面的 onFrameListener。然后,它创建/打开相机对象,将创建的 SurfaceTexture 设置为视频流的目标,并启动相机源。
  4. 当提要中的帧可用时,onFrameAvailable 会向渲染器发送渲染请求。这是在 openGL 线程上获取的,该线程调用 SurfaceTexture 的 updateTexImage(),将帧内存加载到 openGL 纹理中。然后它调用 DirectVideo 的绘制对象,并运行 openGL 程序序列。如果我注释掉这个 .draw() 行,上面提到的错误就会消失,所以问题似乎出在此处的某个地方,但我不排除它是由不正确链接/创建的纹理引起的。

MainActivity.java

public class MainActivity extends Activity implements SurfaceTexture.OnFrameAvailableListener
{
    private Camera mCamera;
    private MyGLSurfaceView glSurfaceView;
    private SurfaceTexture surface;
    MyGL20Renderer renderer;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        glSurfaceView = new MyGLSurfaceView(this);
        renderer = glSurfaceView.getRenderer();
        setContentView(glSurfaceView);
    }

    public void startCamera(int texture)
    {
        surface = new SurfaceTexture(texture);
        surface.setOnFrameAvailableListener(this);
        renderer.setSurface(surface);

        mCamera = Camera.open();

        try
        {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        }
        catch (IOException ioe)
        {
            Log.w("MainActivity","CAM LAUNCH FAILED");
        }
    }

    public void onFrameAvailable(SurfaceTexture surfaceTexture)
    {
        glSurfaceView.requestRender();
    }

    @Override
    public void onPause()
    {
        mCamera.stopPreview();
        mCamera.release();
        System.exit(0);
    }

MyGLSurfaceView.java

class MyGLSurfaceView extends GLSurfaceView
{
    MyGL20Renderer renderer;
    public MyGLSurfaceView(Context context)
    {
        super(context);

        setEGLContextClientVersion(2);

        renderer = new MyGL20Renderer((MainActivity)context);
        setRenderer(renderer);
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }
    public MyGL20Renderer getRenderer()
    {
        return renderer;
    }
}

MyGL20Renderer.java

public class MyGL20Renderer implements GLSurfaceView.Renderer
{

    DirectVideo mDirectVideo;
    int texture;
    private SurfaceTexture surface;
    MainActivity delegate;

    public MyGL20Renderer(MainActivity _delegate)
    {
        delegate = _delegate;
    }

    public void onSurfaceCreated(GL10 unused, EGLConfig config)
    {
        mDirectVideo = new DirectVideo(texture);
        texture = createTexture();
        GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        delegate.startCamera(texture);
    }

    public void onDrawFrame(GL10 unused)
    {
            float[] mtx = new float[16];
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
            surface.updateTexImage();
            surface.getTransformMatrix(mtx); 

            mDirectVideo.draw();
    }

    public void onSurfaceChanged(GL10 unused, int width, int height)
    {
        GLES20.glViewport(0, 0, width, height);
    }

    static public int loadShader(int type, String shaderCode)
    {
        int shader = GLES20.glCreateShader(type);

        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }

    static private int createTexture()
    {
        int[] texture = new int[1];

        GLES20.glGenTextures(1,texture, 0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture[0]);
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
             GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);        
        GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
             GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
     GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
             GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
     GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES,
             GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

        return texture[0];
    }

    public void setSurface(SurfaceTexture _surface)
    {
        surface = _surface;
    }
}

DirectVideo.java

public class DirectVideo {


    private final String vertexShaderCode =
            "#extension GL_OES_EGL_image_external : require\n"+
            "attribute vec4 position;" +
            "attribute vec4 inputTextureCoordinate;" +
            "varying vec2 textureCoordinate;" +
            "void main()" +
            "{"+
                "gl_Position = position;"+
                "textureCoordinate = inputTextureCoordinate.xy;" +
            "}";

        private final String fragmentShaderCode =
            "#extension GL_OES_EGL_image_external : require\n"+
            "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vColor;" +
            "}";

        private FloatBuffer vertexBuffer, textureVerticesBuffer;
        private ShortBuffer drawListBuffer;
         private final int mProgram;
            private int mPositionHandle;
            private int mColorHandle;
            private int mTextureCoordHandle;


    // number of coordinates per vertex in this array
    static final int COORDS_PER_VERTEX = 2;
    static float squareVertices[] = { // in counterclockwise order:
         -1.0f,  1.0f,
         -1.0f,  -1.0f,
         1.0f,  -1.0f,
         1.0f,  1.0f
    };

    private short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices

    static float textureVertices[] = { // in counterclockwise order:
        1.0f,  1.0f,
        1.0f,  0.0f,
        0.0f,  1.0f,
        0.0f,  0.0f
   };

    private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

    private int texture;

    public DirectVideo(int _texture)
    {
        texture = _texture;

        ByteBuffer bb = ByteBuffer.allocateDirect(squareVertices.length * 4);
        bb.order(ByteOrder.nativeOrder());
        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(squareVertices);
        vertexBuffer.position(0);

        ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
        dlb.order(ByteOrder.nativeOrder());
        drawListBuffer = dlb.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);

        ByteBuffer bb2 = ByteBuffer.allocateDirect(textureVertices.length * 4);
        bb2.order(ByteOrder.nativeOrder());
        textureVerticesBuffer = bb2.asFloatBuffer();
        textureVerticesBuffer.put(textureVertices);
        textureVerticesBuffer.position(0);

        int vertexShader = MyGL20Renderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = MyGL20Renderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

        mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
        GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
        GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
        GLES20.glLinkProgram(mProgram);          
    }

    public void draw()
    {
        GLES20.glUseProgram(mProgram);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
        GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture);

        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position");
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,vertexStride, vertexBuffer);

        mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate");
        GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
        GLES20.glVertexAttribPointer(mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,vertexStride, textureVerticesBuffer);

        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

        GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
        GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

        // Disable vertex array
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mTextureCoordHandle);
    }
}

mDirectVideo = new DirectVideo(texture);
texture = createTexture();

应该

texture = createTexture();
mDirectVideo = new DirectVideo(texture);

Shader

private final String vertexShaderCode =
        "attribute vec4 position;" +
        "attribute vec2 inputTextureCoordinate;" +
        "varying vec2 textureCoordinate;" +
        "void main()" +
        "{"+
            "gl_Position = position;"+
            "textureCoordinate = inputTextureCoordinate;" +
        "}";

    private final String fragmentShaderCode =
        "#extension GL_OES_EGL_image_external : require\n"+
        "precision mediump float;" +
        "varying vec2 textureCoordinate;                            \n" +
        "uniform samplerExternalOES s_texture;               \n" +
        "void main() {" +
        "  gl_FragColor = texture2D( s_texture, textureCoordinate );\n" +
        "}";

mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

应该

mColorHandle = GLES20.glGetAttribLocation(mProgram, "s_texture");

从 DirectVideo draw.glVertexAttribPointer 等中删除初始化内容。将其放入某个 init 函数中。

public void draw()
{
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, texture);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SurfaceTexture 和 OpenGL 修改相机输出 的相关文章

  • 需要对某些片段禁用 CollapsingToolbarLayout 的展开

    我有一个AppCompatActivity控制替换许多片段 这是我的布局 活动 main xml
  • 清晰的图标 清晰的 Android 应用程序

    你好 下午好 关于如何提高图标的 png 质量 有什么想法吗 我使用了 Android 开发者页面上的套件 但我无法获得清晰的图像 我的意思是非常清晰 我是否需要以不同的方式加载此图标 而不仅仅是拖入我的布局 谢谢 我使用这个布局
  • 如何使用retrofit2动态设置超时?

    public class Router private static Retrofit retrofit null public Retrofit getRetrofit if retrofit null OkHttpClient clie
  • 在 Android 市场中以编程方式检查我的应用程序版本

    目前 我正在启动时检查应用程序版本代码 并将其与我的服务器上的最新版本代码进行匹配 并根据此匹配 我发送用户从 Android 市场获取最新更新 它运行良好 但我的问题是我必须手动更改服务器上的最新版本代码 并且我不知道新版本何时发布APK
  • 如何从Slog中查看日志

    如何查看 Slog API 生成的日志 是否有任何选项可以查看系统缓冲区中的日志 我的意思是查看我们使用的无线电缓冲区的日志 adb logcat b 无线电 而这个日志是由Android的Log类生成的 Slog API 的输出在哪里 怎
  • Android,如何从 XML 布局添加 Google 地图选项?

    我有一个包含 MapView 的片段 我已将此视图添加到 XML 文件中 如下所示
  • 如何连接到Google Play服务并加载排行榜

    我想将我的游戏与 Google Play 服务连接 我已阅读有关 Android 开发人员的文档 并尝试遵循输入数字示例 但仍然无法加载排行榜 我有导入baseGameUtils 但我使用andengine 所以我没有使用来自谷歌的exte
  • 新安装的Eclipse和Android SDK。无法让模拟器工作。挂在时钟屏幕上

    我对开发是全新的 我已经安装了 Eclipse 和 Andoid SDK 但是 我无法让模拟器工作 我已经尝试过示例记事本代码和 Hello Android 教程代码 每次我尝试运行任一应用程序时 它都会挂在时钟屏幕上 屏幕上还显示正在充电
  • MediaPlayer.create() 始终返回 null

    我以前用过媒体播放器 从来没有遇到过这个问题 每当我尝试使用 MediaPlayer create 时 该方法都会给我 null 并且我无法播放声音 我有什么遗漏的吗 public class Game extends Activity p
  • 在 Android 中使用 AES 加密的最佳实践是什么?

    我为什么问这个问题 我知道人们对 AES 加密存在很多疑问 即使对于 Android 也是如此 如果您在网络上搜索 会发现很多代码片段 但在每个页面上 在每个 Stack Overflow 问题中 我都发现了另一个具有重大差异的实现 所以我
  • Android - 使用 SAX 解析器解析大文件

    我正在尝试使用 SAX 解析器解析来自 webservice 的 xml 数据 当我尝试使用 URL 解析数据 大小 7 4MB 时 它工作正常 但是当我从 URL 复制 xml 数据并放置 xml 文件时 size 7 4MB 在raw文
  • 何时调用 glMatrixMode()

    我所关注的大多数 Android OpenGL ES 教程都有其 onSurfaceChanged 函数 如下所示 public void onSurfaceChanged GL10 gl int width int height gl g
  • 片段活动中的 commitAllowingStateLoss()

    我的应用程序使用片段活动 它仅处于纵向模式 无法旋转屏幕 最初我使用的是commit 方法 但现在我计划不加区别地将这些更改为commitAllowingStateLoss 对于碎片活动 是否有任何理由不不加区别地执行此操作而不重新评估我使
  • foo.setVisibility(View.GONE) 和parent.removeView(foo) 之间的区别

    如果 foo 是一个视图 那么有什么区别foo setVisibility View GONE and fooParent removeView foo 我对两个语句之前和之后视图的内存消耗特别感兴趣 可见性设置为 GONE 的视图是否会消
  • Android:从 PhoneGap 应用打开 Play 商店链接

    我想从我的phonegap 3 4 应用程序打开一个指向Google Play 商店的链接 呼唤market details id com google android apps maps导致 ActivityNotFoundExcepti
  • onStart() 到底做了什么? - 安卓[重复]

    这个问题在这里已经有答案了 一段时间以来 我一直想知道 onStart 函数在 android 生命周期中的确切作用 网上的大多数资源只是说 它在您的活动在屏幕上可见之前被调用 但到目前为止我所做的应用程序我从未使用过onStart 我在
  • 带有工具提示的搜索栏 android

    Hi All 我正在尝试使用工具提示自定义 android 搜索栏 如给定的图像 有没有办法在搜索栏中添加带有拇指的文本视图 或任何其他想法 Thanks 我们可以通过拇指的界限来做到这一点 并在seekbar的progressChange
  • 片段中的 SavedInstanceState 始终为 null

    我使用 XML 以及活动中的 setContentView 将片段附加到活动 A 有一个问题 因为我的片段中有非常动态的视图 所以当方向改变时 我必须恢复所有的观点状态 我有问题 因为我正在使用类似的东西 public void onSav
  • 如何以编程方式检测android中可用的底部软导航栏?

    我试图通过 android 程序确定软导航栏 我没有找到直接的方法来确定 有没有办法找到导航栏的可用性 软导航栏图像在这里 以下方法对我有用并在许多设备上进行了测试 public boolean hasNavBar Resources re
  • AndroidAnnotations 和 Dagger

    我正在尝试使用 Dagger 注入 Android 带注释的 Activity java lang IllegalArgumentException No inject registered for members com app serv

随机推荐