Android Bitmap和YUV之间的相互转换

来源:互联网 发布:广电好还是网络电视好 编辑:程序博客网 时间:2024/06/03 17:25

转自文章《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.   
  13. public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {  
  14.     int len = width * height;  
  15.     // yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。  
  16.     byte[] yuv = new byte[len * 3 / 2];  
  17.     int y, u, v;  
  18.     for (int i = 0; i < height; i++) {  
  19.         for (int j = 0; j < width; j++) {  
  20.             // 屏蔽ARGB的透明度值  
  21.             int rgb = pixels[i * width + j] & 0x00FFFFFF;  
  22.             // 像素的颜色顺序为bgr,移位运算。  
  23.             int r = rgb & 0xFF;  
  24.             int g = (rgb >> 8) & 0xFF;  
  25.             int b = (rgb >> 16) & 0xFF;  
  26.             // 套用公式  
  27.             y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;  
  28.             u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;  
  29.             v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;  
  30.             // rgb2yuv  
  31.             // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);  
  32.             // u = (int) (-0.147 * r - 0.289 * g + 0.437 * b);  
  33.             // v = (int) (0.615 * r - 0.515 * g - 0.1 * b);  
  34.             // RGB转换YCbCr  
  35.             // y = (int) (0.299 * r + 0.587 * g + 0.114 * b);  
  36.             // u = (int) (-0.1687 * r - 0.3313 * g + 0.5 * b + 128);  
  37.             // if (u > 255)  
  38.             // u = 255;  
  39.             // v = (int) (0.5 * r - 0.4187 * g - 0.0813 * b + 128);  
  40.             // if (v > 255)  
  41.             // v = 255;  
  42.             // 调整  
  43.             y = y < 16 ? 16 : (y > 255 ? 255 : y);  
  44.             u = u < 0 ? 0 : (u > 255 ? 255 : u);  
  45.             v = v < 0 ? 0 : (v > 255 ? 255 : v);  
  46.             // 赋值  
  47.             yuv[i * width + j] = (byte) y;  
  48.             yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;  
  49.             yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;  
  50.         }  
  51.     }  
  52.     return yuv;  
  53. }  
  54.   
  55. public static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,  
  56.         int width, int height) {  
  57.   
  58.     final int frameSize = width * height;  
  59.     if (rgbBuf == null)  
  60.   
  61.         throw new NullPointerException("buffer 'rgbBuf' is null");  
  62.     if (rgbBuf.length < frameSize * 3)  
  63.   
  64.         throw new IllegalArgumentException("buffer 'rgbBuf' size "  
  65.                 + rgbBuf.length + " < minimum " + frameSize * 3);  
  66.   
  67.     if (yuv420sp == null)  
  68.   
  69.         throw new NullPointerException("buffer 'yuv420sp' is null");  
  70.   
  71.     if (yuv420sp.length < frameSize * 3 / 2)  
  72.   
  73.         throw new IllegalArgumentException("buffer 'yuv420sp' size "  
  74.                 + yuv420sp.length + " < minimum " + frameSize * 3 / 2);  
  75.   
  76.     int i = 0, y = 0;  
  77.   
  78.     int uvp = 0, u = 0, v = 0;  
  79.   
  80.     int y1192 = 0, r = 0, g = 0, b = 0;  
  81.   
  82.     for (int j = 0, yp = 0; j < height; j++) {  
  83.         uvp = frameSize + (j >> 1) * width;  
  84.         u = 0;  
  85.         v = 0;  
  86.   
  87.         for (i = 0; i < width; i++, yp++) {  
  88.             y = (0xff & ((int) yuv420sp[yp])) - 16;  
  89.   
  90.             if (y < 0)  
  91.                 y = 0;  
  92.   
  93.             if ((i & 1) == 0) {  
  94.                 v = (0xff & yuv420sp[uvp++]) - 128;  
  95.                 u = (0xff & yuv420sp[uvp++]) - 128;  
  96.             }  
  97.   
  98.             y1192 = 1192 * y;  
  99.             r = (y1192 + 1634 * v);  
  100.             g = (y1192 - 833 * v - 400 * u);  
  101.             b = (y1192 + 2066 * u);  
  102.   
  103.             if (r < 0)  
  104.                 r = 0;  
  105.             else if (r > 262143)  
  106.                 r = 262143;  
  107.   
  108.             if (g < 0)  
  109.                 g = 0;  
  110.             else if (g > 262143)  
  111.                 g = 262143;  
  112.   
  113.             if (b < 0)  
  114.                 b = 0;  
  115.             else if (b > 262143)  
  116.                 b = 262143;  
  117.   
  118.             rgbBuf[yp * 3] = (byte) (r >> 10);  
  119.             rgbBuf[yp * 3 + 1] = (byte) (g >> 10);  
  120.             rgbBuf[yp * 3 + 2] = (byte) (b >> 10);  
  121.         }  
  122.     }  
  123. }  
  124.   
  125. /* 
  126.  * 获取位图的RGB数据 
  127.  */  
  128. public static byte[] getRGBByBitmap(Bitmap bitmap) {  
  129.     if (bitmap == null) {  
  130.         return null;  
  131.     }  
  132.   
  133.     int width = bitmap.getWidth();  
  134.     int height = bitmap.getHeight();  
  135.   
  136.     int size = width * height;  
  137.   
  138.     int pixels[] = new int[size];  
  139.     bitmap.getPixels(pixels, 0, width, 00, width, height);  
  140.   
  141.     byte[] data = convertColorToByte(pixels);  
  142.   
  143.     return data;  
  144. }  
  145.   
  146. /* 
  147.  * 获取位图的YUV数据 
  148.  */  
  149. public static byte[] getYUVByBitmap(Bitmap bitmap) {  
  150.     if (bitmap == null) {  
  151.         return null;  
  152.     }  
  153.     int width = bitmap.getWidth();  
  154.     int height = bitmap.getHeight();  
  155.   
  156.     int size = width * height;  
  157.   
  158.     int pixels[] = new int[size];  
  159.     bitmap.getPixels(pixels, 0, width, 00, width, height);  
  160.   
  161.     // byte[] data = convertColorToByte(pixels);  
  162.     byte[] data = rgb2YCbCr420(pixels, width, height);  
  163.   
  164.     return data;  
  165. }  
  166.   
  167. /* 
  168.  * 像素数组转化为RGB数组 
  169.  */  
  170. public static byte[] convertColorToByte(int color[]) {  
  171.     if (color == null) {  
  172.         return null;  
  173.     }  
  174.   
  175.     byte[] data = new byte[color.length * 3];  
  176.     for (int i = 0; i < color.length; i++) {  
  177.         data[i * 3] = (byte) (color[i] >> 16 & 0xff);  
  178.         data[i * 3 + 1] = (byte) (color[i] >> 8 & 0xff);  
  179.         data[i * 3 + 2] = (byte) (color[i] & 0xff);  
  180.     }  
  181.   
  182.     return data;  
  183.   
  184. }  

[java] view plain copy
  1. // untested function  
  2.     byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {  
  3.   
  4.         int [] argb = new int[inputWidth * inputHeight];  
  5.   
  6.         scaled.getPixels(argb, 0, inputWidth, 00, inputWidth, inputHeight);  
  7.   
  8.         byte [] yuv = new byte[inputWidth*inputHeight*3/2];  
  9.         encodeYUV420SP(yuv, argb, inputWidth, inputHeight);  
  10.   
  11.         scaled.recycle();  
  12.   
  13.         return yuv;  
  14.     }  
  15.   
  16.     void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {  
  17.         final int frameSize = width * height;  
  18.   
  19.         int yIndex = 0;  
  20.         int uvIndex = frameSize;  
  21.   
  22.         int a, R, G, B, Y, U, V;  
  23.         int index = 0;  
  24.         for (int j = 0; j < height; j++) {  
  25.             for (int i = 0; i < width; i++) {  
  26.   
  27.                 a = (argb[index] & 0xff000000) >> 24// a is not used obviously  
  28.                 R = (argb[index] & 0xff0000) >> 16;  
  29.                 G = (argb[index] & 0xff00) >> 8;  
  30.                 B = (argb[index] & 0xff) >> 0;  
  31.   
  32.                 // well known RGB to YUV algorithm  
  33.                 Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;  
  34.                 U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;  
  35.                 V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;  
  36.   
  37.                 // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2  
  38.                 //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other  
  39.                 //    pixel AND every other scanline.  
  40.                 yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));  
  41.                 if (j % 2 == 0 && index % 2 == 0) {   
  42.                     yuv420sp[uvIndex++] = (byte)((V<0) ? 0 : ((V > 255) ? 255 : V));  
  43.                     yuv420sp[uvIndex++] = (byte)((U<0) ? 0 : ((U > 255) ? 255 : U));  
  44.                 }  
  45.   
  46.                 index ++;  
  47.             }  
  48.         }  
  49.     }  
[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.   
  5.         int[] U, V;  
  6.         U = new int[frameSize];  
  7.         V = new int[frameSize];  
  8.   
  9.         final int uvwidth = width / 2;  
  10.   
  11.         int r, g, b, y, u, v;  
  12.         for (int j = 0; j < height; j++) {  
  13.             int index = width * j;  
  14.             for (int i = 0; i < width; i++) {  
  15.                 r = (rgba[index] & 0xff000000) >> 24;  
  16.                 g = (rgba[index] & 0xff0000) >> 16;  
  17.                 b = (rgba[index] & 0xff00) >> 8;  
  18.   
  19.                 // rgb to yuv  
  20.                 y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;  
  21.                 u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;  
  22.                 v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;  
  23.   
  24.                 // clip y  
  25.                 yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));  
  26.                 U[index] = u;  
  27.                 V[index++] = v;  
  28.             }  
  29.         }  
  30.     }  

[java] view plain copy
  1. static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {  
  2.         final int frameSize = width * height;  
  3.   
  4.         int yIndex = 0;  
  5.         int uvIndex = frameSize;  
  6.   
  7.         int R, G, B, Y, U, V;  
  8.         int index = 0;  
  9.         for (int j = 0; j < height; j++) {  
  10.             for (int i = 0; i < width; i++) {  
  11.   
  12.                 R = (argb[index] & 0xff0000) >> 16;  
  13.                 G = (argb[index] & 0xff00) >> 8;  
  14.                 B = (argb[index] & 0xff) >> 0;  
  15.   
  16.                 // well known RGB to YUV algorithm  
  17.                 Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;  
  18.                 U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;  
  19.                 V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;  
  20.   
  21.                 // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2  
  22.                 //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other  
  23.                 //    pixel AND every other scanline.  
  24.                 yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : ((Y > 255) ? 255 : Y));  
  25.                 if (j % 2 == 0 && index % 2 == 0) {  
  26.                     yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : ((V > 255) ? 255 : V));  
  27.                     yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : ((U > 255) ? 255 : U));  
  28.                 }  
  29.                 index++;  
  30.             }  
  31.         }  
  32.     }  


原创粉丝点击