Android OpenGL ES process yuv preview as RGBA

拟墨画扇 提交于 2019-12-23 05:37:11

问题


I want to use OpenGL to process the data from camera which format is NV21 on Android platform.

My code was below:

vertex shader:

attribute vec4 position;
attribute vec2 inputTextureCoordinate;
varying vec2 v_texCoord;
void main()
{
   gl_Position = position;
   v_texCoord = inputTextureCoordinate;
}

fragment shader:

precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D yTexture; 
uniform sampler2D uvTexture;
const mat3 yuv2rgb = mat3(
                        1, 0, 1.2802,
                        1, -0.214821, -0.380589,
                        1, 2.127982, 0
                        );

void main() {    
    vec3 yuv = vec3(
                1.1643 * (texture2D(yTexture, v_texCoord).r - 0.0627),
                texture2D(uvTexture, v_texCoord).a - 0.5,
                texture2D(uvTexture, v_texCoord).r - 0.5
                );
    vec3 rgb = yuv * yuv2rgb;
    gl_FragColor = vec4(rgb, 1.0);
}

I send yTexture:

GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, w, h, 0,                  GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE,                           ByteBuffer.wrap(data));

where data was the nv21 format byte array of camera preview And I send uvTexture:

byte[] luminanceAlpha = new byte[w * h / 2];
System.arraycopy(data, w * h, luminanceAlpha, 0, w * h / 2);
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0,            GLES20.GL_LUMINANCE_ALPHA, w / 2, h / 2, 0,                             GLES20.GL_LUMINANCE_ALPHA,                              GLES20.GL_UNSIGNED_BYTE, ByteBuffer.wrap(luminanceAlpha));

That's all the code which I thought was important. But when I run the program, I found the result in GLSurfaceView looks more bluer. Is there any wrong with my code. I was very trouble.


回答1:


Why not using the GLSurfaceView and generate the SurfaceTexture, then passing it into camera using setPreviewTexture? If you want the pixel RGB data, using glReadPixels to get the buffer data.



来源:https://stackoverflow.com/questions/15634887/android-opengl-es-process-yuv-preview-as-rgba

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