title: camera2 api convert yuv420 to rgb green out
body: <p>i trying convert image from YUV_420_888 to rgb and i have some trouble with output image. In ImageReader i get image in format  YUV_420_888 (using camera 2 api for get this image preview). </p>
  
  <pre><code>imageReader = ImageReader.newInstance(1920,1080,ImageFormat.YUV_420_888,10);
  </code></pre>
  
  <p>In android sdk for YuvImage class writing, that YuvImage using only NV21, YUY2. </p>
  
  <p>as we can see difference between N21 and yuv420 not big and i try convert data to N21</p>
  
  <p><strong>YUV420:</strong>
  <a href="https://i.stack.imgur.com/cwG0i.png" rel="noreferrer"><img src="https://i.stack.imgur.com/cwG0i.png" alt="enter image description here"></a></p>
  
  <p>and <strong>N21:</strong>
  <a href="https://i.stack.imgur.com/HxG1S.png" rel="noreferrer"><img src="https://i.stack.imgur.com/HxG1S.png" alt="enter image description here"></a></p>
  
  <p>in <strong>onImageAvailable</strong> i get separately each Planes and put them in correct place (as on image)</p>
  
  <pre><code>ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  
  ByteBuffer bufferY = image.getPlanes()[0].getBuffer();
  byte[] data0 = new byte[bufferY.remaining()];
  bufferY.get(data0);
  
  ByteBuffer bufferU = image.getPlanes()[1].getBuffer();
  byte[] data1 = new byte[bufferU.remaining()];
  bufferU.get(data1);
  
  ByteBuffer bufferV = image.getPlanes()[2].getBuffer();
  byte[] data2 = new byte[bufferV.remaining()];
  bufferV.get(data2);
  ...
  outputStream.write(data0);
  for (int i=0;i&lt;bufferV.remaining();i++) {
      outputStream.write(data1[i]);
      outputStream.write(data2[i]);
  }
  </code></pre>
  
  <p>after create YuvImage, convert to Bitmap, view and save</p>
  
  <pre><code>final YuvImage yuvImage = new YuvImage(outputStream.toByteArray(), ImageFormat.NV21, 1920,1080, null);
  ByteArrayOutputStream outBitmap = new ByteArrayOutputStream();
  
  yuvImage.compressToJpeg(new Rect(0, 0,1920, 1080), 95, outBitmap);
  
  byte[] imageBytes = outBitmap.toByteArray();
  
  final Bitmap imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
  mImageView.setImageBitmap(imageBitmap);
  ...
  imageBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
  </code></pre>
  
  <p>but my saved  image is green and pink:
  <a href="https://i.stack.imgur.com/RK7xn.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/RK7xn.jpg" alt="capture image"></a>
  <a href="https://i.stack.imgur.com/VYq3c.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/VYq3c.jpg" alt="capture image"></a></p>
  
  <p><strong>what did i miss??</strong> </p>