本文介绍了Android的OpenGL ES的过程YUV preVIEW为RGBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用OpenGL从摄像头,格式是NV21 Android平台处理数据。

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

我的code是如下:

顶点着色器:

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

片段着色器:

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);
}

我送yTexture:

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));

,其中数据是相机preVIEW的NV21格式字节数组
我送uvTexture:

where data was the nv21 format byte array of camera previewAnd 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));

这就是所有的code我认为是非常重要的。但是,当我运行程序时,我发现结果GLSurfaceView看起来更蓝。有什么毛病我code。我很烦恼。

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.

推荐答案

为什么不使用的并产生的表面纹理,然后将其传递到使用相机的?如果你想像素RGB数据,使用以获取缓冲区中的数据。

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.

这篇关于Android的OpenGL ES的过程YUV preVIEW为RGBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:27