1

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?

Community
  • 1
  • 1
user3331791
  • 11
  • 1
  • 2
  • Do you mean that you want to make the texture data to png or another image format ? – Sung Feb 21 '14 at 17:41
  • @SungWoo: Like the sample code ,the camera preview will be shown at the SurfaceTexture directly. But now I want to read the preview data from the camera,and do some process to the frame,and then show the processed data at the SurfaceTexture. How to read the camera preview data,and how to show the processed data? – user3331791 Feb 27 '14 at 01:32
  • I got it ! check my post below – Sung Feb 27 '14 at 06:14

1 Answers1

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.

Sung
  • 1,036
  • 1
  • 8
  • 22