YUV420转换成RGB

 

 private static void decodeYUV420SP(int[] 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++) {
    if (y < 0)
     y = 0;

    rgbBuf[yp * 3] = (byte) y;
    rgbBuf[yp * 3 + 1] = (byte) y;
    rgbBuf[yp * 3 + 2] = (byte) y;
    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;
    //在这里转换成byte在手机上显示时  比较亮的部分解码效果很差,呈现黄色块状,可能是因为颜色信息丢失的原因
    //rgbBuf[yp * 3] = (byte) (r >> 10);
    rgbBuf[yp * 3] =  (r >> 10);
    rgbBuf[yp * 3 + 1] =  (g >> 10);
    rgbBuf[yp * 3 + 2] =  (b >> 10);
    
    //mColors[j*stride+i]=(0xf0<<16)|((rgbBuf[yp * 3] )<<16)|((rgbBuf[yp * 3 + 1])<<8)|(rgbBuf[yp * 3 + 2]);//the shift operation generates the color ARGB
    mColors[j*stride+i]=(0xff<<24) | (rgbBuf[yp * 3]<<16)|(rgbBuf[yp * 3 + 1]<<8)|(rgbBuf[yp * 3 + 2]);//the shift operation generates the color ARGB
   }
  }
 }

 

 

-------------------------------------------------------------------------------------

bitMap2=Bitmap.createBitmap(mColors, 0, stride, width, height, Config.RGB_565);

-------------------------------------------------------------------------------------

               canvas=mSurfaceHolder.lockCanvas(null);
         canvas.drawBitmap(bitMap2, 0, 0, new Paint());
         mSurfaceHolder.unlockCanvasAndPost(canvas);// 更新屏幕显示内容

      

 

 

posted on 2011-10-15 16:47  h2内存数据库  阅读(2791)  评论(0编辑  收藏  举报

导航