以前做过的一个视频通话中,有用到Bitmap和YUV的转换,现在整理出来。

参考自:http://blog.csdn.net/lancees/article/details/7686046

http://www.cnblogs.com/leaven/archive/2012/09/06/2672830.html

以下是各种转换,仅供参考

[java] view plaincopy
  1. private static Bitmap getAssetFile() {
  2. Bitmap bitmap = null;
  3. try {
  4. bitmap = BitmapFactory.decodeStream(NgnApplication.getContext()
  5. .getAssets().open("chat_default_bg.png"));
  6. } catch (IOException e) {
  7. // TODO Auto-generated catch block
  8. e.printStackTrace();
  9. }
  10. return bitmap;
  11. }
  12. public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
  13. int len = width * height;
  14. // yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
  15. byte[] yuv = new byte[len * 3 / 2];
  16. int y, u, v;
  17. for (int i = 0; i < height; i++) {
  18. for (int j = 0; j < width; j++) {
  19. // 屏蔽ARGB的透明度值
  20. int rgb = pixels[i * width + j] & 0x00FFFFFF;
  21. // 像素的颜色顺序为bgr,移位运算。
  22. int r = rgb & 0xFF;
  23. int g = (rgb >> 8) & 0xFF;
  24. int b = (rgb >> 16) & 0xFF;
  25. // 套用公式
  26. y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
  27. u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
  28. v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
  29. // rgb2yuv
  30. // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
  31. // u = (int) (-0.147 * r - 0.289 * g + 0.437 * b);
  32. // v = (int) (0.615 * r - 0.515 * g - 0.1 * b);
  33. // RGB转换YCbCr
  34. // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
  35. // u = (int) (-0.1687 * r - 0.3313 * g + 0.5 * b + 128);
  36. // if (u > 255)
  37. // u = 255;
  38. // v = (int) (0.5 * r - 0.4187 * g - 0.0813 * b + 128);
  39. // if (v > 255)
  40. // v = 255;
  41. // 调整
  42. y = y < 16 ? 16 : (y > 255 ? 255 : y);
  43. u = u < 0 ? 0 : (u > 255 ? 255 : u);
  44. v = v < 0 ? 0 : (v > 255 ? 255 : v);
  45. // 赋值
  46. yuv[i * width + j] = (byte) y;
  47. yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
  48. yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;
  49. }
  50. }
  51. return yuv;
  52. }
  53. public static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,
  54. int width, int height) {
  55. final int frameSize = width * height;
  56. if (rgbBuf == null)
  57. throw new NullPointerException("buffer 'rgbBuf' is null");
  58. if (rgbBuf.length < frameSize * 3)
  59. throw new IllegalArgumentException("buffer 'rgbBuf' size "
  60. + rgbBuf.length + " < minimum " + frameSize * 3);
  61. if (yuv420sp == null)
  62. throw new NullPointerException("buffer 'yuv420sp' is null");
  63. if (yuv420sp.length < frameSize * 3 / 2)
  64. throw new IllegalArgumentException("buffer 'yuv420sp' size "
  65. + yuv420sp.length + " < minimum " + frameSize * 3 / 2);
  66. int i = 0, y = 0;
  67. int uvp = 0, u = 0, v = 0;
  68. int y1192 = 0, r = 0, g = 0, b = 0;
  69. for (int j = 0, yp = 0; j < height; j++) {
  70. uvp = frameSize + (j >> 1) * width;
  71. u = 0;
  72. v = 0;
  73. for (i = 0; i < width; i++, yp++) {
  74. y = (0xff & ((int) yuv420sp[yp])) - 16;
  75. if (y < 0)
  76. y = 0;
  77. if ((i & 1) == 0) {
  78. v = (0xff & yuv420sp[uvp++]) - 128;
  79. u = (0xff & yuv420sp[uvp++]) - 128;
  80. }
  81. y1192 = 1192 * y;
  82. r = (y1192 + 1634 * v);
  83. g = (y1192 - 833 * v - 400 * u);
  84. b = (y1192 + 2066 * u);
  85. if (r < 0)
  86. r = 0;
  87. else if (r > 262143)
  88. r = 262143;
  89. if (g < 0)
  90. g = 0;
  91. else if (g > 262143)
  92. g = 262143;
  93. if (b < 0)
  94. b = 0;
  95. else if (b > 262143)
  96. b = 262143;
  97. rgbBuf[yp * 3] = (byte) (r >> 10);
  98. rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
  99. rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
  100. }
  101. }
  102. }
  103. /*
  104. * 获取位图的RGB数据
  105. */
  106. public static byte[] getRGBByBitmap(Bitmap bitmap) {
  107. if (bitmap == null) {
  108. return null;
  109. }
  110. int width = bitmap.getWidth();
  111. int height = bitmap.getHeight();
  112. int size = width * height;
  113. int pixels[] = new int[size];
  114. bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
  115. byte[] data = convertColorToByte(pixels);
  116. return data;
  117. }
  118. /*
  119. * 获取位图的YUV数据
  120. */
  121. public static byte[] getYUVByBitmap(Bitmap bitmap) {
  122. if (bitmap == null) {
  123. return null;
  124. }
  125. int width = bitmap.getWidth();
  126. int height = bitmap.getHeight();
  127. int size = width * height;
  128. int pixels[] = new int[size];
  129. bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
  130. // byte[] data = convertColorToByte(pixels);
  131. byte[] data = rgb2YCbCr420(pixels, width, height);
  132. return data;
  133. }
  134. /*
  135. * 像素数组转化为RGB数组
  136. */
  137. public static byte[] convertColorToByte(int color[]) {
  138. if (color == null) {
  139. return null;
  140. }
  141. byte[] data = new byte[color.length * 3];
  142. for (int i = 0; i < color.length; i++) {
  143. data[i * 3] = (byte) (color[i] >> 16 & 0xff);
  144. data[i * 3 + 1] = (byte) (color[i] >> 8 & 0xff);
  145. data[i * 3 + 2] = (byte) (color[i] & 0xff);
  146. }
  147. return data;
  148. }
[java] view plaincopy
  1. // untested function
  2. byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {
  3. int [] argb = new int[inputWidth * inputHeight];
  4. scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
  5. byte [] yuv = new byte[inputWidth*inputHeight*3/2];
  6. encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
  7. scaled.recycle();
  8. return yuv;
  9. }
  10. void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
  11. final int frameSize = width * height;
  12. int yIndex = 0;
  13. int uvIndex = frameSize;
  14. int a, R, G, B, Y, U, V;
  15. int index = 0;
  16. for (int j = 0; j < height; j++) {
  17. for (int i = 0; i < width; i++) {
  18. a = (argb[index] & 0xff000000) >> 24; // a is not used obviously
  19. R = (argb[index] & 0xff0000) >> 16;
  20. G = (argb[index] & 0xff00) >> 8;
  21. B = (argb[index] & 0xff) >> 0;
  22. // well known RGB to YUV algorithm
  23. Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;
  24. U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;
  25. V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;
  26. // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
  27. //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
  28. //    pixel AND every other scanline.
  29. yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
  30. if (j % 2 == 0 && index % 2 == 0) {
  31. yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));
  32. yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));
  33. }
  34. index ++;
  35. }
  36. }
  37. }
[java] view plaincopy
  1. static public void encodeYUV420SP(byte[] yuv420sp, int[] rgba, int width,
  2. int height) {
  3. final int frameSize = width * height;
  4. int[] U, V;
  5. U = new int[frameSize];
  6. V = new int[frameSize];
  7. final int uvwidth = width / 2;
  8. int r, g, b, y, u, v;
  9. for (int j = 0; j < height; j++) {
  10. int index = width * j;
  11. for (int i = 0; i < width; i++) {
  12. r = (rgba[index] & 0xff000000) >> 24;
  13. g = (rgba[index] & 0xff0000) >> 16;
  14. b = (rgba[index] & 0xff00) >> 8;
  15. // rgb to yuv
  16. y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;
  17. u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;
  18. v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;
  19. // clip y
  20. yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));
  21. U[index] = u;
  22. V[index++] = v;
  23. }
  24. }
  25. }
[java] view plaincopy
  1. static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
  2. final int frameSize = width * height;
  3. int yIndex = 0;
  4. int uvIndex = frameSize;
  5. int R, G, B, Y, U, V;
  6. int index = 0;
  7. for (int j = 0; j < height; j++) {
  8. for (int i = 0; i < width; i++) {
  9. R = (argb[index] & 0xff0000) >> 16;
  10. G = (argb[index] & 0xff00) >> 8;
  11. B = (argb[index] & 0xff) >> 0;
  12. // well known RGB to YUV algorithm
  13. Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
  14. U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
  15. V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
  16. // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
  17. //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
  18. //    pixel AND every other scanline.
  19. yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));
  20. if (j % 2 == 0 && index % 2 == 0) {
  21. yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));
  22. yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));
  23. }
  24. index++;
  25. }
  26. }
  27. }

Bitmap和YUV的转换相关推荐

  1. 简介Bitmap、YUV,NV21与Bitmap互转

    1. Bitmap 1.1Bitmap简介 关于Bitmap,它和Drawable差不多就是一种图片,Bitmap相关的使用主要有两种: 给ImageView设置背景 当做画布来使用 分别对应下面两个 ...

  2. rgb到yuv的转换

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 RGB到YUV(4:2:0)的转换 实验目标:实验RGB到YUV 的转换 一.实验原理 二.编程步骤 1.读入rgb分量 2.由RGB ...

  3. yuv格式转换是那个组织定义的_AI 如何赋能摄像机?这场沙龙为你解锁“软件定义”新概念...

    在数字经济的发展中,数据基础设施将发挥关键支撑作用,机器视觉是数据基础设施非常重要的一环.当前 65% 的行业数字化信息来自视频,可见机器视觉与生活.娱乐.通信.生产.安全的深度结合下,一个千亿美元的 ...

  4. YUV格式转换RGB(基于opencv)

    在编写代码将需要处理YUV格从每个视频帧中提取,然后将其保存为图片.有两种常见的方法在线,第一种是通过opencv自带cvCvtColor,可是这样的方法有bug.得到的图片会泛白.另外一种方法是公式 ...

  5. yuv格式转换是那个组织定义的_YUV格式

    在前几篇介绍了OpenGL几种2D效果(旋转.平移.缩放.滤镜)后可以看到,GL图像颜色空间是用R.G.B.A,也就是红.绿.蓝 加一个透明度通道来表示的.比如,gl_FragColor 通常在取值的 ...

  6. RGB与YUV的转换

    计算机领域,RGB被称为基色分量,组合后能显示的颜色叫做颜色空间,一般取值范围从0-255(2^8,可以显示1600万多种颜色:现在有的显示器为10位位深,即2^10,约可以显示10亿种颜色) 还有一 ...

  7. 流媒体之色彩转换——RGB(X)与YUV之间转换

    文章目录 一:RGB32转RGB24 二:YUV与RGB相互转换 2.1 YUV->RGB 2.1.1 常规转换 2.1.2 去浮点转换 2.1.3 去浮点去乘法转换 2.1.4 查表转换 2. ...

  8. yuv显卡转换rgb

    该文是转载:原文地址: http://blog.csdn.net/wangchenggggdn/archive/2010/07/05/5713075.aspx 首先要感谢ffmpeg, 如果没有它,所 ...

  9. 详解RGB和YUV色彩空间转换

    前言 首先指出本文中的RGB指的是非线性RGB,意思就是经过了伽马校正,按照行业规矩应当写成R'G'B',但是为了书写方便,仍写成RGB.关于YUV有多种叫法,分别是YUV,YPbPr,YCbCr.因 ...

最新文章

  1. JavaScript有哪三部分组成?
  2. android 百度导航滑动会异常,百度导航Android版问题集
  3. MFC添加背景图片方法
  4. 一朝读码深似海,不读源码薪难升!读懂.NET5源码,到底多重要?
  5. 一分钟搞懂的算法之BPE算法
  6. j2ee学习方法摘要
  7. lua搭建ui_LTUI, 一个基于 lua 的跨平台字符终端 UI 界面库
  8. python库-密码学库pynacl
  9. java发送微信订阅消息
  10. PowerDesign 初识
  11. 路畅畅云固件升级教程_【图】E-Link功能/智能语音 路畅畅云+车载导航评测_汽配中国网...
  12. AttendanceRecord 考勤软件
  13. word文档字不靠边_word怎么调整单元格文字边距表格文字紧靠边框怎么办
  14. java实现黄金队列
  15. 手机邮箱怎么弄_我的手机邮箱怎么设置
  16. 硬替换WIN10雅黑和宋体的苹方字体
  17. 计算机毕设Python+Vue智能导诊系统(程序+LW+部署)
  18. 大一学生Web课程设计 红酒美食主题网页制作(HTML+CSS+JavaScript)
  19. 应届毕业生所需准备的面试话术及套路回答
  20. 国外问卷调查项目的现状与前景

热门文章

  1. C 一个非递减数组 下标从0到n 元素的取值范围为从0到n的整数 判断其中是否有重复元素
  2. c++连接oracle数据库程序,无法从c++程序连接到我的oracle数据库
  3. linux sa 命令,Linux 常用命令全拼
  4. php获取类的熟悉,php如何获取类的所有方法
  5. caffeine_使用Caffeine和Spring Boot的多个缓存配置
  6. mvp的全称_现役最强外援,总决赛MVP,来到CBA之后赚了多少钱?
  7. 试试这款针对JAVA应用的开源防火墙!
  8. 每日一皮:周六了,想跟你说一句...
  9. 10年布局,这道题都不懂就别想进阿里
  10. 全球最大同性交友网站必备的五大神器!