title: Converting YUV_420_888 to JPEG and saving file results distorted image
body: <p>I've used the <code>ImageUtil</code> class provided in <a href="https://stackoverflow.com/a/40152147/2949966">https://stackoverflow.com/a/40152147/2949966</a> within my git repo: <a href="https://github.com/ahasbini/cameraview/tree/camera_preview_imp" rel="noreferrer">https://github.com/ahasbini/cameraview/tree/camera_preview_imp</a> (note the implementation is in <code>camera_preview_imp</code> branch) to implement a frame preview callback. An <code>ImageReader</code> is set to preview frames in the <code>ImageFormat.YUV_420_888</code> format which will be converted into <code>ImageFormat.JPEG</code> using the <code>ImageUtil</code> class and send it to the frame callback. The demo app saves a frame from the callback to a file every 50 frames. All of the saved frame images are coming out distorted similar to below:</p>
  
  <p><a href="https://i.stack.imgur.com/GZpHX.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/GZpHX.jpg" alt="enter image description here"></a></p>
  
  <p>If I've changed the <code>ImageReader</code> to use <code>ImageFormat.JPEG</code> instead by doing the following changes in <code>Camera2</code>:</p>
  
  <pre><code>mPreviewImageReader = ImageReader.newInstance(previewSize.getWidth(),
      previewSize.getHeight(), ImageFormat.JPEG, /* maxImages */ 2);
  mCamera.createCaptureSession(Arrays.asList(surface, mPreviewImageReader.getSurface()),
      mSessionCallback, null);
  </code></pre>
  
  <p>the image is coming properly without any distortions however the frame rate drops significantly and the view starts to lag. Hence I believe the <code>ImageUtil</code> class is not converting properly.</p>