YUV解码功能错误或硬件问题?

时间:2010-10-27 12:54:52

标签: android graph yuv

我将YUV转换为RGB功能1& 2(来自堆栈溢出)

但结果错误,如http://163.18.62.32/device.jpg

我不明白步骤中的错误

我的设备是Moto Milestone 2.1更新1

功能1

    public int[] decodeYUV420SP( byte[] yuv420sp, int width, int height) {   

        final int frameSize = width * height;   

        int rgb[]=new int[width*height];   
        for (int j = 0, yp = 0; j < height; j++) {   
            int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;   
            for (int i = 0; i < width; i++, yp++) {   
                int 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;   
                }   

                int y1192 = 1192 * y;   
                int r = (y1192 + 1634 * v);   
                int g = (y1192 - 833 * v - 400 * u);   
                int 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;   

                rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) &    
        0xff00) | ((b >> 10) & 0xff);   


            }   
        }   
        return rgb;   
        } 

功能2

public void decodeYUV(int[] out, byte[] fg, int width, int height)
  throws NullPointerException, IllegalArgumentException {
int sz = width * height;
if (out == null)
  throw new NullPointerException("buffer out is null");
if (out.length < sz)
  throw new IllegalArgumentException("buffer out size " + out.length
          + " < minimum " + sz);
if (fg == null)
  throw new NullPointerException("buffer 'fg' is null");
if (fg.length < sz)
  throw new IllegalArgumentException("buffer fg size " + fg.length
          + " < minimum " + sz * 3 / 2);
int i, j;
int Y, Cr = 0, Cb = 0;
for (j = 0; j < height; j++) {
  int pixPtr = j * width;
  final int jDiv2 = j >> 1;
  for (i = 0; i < width; i++) {
      Y = fg[pixPtr];
      if (Y < 0)
          Y += 255;
      if ((i & 0x1) != 1) {
          final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
          Cb = fg[cOff];
          if (Cb < 0)
              Cb += 127;
          else
              Cb -= 128;
          Cr = fg[cOff + 1];
          if (Cr < 0)
              Cr += 127;
          else
              Cr -= 128;
      }
      int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
      if (R < 0)
          R = 0;
      else if (R > 255)
          R = 255;
      int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1)
              + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
      if (G < 0)
          G = 0;
      else if (G > 255)
          G = 255;
      int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
      if (B < 0)
          B = 0;
      else if (B > 255)
          B = 255;
      out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
  }
}
}

我就像这样使用它们

功能1

int[] rgbBuf =decodeYUV420SP(_data,height,width);

功能2

int[] rgbBuf = new int[height*width];
decodeYUV(rgbBuf,_height,.width);

转换为Bitmap并显示在

Bitmap bm = Bitmap.createBitmap(rgbBuf,width,height);
View01.setImageBitmap(bm);

2 个答案:

答案 0 :(得分:1)

据我所知,没有Bitmap.createBitmap(.....);
使用以下参数集(byte[] inVal, width, height)

答案 1 :(得分:0)

两种解码方法的签名都有宽度,然后是高度。

你的电话似乎翻了一圈。

public int[] decodeYUV420SP( byte[] yuv420sp, int width, int height)

但你打电话:

decodeYUV420SP(_data,height,width);

public void decodeYUV(int[] out, byte[] fg, int width, int height)

但你打电话:

decodeYUV(rgbBuf,_height,.width);