Android Bitmap和YUV之间的相互转换

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 17:01   45   0

转自文章《Bitmap和YUV的转换》http://blog.csdn.net/up1up2up3/article/details/8108902

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

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

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

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

[java] view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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. }


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP