title: camera2 output to Bitmap
body: <p>I'm trying to use <a href="https://developers.google.com/vision/barcodes-overview" rel="nofollow noreferrer">Google Mobile Vision API</a> with the camera2 module and I'm having a lot of trouble.</p>
  
  <p>I'm using Google's <a href="https://github.com/googlesamples/android-Camera2Video" rel="nofollow noreferrer">android-Camera2Video</a> example code as a base. I've modified it to include the following callback:</p>
  
  <p><strong>Camera2VideoFragment.java</strong></p>
  
  <pre><code>OnCameraImageAvailable mCameraImageCallback;
  
  public interface OnCameraImageAvailable {
      void onCameraImageAvailable(Image image);
  }
  
  
  ImageReader.OnImageAvailableListener mImageAvailable = new ImageReader.OnImageAvailableListener() {
      @Override
      public void onImageAvailable(ImageReader reader) {
  
          Image image = reader.acquireLatestImage();
          if (image == null)
              return;
  
          mCameraImageCallback.onCameraImageAvailable(image);
  
          image.close();
      }
  };
  </code></pre>
  
  <p>That way any fragment including <code>Camera2VideoFragment.java</code> can get access to its images.</p>
  
  <p>Now, The Barcode API only accepts <code>Bitmap</code> images, but I'm unable to convert <code>YUV_420_888</code> to Bitmap. Instead, I changed the <code>imageReader</code>'s file format to <code>JPEG</code> and ran the following conversion code:</p>
  
  <pre><code>    Image.Plane[] planes = image.getPlanes();
      ByteBuffer buffer = planes[0].getBuffer();
      buffer.rewind();
      byte[] data = new byte[buffer.capacity()];
      buffer.get(data);
      Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  </code></pre>
  
  <p>This worked but the framerate drop of feeding <code>JPEG</code> data to the <code>imageReader</code> was significant. I'm wondering if anyone has worked around this issue before.</p>