private static byte[] acquireJpegBytesAndClose(Image image){
  ByteBuffer buffer;
  if (image.getFormat() == ImageFormat.JPEG) {
    Image.Plane plane0=image.getPlanes()[0];
    buffer=plane0.getBuffer();
  }
 else   if (image.getFormat() == ImageFormat.YUV_420_888) {
    buffer=ByteBuffer.allocateDirect(image.getWidth() * image.getHeight() * 3);
    Log.v(TAG,"Compressing JPEG with software encoder.");
    int numBytes=JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(image),buffer,JPEG_QUALITY);
    if (numBytes < 0) {
      throw new RuntimeException("Error compressing jpeg.");
    }
    buffer.limit(numBytes);
  }
 else {
    throw new RuntimeException("Unsupported image format.");
  }
  byte[] imageBytes=new byte[buffer.remaining()];
  buffer.get(imageBytes);
  buffer.rewind();
  image.close();
  return imageBytes;
}

private static byte [ ] acquireJpegBytesAndClose ( Image image ) { ByteBuffer buffer ; if ( image . getFormat ( ) == ImageFormat . JPEG ) { Image . Plane plane0 = image . getPlanes ( ) [ 0 ] ; buffer = plane0 . getBuffer ( ) ; } else if ( image . getFormat ( ) == ImageFormat . YUV_420_888 ) { buffer = ByteBuffer . allocateDirect ( image . getWidth ( ) * image . getHeight ( ) * 3 ) ; Log . v ( TAG , "Compressing JPEG with software encoder." ) ; int numBytes = JpegUtilNative . compressJpegFromYUV420Image ( new AndroidImageProxy ( image ) , buffer , JPEG_QUALITY ) ; if ( numBytes < 0 ) { throw new RuntimeException ( "Error compressing jpeg." ) ; } buffer . limit ( numBytes ) ; } else { throw new RuntimeException ( "Unsupported image format." ) ; } byte [ ] imageBytes = new byte [ buffer . remaining ( ) ] ; buffer . get ( imageBytes ) ; buffer . rewind ( ) ; image . close ( ) ; return imageBytes ; }
given an image reader , this extracts the final image . if the image in the reader is jpeg , we extract and return it as is . if the image is yuv , we convert it to jpeg and return the result .
