[英]Android OpenGL ES process yuv preview as RGBA
我想使用OpenGL处理来自Android平台上格式为NV21的相机中的数据。
我的代码如下:
顶点着色器:
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:
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_LUMINANCE, w, h, 0, GLES20.GL_LUMINANCE, GLES20.GL_UNSIGNED_BYTE, ByteBuffer.wrap(data));
数据是摄像机预览的nv21格式字节数组,然后发送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));
那就是我认为很重要的所有代码。 但是,当我运行程序时,我发现GLSurfaceView中的结果看起来更蓝。 我的代码有什么问题吗? 我很麻烦
为什么不使用GLSurfaceView并生成SurfaceTexture ,然后使用setPreviewTexture将其传递到相机中呢? 如果要像素RGB数据,请使用glReadPixels获取缓冲区数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.