Android GLSurfaceView 显示YUV数据的坑,图像偏蓝色
2017-10-13 15:18
615 查看
前几天学习使用 GLSurfaceView 显示摄像头获取到的YUV数据,发现画面偏蓝色。经过反复的查找,原来是采集的数据排列顺序的问题,
params.setPictureFormat(ImageFormat.NV21);
本来以为数据排列是
按照这个数据顺序,显示出来的图像有问题(偏蓝色),一个偶然的机会,突然想到是不是UV数据出错了。结果查看了帮助文档:
居然是 YCrCb,不是 YCbCr哦。
粗心的人伤不起啊,以后要多加仔细了。
下面放点源代码。
YUV转RGBA像素点算法
YUV数据分离
params.setPictureFormat(ImageFormat.NV21);
本来以为数据排列是
按照这个数据顺序,显示出来的图像有问题(偏蓝色),一个偶然的机会,突然想到是不是UV数据出错了。结果查看了帮助文档:
居然是 YCrCb,不是 YCbCr哦。
粗心的人伤不起啊,以后要多加仔细了。
下面放点源代码。
YUV转RGBA像素点算法
这里写代码片 private static final String vertexShaderString = "attribute vec4 vertexIn;" + "attribute vec2 textureIn;" + "varying vec2 textureOut;" + "void main() {" + "gl_Position = vertexIn;" + "textureOut = textureIn;" + "}"; private static final String yuvFragmentShaderString = "precision mediump float;" + "uniform sampler2D tex_y;" + "uniform sampler2D tex_u;" + "uniform sampler2D tex_v;" + "varying vec2 textureOut;" + "void main() {" + "float y = (texture2D(tex_y, textureOut).r - 16./255.) * 1.164;" + " if(y < 0.0) y=0.0;" + "vec4 c = vec4(y);" + "vec4 U = vec4(texture2D(tex_u, textureOut).r - 128./255.);" + "vec4 V = vec4(texture2D(tex_v, textureOut).r - 128./255.);" + "c += V * vec4(1.596, -0.813, 0, 0);" + "c += U * vec4(0, -0.392, 2.017, 0);" + // "c.a = 1.0;" + "gl_FragColor = c;" + "}";
YUV数据分离
这里写代码片 @Override public void onPreviewFrame(byte[] data, Camera camera) { Camera.Size size = camera.getParameters().getPreviewSize(); //获取预览大小 int width = size.width; int height = size.height; int pixelLength = width * height; byte[] yData = new byte[pixelLength]; byte[] uData = new byte[pixelLength / 4]; byte[] vData = new byte[pixelLength / 4]; System.arraycopy(data, 0, yData, 0, pixelLength); for (int i = 0; i < pixelLength / 4; i++) { vData[i] = data[pixelLength + i * 2]; uData[i] = data[pixelLength + i * 2 + 1]; } //处理YUV数据 }
相关文章推荐
- 如何在Android上快速显示yuv数据
- 【Android开发学习38】关于GLSurfaceView显示的几点说明
- Android用surface直接显示yuv数据(一)
- Android用surface直接显示yuv数据(二)
- android ViewPager在拖拽到左边和右边的时候,禁止显示黄色或者蓝色的渐变图片的解决方法
- Android上使用OpenGLES2.0显示YUV数据
- Android开发(16)-TextView显示表情图像和文字
- Android上使用OpenGLES2.0显示YUV数据
- Android使用OpenGL ES2.0显示YUV,您的手机上的数据要解决两个方面的坐标
- android GLSurfaceView显示图片的问题!!!!
- Android用surface直接显示yuv数据(一)
- Android下拉刷新库,利用viewdraghelper实现,集成了下拉刷新,底部加载更多,数据初始加载显示loading等功能
- Android使用surface直接显示yuv数据(三)
- ffmpeg转换和显示yuv,rgb等原始图像数据
- Android的TextView中显示图片和图像的方法
- Android的TextView中显示图片和图像的方法
- Android开发(16)-TextView显示表情图像和文字
- Android捕捉图像后在SurfaceView上变形显示问题的处理
- Android开发(16)-TextView显示表情图像和文字
- Android用surface直接显示yuv数据(三)