title: YUV decode function error or hardware problem?
body: <p>I get the  YUV to RGB function 1&amp;2 (from stack overflow)</p>
  
  <p>but the result is wrong like this <a href="http://163.18.62.32/device.jpg" rel="nofollow">http://163.18.62.32/device.jpg</a></p>
  
  <p>I dont understand what's wrong in the step </p>
  
  <p>my device is Moto Milestone with 2.1 update 1</p>
  
  <p><strong>function 1</strong></p>
  
  <pre><code>    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 &lt; height; j++) {   
              int uvp = frameSize + (j &gt;&gt; 1) * width, u = 0, v = 0;   
              for (int i = 0; i &lt; width; i++, yp++) {   
                  int y = (0xff &amp; ((int) yuv420sp[yp])) - 16;   
                  if (y &lt; 0) y = 0;   
                  if ((i &amp; 1) == 0) {   
                      v = (0xff &amp; yuv420sp[uvp++]) - 128;   
                      u = (0xff &amp; 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 &lt; 0) r = 0; else if (r &gt; 262143) r = 262143;   
                  if (g &lt; 0) g = 0; else if (g &gt; 262143) g = 262143;   
                  if (b &lt; 0) b = 0; else if (b &gt; 262143) b = 262143;   
  
                  rgb[yp] = 0xff000000 | ((r &lt;&lt; 6) &amp; 0xff0000) | ((g &gt;&gt; 2) &amp;    
          0xff00) | ((b &gt;&gt; 10) &amp; 0xff);   
  
  
              }   
          }   
          return rgb;   
          } 
  </code></pre>
  
  <p><strong>function 2</strong></p>
  
  <pre><code>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 &lt; sz)
    throw new IllegalArgumentException("buffer out size " + out.length
            + " &lt; minimum " + sz);
  if (fg == null)
    throw new NullPointerException("buffer 'fg' is null");
  if (fg.length &lt; sz)
    throw new IllegalArgumentException("buffer fg size " + fg.length
            + " &lt; minimum " + sz * 3 / 2);
  int i, j;
  int Y, Cr = 0, Cb = 0;
  for (j = 0; j &lt; height; j++) {
    int pixPtr = j * width;
    final int jDiv2 = j &gt;&gt; 1;
    for (i = 0; i &lt; width; i++) {
        Y = fg[pixPtr];
        if (Y &lt; 0)
            Y += 255;
        if ((i &amp; 0x1) != 1) {
            final int cOff = sz + jDiv2 * width + (i &gt;&gt; 1) * 2;
            Cb = fg[cOff];
            if (Cb &lt; 0)
                Cb += 127;
            else
                Cb -= 128;
            Cr = fg[cOff + 1];
            if (Cr &lt; 0)
                Cr += 127;
            else
                Cr -= 128;
        }
        int R = Y + Cr + (Cr &gt;&gt; 2) + (Cr &gt;&gt; 3) + (Cr &gt;&gt; 5);
        if (R &lt; 0)
            R = 0;
        else if (R &gt; 255)
            R = 255;
        int G = Y - (Cb &gt;&gt; 2) + (Cb &gt;&gt; 4) + (Cb &gt;&gt; 5) - (Cr &gt;&gt; 1)
                + (Cr &gt;&gt; 3) + (Cr &gt;&gt; 4) + (Cr &gt;&gt; 5);
        if (G &lt; 0)
            G = 0;
        else if (G &gt; 255)
            G = 255;
        int B = Y + Cb + (Cb &gt;&gt; 1) + (Cb &gt;&gt; 2) + (Cb &gt;&gt; 6);
        if (B &lt; 0)
            B = 0;
        else if (B &gt; 255)
            B = 255;
        out[pixPtr++] = 0xff000000 + (B &lt;&lt; 16) + (G &lt;&lt; 8) + R;
    }
  }
  }
  </code></pre>
  
  <p>and i use them like this</p>
  
  <p><strong>function 1</strong></p>
  
  <pre><code>int[] rgbBuf =decodeYUV420SP(_data,height,width);
  </code></pre>
  
  <p><strong>function 2</strong></p>
  
  <pre><code>int[] rgbBuf = new int[height*width];
  decodeYUV(rgbBuf,_height,.width);
  </code></pre>
  
  <p>than convert to Bitmap and show on</p>
  
  <pre><code>Bitmap bm = Bitmap.createBitmap(rgbBuf,width,height);
  View01.setImageBitmap(bm);
  </code></pre>