You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

1. Core Grayscale Filter Implementation

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);
}
2. Camera2 + OpenGL ES Integration Steps

Here’s how to wire up the camera preview to your OpenGL renderer:

  • Set up a SurfaceTexture for OpenGL input
    Create a SurfaceTexture that 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 a GLSurfaceView.Renderer to handle drawing the filtered frame:

    1. On surface creation: Compile and link the vertex/fragment shaders into a program.
    2. 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.
3. Minimal Sample Project Structure

A self-contained test app would look like this:

  • MainActivity: Manages Camera2 lifecycle, initializes the GLSurfaceView, and sets up the capture session.
  • GrayscaleRenderer: Implements GLSurfaceView.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:05:08