Camera2 API中应用OpenGL黑白灰度滤镜的示例应用咨询
Hey there! No worries at all—getting started with Camera2 + OpenGL ES filters can feel a bit overwhelming at first, but I’ve got some solid, self-contained examples and pointers to help you build that grayscale filter step by step.
Let’s start with the most critical parts: the shader code and how to connect Camera2’s preview to OpenGL ES.
Shader Code for Grayscale Filter
These are the two shaders you’ll need—they’re simple and focused solely on converting color frames to black and white.
Vertex Shader
This handles passing vertex and texture coordinates to the fragment shader:
attribute vec4 aPosition; attribute vec2 aTexCoord; varying vec2 vTexCoord; void main() { gl_Position = aPosition; vTexCoord = aTexCoord; }
Fragment Shader
This calculates the grayscale value using the standard luminance formula (0.299R + 0.587G + 0.114*B):
precision mediump float; varying vec2 vTexCoord; uniform sampler2D sTexture; void main() { vec4 color = texture2D(sTexture, vTexCoord); float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114)); gl_FragColor = vec4(gray, gray, gray, color.a); }
Here’s how to wire up the camera preview to your OpenGL renderer:
Set up a SurfaceTexture for OpenGL input
Create aSurfaceTexturethat will receive frames from Camera2. Pass this to your OpenGL renderer, and use it as the output surface when creating the Camera2 capture session:// In your Activity's Camera2 setup SurfaceTexture surfaceTexture = glSurfaceView.getHolder().getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight()); Surface surface = new Surface(surfaceTexture); // Add this surface to your capture session outputs cameraDevice.createCaptureSession(Arrays.asList(surface), captureSessionCallback, null);OpenGL Renderer Workflow
Implement aGLSurfaceView.Rendererto handle drawing the filtered frame:- On surface creation: Compile and link the vertex/fragment shaders into a program.
- On each frame update: Call
surfaceTexture.updateTexImage()to get the latest camera frame, bind the texture to your shader’s sampler, then draw a full-screen rectangle to apply the filter.
A self-contained test app would look like this:
MainActivity: Manages Camera2 lifecycle, initializes theGLSurfaceView, and sets up the capture session.GrayscaleRenderer: ImplementsGLSurfaceView.Renderer, handles shader compilation, texture binding, and frame drawing.ShaderUtils: A helper class to load, compile, and link shaders (reduces boilerplate in the renderer).
This structure keeps things simple so you can focus on understanding how the camera feed connects to the OpenGL filter without extra complexity.
内容的提问来源于stack exchange,提问作者user2991413

