private byte[] acquireJpegBytes(Image img,int degrees){
  ByteBuffer buffer;
  if (img.getFormat() == ImageFormat.JPEG) {
    Image.Plane plane0=img.getPlanes()[0];
    buffer=plane0.getBuffer();
    byte[] imageBytes=new byte[buffer.remaining()];
    buffer.get(imageBytes);
    buffer.rewind();
    return imageBytes;
  }
 else   if (img.getFormat() == ImageFormat.YUV_420_888) {
    buffer=mJpegByteBufferPool.acquire();
    if (buffer == null) {
      buffer=ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * 3);
    }
    int numBytes=JpegUtilNative.compressJpegFromYUV420Image(new AndroidImageProxy(img),buffer,JPEG_QUALITY,degrees);
    if (numBytes < 0) {
      throw new RuntimeException("Error compressing jpeg.");
    }
    buffer.limit(numBytes);
    byte[] imageBytes=new byte[buffer.remaining()];
    buffer.get(imageBytes);
    buffer.clear();
    mJpegByteBufferPool.release(buffer);
    return imageBytes;
  }
 else {
    throw new RuntimeException("Unsupported image format.");
  }
}

private byte [ ] acquireJpegBytes ( Image img , int degrees ) { ByteBuffer buffer ; if ( img . getFormat ( ) == ImageFormat . JPEG ) { Image . Plane plane0 = img . getPlanes ( ) [ 0 ] ; buffer = plane0 . getBuffer ( ) ; byte [ ] imageBytes = new byte [ buffer . remaining ( ) ] ; buffer . get ( imageBytes ) ; buffer . rewind ( ) ; return imageBytes ; } else if ( img . getFormat ( ) == ImageFormat . YUV_420_888 ) { buffer = mJpegByteBufferPool . acquire ( ) ; if ( buffer == null ) { buffer = ByteBuffer . allocateDirect ( img . getWidth ( ) * img . getHeight ( ) * 3 ) ; } int numBytes = JpegUtilNative . compressJpegFromYUV420Image ( new AndroidImageProxy ( img ) , buffer , JPEG_QUALITY , degrees ) ; if ( numBytes < 0 ) { throw new RuntimeException ( "Error compressing jpeg." ) ; } buffer . limit ( numBytes ) ; byte [ ] imageBytes = new byte [ buffer . remaining ( ) ] ; buffer . get ( imageBytes ) ; buffer . clear ( ) ; mJpegByteBufferPool . release ( buffer ) ; return imageBytes ; } else { throw new RuntimeException ( "Unsupported image format." ) ; } }
given an image reader , extracts the jpeg image bytes and then closes the reader .
