问题
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
Using Opengl in JAVA
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
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);
pass the texture to shader
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, m_TextureMain); glUniform1i(textures[0][displayMode], 0);
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