code: private int compress(BufferedImage bi, float quality) {
  
  		baos.reset();
  
  		final JPEGImageWriteParam params = new JPEGImageWriteParam(null);
  		params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  		params.setCompressionQuality(quality);
  
  		try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos)) {
  			final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
  			writer.setOutput(mcios);
  			writer.write(null, new IIOImage(bi, null, null), params);
  		} catch (IOException e) {
  			throw new IllegalStateException(e);
  		}
  
  		return baos.size();
  	}
docs: Compress {@link BufferedImage} with a given quality into byte array.
  
  @param bi the {@link BufferedImage} to compres into byte array
  @param quality the compressed image quality (1 = 100%, 0.5 = 50%, 0.1 = 10%, etc)
  @return The size of compressed data (number of bytes)