【问题标题】:Problem in the combination of textureView and OpenGLtextureView和OpenGL结合的问题
【发布时间】:2019-03-03 21:01:56
【问题描述】:

目前我正在尝试同时使用 textureView 和 OpenGL,我的目标是从 SurfaceTexture 读取缓冲区。经过大量搜索,我找到了grafika,但没有找到使用textureview的合适示例。(并且由于某种原因我必须使用textureView)

我尝试的是创建纹理并尝试设置纹理视图以使用它:

    @Override
protected void onCreate(final Bundle savedInstanceState) {

    super.onCreate(null);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_textureview);

    displayTextureView=(TextureView) findViewById(R.id.camera_textureview);
    mEglCore = new EglCore(null, EglCore.FLAG_RECORDABLE);
    mOffscreenSurface=new OffscreenSurface(mEglCore,VIDEO_WIDTH,VIDEO_HEIGHT);
    mOffscreenSurface.makeCurrent();
    mFullFrameBlit = new FullFrameRect(
            new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT));
    mTextureId = mFullFrameBlit.createTextureObject();
    mCameraTexture = new SurfaceTexture(false);
    mCameraTexture.attachToGLContext(mTextureId);
    displayTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
    displayTextureView.setSurfaceTexture(mCameraTexture);
    mHandler = new MainHandler(this);
    Initialized=true;
}

但是它给了我错误:

GLConsumer 已附加到上下文

我也找到了this,但是在我尝试了这里描述的方法后,我从 glReadPixels 得到的结果是全黑的,所以我猜 SurfaceTexture 必须附加到 GLcontext 才能读取像素。

谁能帮帮我?

【问题讨论】:

    标签: android opengl-es textureview


    【解决方案1】:

    你应该参考这个 https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/ContinuousCaptureActivity.java

    本例使用surfaceView,只需替换为textureView即可。

    第 386 行:"mDisplaySurface = new WindowSurface(mEglCore, holder.getSurface(), false);" 只需将 holder.getSurface() 替换为 textureView 中的 surfacetexture。

    还有一种读取缓冲区的方法 https://github.com/google/grafika/blob/master/app/src/main/java/com/android/grafika/gles/EglSurfaceBase.java

    看看 saveFrame 方法。

    【讨论】:

    • 感谢您的回答!通过surfacetexture,我需要自己创建一个还是使用textureview自动创建的?如果我使用textureview自动创建的那个,我如何获得surfacetexture绑定的纹理ID,以便我可以在windowsurface上绘制框架?(另外,我查看了android sdk,看起来surfacetexture没有绑定当它s created), and if I use the one I created like in my code, there will be the error I mentioned and it doesnt 工作时到任何 GLcontext....
    • 刚刚创建了一个我现在拥有的演示,你能看看吗? github.com/MICHAEL-ZENGZF/Get_SurfaceTexture_Buffer/blob/master/…
    • 您的演示不正确。正确的做法是: 1、使用textureView中的surfacetexture来创建一个屏幕windowsurface 2 。使用 oes 纹理创建另一个表面纹理 3. 将相机绑定到新的表面纹理,因此相机将为表面纹理提供框架 4. 将 oes 纹理中的框架绘制到屏幕窗口表面。
    • 太棒了!!!你完美解决了我的问题~另外,我还有一个问题,当我尝试直接从窗口表面读取像素时,相机预览真的很慢,所以我将纹理渲染到 OffscreenSurface 然后它解决了问题,你能告诉我这背后的机制是什么?
    • opengl api readPixel 会将数据从 gpu ram 读取到 cpu ram,这很慢。所以readPixel会阻塞渲染线程,相机预览的fps会降低。如果你想要更流畅的效果,你应该使用从opengl 3.0开始引入的PBO技术。
    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多