title: ImageReader format compatibility for FirebaseVisionImageMetadata (via Camera2 API on Android)
body: <p>I'd like to use the image byte array from the ImageReader to construct a <code>FirebaseVisionImageMetadata</code> object for barcode scanning (as part of the <a href="https://firebase.google.com/docs/ml-kit/read-barcodes" rel="nofollow noreferrer">ML Kit</a> ). I'm using the Camera2 API and following <a href="https://github.com/google/cameraview/pull/123/commits/f90580b1891c3edf91b4a254219d08b01110afce" rel="nofollow noreferrer">this PR</a> for supporting frame previews.</p>
  
  <pre><code>Size sz = chooseOptimalSize();
  mCallbackImageDataReader =
          ImageReader.newInstance(sz.getWidth(), sz.getHeight(), ImageFormat.YV12, 1);
      mCallbackImageDataReader.setOnImageAvailableListener(reader -&gt; {
          byte[] data = getByteDataFromImageReader(reader);
          if (data != null) {
               //rotation compensation calculation not shown
              mCallback.onPreviewFrame(data, reader.getImageFormat(), reader.getWidth(), reader.getHeight(), rotationCompensation);
          }
      }, null);
  
  @Nullable
  private byte[] getByteDataFromImageReader(@NonNull ImageReader reader) {
      byte[] data = null;
      try (Image image = reader.acquireNextImage()) {
          Image.Plane[] planes = image.getPlanes();
          if (planes.length &gt; 0) {
              ByteBuffer buffer = planes[0].getBuffer();
              data = new byte[buffer.remaining()];
              buffer.get(data);
          }
      }
      return data;
  } 
  </code></pre>
  
  <p>However, when I pass in the resulting format to the <code>FirebaseVisionImageMetadata</code> builder, I get the following error:</p>
  
  <pre><code>final FirebaseVisionImageMetadata meta = new FirebaseVisionImageMetadata.Builder()
          .setFormat(format) //exception thrown here
          .setWidth(width)
          .setHeight(height)
          .setRotation(rotation)
          .build();
  
  java.lang.IllegalArgumentException
      at com.google.android.gms.common.internal.Preconditions.checkArgument(Unknown Source:5)
      at com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata$Builder.setFormat(Unknown Source:13)
  </code></pre>
  
  <p>The format I’m passing in to the <code>ImageReader</code> is <code>ImageReader.YV12</code> since <code>FirebaseVisionImageMetadata</code> only accepts either <code>NV21</code> or <code>YV12</code>, but since <code>ImageReader</code> doesn’t accept <code>NV21</code>, I’m left with <code>YV12</code>. When I use this format (as shown above), the camera launches with a black screen. I've also tried passing in <code>ImageReader.YUV_420_888</code>, but that results in the same <code>IllegalArgumentException</code> from <code>FirebaseVisionImageMetadata</code>.</p>
  
  <p>My guess is that I probably have to convert the byte array differently, or to a different format (?). </p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Sources: </p>
  
  <ol>
  <li><a href="https://github.com/google/cameraview/pull/123/commits/f90580b1891c3edf91b4a254219d08b01110afce" rel="nofollow noreferrer">https://github.com/google/cameraview/pull/123/commits/f90580b1891c3edf91b4a254219d08b01110afce</a></li>
  <li><a href="https://developer.android.com/reference/android/graphics/ImageFormat.html?hl=BG#YUV_420_888" rel="nofollow noreferrer">https://developer.android.com/reference/android/graphics/ImageFormat.html?hl=BG#YUV_420_888</a></li>
  </ol>