Bitmap和YUV的转换
以前做过的一个视频通话中,有用到Bitmap和YUV的转换,现在整理出来。
参考自:http://blog.csdn.net/lancees/article/details/7686046
http://www.cnblogs.com/leaven/archive/2012/09/06/2672830.html
以下是各种转换,仅供参考
- private static Bitmap getAssetFile() {
- Bitmap bitmap = null;
- try {
- bitmap = BitmapFactory.decodeStream(NgnApplication.getContext()
- .getAssets().open("chat_default_bg.png"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return bitmap;
- }
- public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
- int len = width * height;
- // yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
- byte[] yuv = new byte[len * 3 / 2];
- int y, u, v;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- // 屏蔽ARGB的透明度值
- int rgb = pixels[i * width + j] & 0x00FFFFFF;
- // 像素的颜色顺序为bgr,移位运算。
- int r = rgb & 0xFF;
- int g = (rgb >> 8) & 0xFF;
- int b = (rgb >> 16) & 0xFF;
- // 套用公式
- y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
- u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
- v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
- // rgb2yuv
- // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
- // u = (int) (-0.147 * r - 0.289 * g + 0.437 * b);
- // v = (int) (0.615 * r - 0.515 * g - 0.1 * b);
- // RGB转换YCbCr
- // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
- // u = (int) (-0.1687 * r - 0.3313 * g + 0.5 * b + 128);
- // if (u > 255)
- // u = 255;
- // v = (int) (0.5 * r - 0.4187 * g - 0.0813 * b + 128);
- // if (v > 255)
- // v = 255;
- // 调整
- y = y < 16 ? 16 : (y > 255 ? 255 : y);
- u = u < 0 ? 0 : (u > 255 ? 255 : u);
- v = v < 0 ? 0 : (v > 255 ? 255 : v);
- // 赋值
- yuv[i * width + j] = (byte) y;
- yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
- yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;
- }
- }
- return yuv;
- }
- public static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,
- int width, int height) {
- final int frameSize = width * height;
- if (rgbBuf == null)
- throw new NullPointerException("buffer 'rgbBuf' is null");
- if (rgbBuf.length < frameSize * 3)
- throw new IllegalArgumentException("buffer 'rgbBuf' size "
- + rgbBuf.length + " < minimum " + frameSize * 3);
- if (yuv420sp == null)
- throw new NullPointerException("buffer 'yuv420sp' is null");
- if (yuv420sp.length < frameSize * 3 / 2)
- throw new IllegalArgumentException("buffer 'yuv420sp' size "
- + yuv420sp.length + " < minimum " + frameSize * 3 / 2);
- int i = 0, y = 0;
- int uvp = 0, u = 0, v = 0;
- int y1192 = 0, r = 0, g = 0, b = 0;
- for (int j = 0, yp = 0; j < height; j++) {
- uvp = frameSize + (j >> 1) * width;
- u = 0;
- v = 0;
- for (i = 0; i < width; i++, yp++) {
- y = (0xff & ((int) yuv420sp[yp])) - 16;
- if (y < 0)
- y = 0;
- if ((i & 1) == 0) {
- v = (0xff & yuv420sp[uvp++]) - 128;
- u = (0xff & yuv420sp[uvp++]) - 128;
- }
- y1192 = 1192 * y;
- r = (y1192 + 1634 * v);
- g = (y1192 - 833 * v - 400 * u);
- b = (y1192 + 2066 * u);
- if (r < 0)
- r = 0;
- else if (r > 262143)
- r = 262143;
- if (g < 0)
- g = 0;
- else if (g > 262143)
- g = 262143;
- if (b < 0)
- b = 0;
- else if (b > 262143)
- b = 262143;
- rgbBuf[yp * 3] = (byte) (r >> 10);
- rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
- rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
- }
- }
- }
- /*
- * 获取位图的RGB数据
- */
- public static byte[] getRGBByBitmap(Bitmap bitmap) {
- if (bitmap == null) {
- return null;
- }
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- int size = width * height;
- int pixels[] = new int[size];
- bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
- byte[] data = convertColorToByte(pixels);
- return data;
- }
- /*
- * 获取位图的YUV数据
- */
- public static byte[] getYUVByBitmap(Bitmap bitmap) {
- if (bitmap == null) {
- return null;
- }
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- int size = width * height;
- int pixels[] = new int[size];
- bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
- // byte[] data = convertColorToByte(pixels);
- byte[] data = rgb2YCbCr420(pixels, width, height);
- return data;
- }
- /*
- * 像素数组转化为RGB数组
- */
- public static byte[] convertColorToByte(int color[]) {
- if (color == null) {
- return null;
- }
- byte[] data = new byte[color.length * 3];
- for (int i = 0; i < color.length; i++) {
- data[i * 3] = (byte) (color[i] >> 16 & 0xff);
- data[i * 3 + 1] = (byte) (color[i] >> 8 & 0xff);
- data[i * 3 + 2] = (byte) (color[i] & 0xff);
- }
- return data;
- }
- // untested function
- byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
- int [] argb = new int[inputWidth * inputHeight];
- scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
- byte [] yuv = new byte[inputWidth*inputHeight*3/2];
- encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
- scaled.recycle();
- return yuv;
- }
- void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
- final int frameSize = width * height;
- int yIndex = 0;
- int uvIndex = frameSize;
- int a, R, G, B, Y, U, V;
- int index = 0;
- for (int j = 0; j < height; j++) {
- for (int i = 0; i < width; i++) {
- a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
- R = (argb[index] & 0xff0000) >> 16;
- G = (argb[index] & 0xff00) >> 8;
- B = (argb[index] & 0xff) >> 0;
- // well known RGB to YUV algorithm
- Y = ( ( 66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
- U = ( ( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
- V = ( ( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
- // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
- // meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
- // pixel AND every other scanline.
- yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
- if (j % 2 == 0 && index % 2 == 0) {
- yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
- yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
- }
- index ++;
- }
- }
- }
- static public void encodeYUV420SP(byte[] yuv420sp, int[] rgba, int width,
- int height) {
- final int frameSize = width * height;
- int[] U, V;
- U = new int[frameSize];
- V = new int[frameSize];
- final int uvwidth = width / 2;
- int r, g, b, y, u, v;
- for (int j = 0; j < height; j++) {
- int index = width * j;
- for (int i = 0; i < width; i++) {
- r = (rgba[index] & 0xff000000) >> 24;
- g = (rgba[index] & 0xff0000) >> 16;
- b = (rgba[index] & 0xff00) >> 8;
- // rgb to yuv
- y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;
- u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;
- v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;
- // clip y
- yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));
- U[index] = u;
- V[index++] = v;
- }
- }
- }
- static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
- final int frameSize = width * height;
- int yIndex = 0;
- int uvIndex = frameSize;
- int R, G, B, Y, U, V;
- int index = 0;
- for (int j = 0; j < height; j++) {
- for (int i = 0; i < width; i++) {
- R = (argb[index] & 0xff0000) >> 16;
- G = (argb[index] & 0xff00) >> 8;
- B = (argb[index] & 0xff) >> 0;
- // well known RGB to YUV algorithm
- Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
- U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
- V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
- // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
- // meaning for every 4 Y pixels there are 1 V and 1 U. Note the sampling is every other
- // pixel AND every other scanline.
- yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
- if (j % 2 == 0 && index % 2 == 0) {
- yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
- yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
- }
- index++;
- }
- }
- }
Bitmap和YUV的转换相关推荐
- 简介Bitmap、YUV,NV21与Bitmap互转
1. Bitmap 1.1Bitmap简介 关于Bitmap,它和Drawable差不多就是一种图片,Bitmap相关的使用主要有两种: 给ImageView设置背景 当做画布来使用 分别对应下面两个 ...
- rgb到yuv的转换
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 RGB到YUV(4:2:0)的转换 实验目标:实验RGB到YUV 的转换 一.实验原理 二.编程步骤 1.读入rgb分量 2.由RGB ...
- yuv格式转换是那个组织定义的_AI 如何赋能摄像机?这场沙龙为你解锁“软件定义”新概念...
在数字经济的发展中,数据基础设施将发挥关键支撑作用,机器视觉是数据基础设施非常重要的一环.当前 65% 的行业数字化信息来自视频,可见机器视觉与生活.娱乐.通信.生产.安全的深度结合下,一个千亿美元的 ...
- YUV格式转换RGB(基于opencv)
在编写代码将需要处理YUV格从每个视频帧中提取,然后将其保存为图片.有两种常见的方法在线,第一种是通过opencv自带cvCvtColor,可是这样的方法有bug.得到的图片会泛白.另外一种方法是公式 ...
- yuv格式转换是那个组织定义的_YUV格式
在前几篇介绍了OpenGL几种2D效果(旋转.平移.缩放.滤镜)后可以看到,GL图像颜色空间是用R.G.B.A,也就是红.绿.蓝 加一个透明度通道来表示的.比如,gl_FragColor 通常在取值的 ...
- RGB与YUV的转换
计算机领域,RGB被称为基色分量,组合后能显示的颜色叫做颜色空间,一般取值范围从0-255(2^8,可以显示1600万多种颜色:现在有的显示器为10位位深,即2^10,约可以显示10亿种颜色) 还有一 ...
- 流媒体之色彩转换——RGB(X)与YUV之间转换
文章目录 一:RGB32转RGB24 二:YUV与RGB相互转换 2.1 YUV->RGB 2.1.1 常规转换 2.1.2 去浮点转换 2.1.3 去浮点去乘法转换 2.1.4 查表转换 2. ...
- yuv显卡转换rgb
该文是转载:原文地址: http://blog.csdn.net/wangchenggggdn/archive/2010/07/05/5713075.aspx 首先要感谢ffmpeg, 如果没有它,所 ...
- 详解RGB和YUV色彩空间转换
前言 首先指出本文中的RGB指的是非线性RGB,意思就是经过了伽马校正,按照行业规矩应当写成R'G'B',但是为了书写方便,仍写成RGB.关于YUV有多种叫法,分别是YUV,YPbPr,YCbCr.因 ...
最新文章
- JavaScript有哪三部分组成?
- android 百度导航滑动会异常,百度导航Android版问题集
- MFC添加背景图片方法
- 一朝读码深似海,不读源码薪难升!读懂.NET5源码,到底多重要?
- 一分钟搞懂的算法之BPE算法
- j2ee学习方法摘要
- lua搭建ui_LTUI, 一个基于 lua 的跨平台字符终端 UI 界面库
- python库-密码学库pynacl
- java发送微信订阅消息
- PowerDesign 初识
- 路畅畅云固件升级教程_【图】E-Link功能/智能语音 路畅畅云+车载导航评测_汽配中国网...
- AttendanceRecord 考勤软件
- word文档字不靠边_word怎么调整单元格文字边距表格文字紧靠边框怎么办
- java实现黄金队列
- 手机邮箱怎么弄_我的手机邮箱怎么设置
- 硬替换WIN10雅黑和宋体的苹方字体
- 计算机毕设Python+Vue智能导诊系统(程序+LW+部署)
- 大一学生Web课程设计 红酒美食主题网页制作(HTML+CSS+JavaScript)
- 应届毕业生所需准备的面试话术及套路回答
- 国外问卷调查项目的现状与前景
热门文章
- C 一个非递减数组 下标从0到n 元素的取值范围为从0到n的整数 判断其中是否有重复元素
- c++连接oracle数据库程序,无法从c++程序连接到我的oracle数据库
- linux sa 命令,Linux 常用命令全拼
- php获取类的熟悉,php如何获取类的所有方法
- caffeine_使用Caffeine和Spring Boot的多个缓存配置
- mvp的全称_现役最强外援,总决赛MVP,来到CBA之后赚了多少钱?
- 试试这款针对JAVA应用的开源防火墙!
- 每日一皮:周六了,想跟你说一句...
- 10年布局,这道题都不懂就别想进阿里
- 全球最大同性交友网站必备的五大神器!