How to show camera preview by opengl

假如想象 提交于 2019-12-23 03:47:10

问题


I want to show camera preview by SurfaceTexture. I have read this sample and it work well:: Modifying camera output using SurfaceTexture and OpenGL

Now I want to do some process to the preview frames, how to get the data and push to the texture?


回答1:


There are two ways to use opengl to show this pixels data

  1. Using Opengl in JAVA

  2. Using opengl in c or c++ ( JNI )

Actually, either way, It is going to be easy if you know what opengl pipeline is

I alway use opengl with c++ because of reusability anyway, just follow three steps below

  1. make a texture image using the preview buffer from android API (pixel is a pointer which points the camera frame buffer)

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0,GL_RGBA, GL_UNSIGNED_BYTE, pixel); 
    
  2. pass the texture to shader

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, m_TextureMain);
        glUniform1i(textures[0][displayMode], 0);
    
  3. process the texture at the frament shader ( texutre0 is what we just made and this code process it to grayscale image )

          static char grayscale[] = 
                "precision highp float; \n"
                "uniform sampler2D texture0;\n"
                "varying vec2 textureCoordinate;\n"
                "void main()\n"
                "{\n"
                "    float grayValue = dot(texture2D(texture0, textureCoordinate), vec4(0.3, 0.59, 0.11, 0.0));\n"
                "   gl_FragColor = vec4(grayValue, grayValue, grayValue, 1.0);\n"
                "}\n";
    

I omitted many steps which is required in opengl because It's too long such as binding, generate a framebuffer, a renderbuffer, shader settings and compile it and how to do double buffering to avoid flicking. If you don't what these are, find an example and change the three steps.



来源:https://stackoverflow.com/questions/21902490/how-to-show-camera-preview-by-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!