public static byte[] encodeJPEG(BufferedImage image,float quality) throws IOException {
  ByteArrayOutputStream out=new ByteArrayOutputStream();
  ImageOutputStream iosout=new MemoryCacheImageOutputStream(out);
  if (Debug.debugging("jpeghelper")) {
    Debug.output("Got output stream..." + out);
  }
  Iterator<ImageWriter> iter=ImageIO.getImageWritersByFormatName("jpeg");
  ImageWriter writer=iter.next();
  ImageWriteParam iwp=writer.getDefaultWriteParam();
  iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
  iwp.setCompressionQuality(quality);
  if (Debug.debugging("jpeghelper")) {
    Debug.output("Got encode params...");
  }
  writer.setOutput(iosout);
  if (Debug.debugging("jpeghelper")) {
    Debug.output("Got jpeg encoder...");
  }
  IIOImage iioi=new IIOImage(image,null,null);
  writer.write(null,iioi,iwp);
  writer.dispose();
  if (Debug.debugging("jpeghelper")) {
    Debug.output("encoded?");
  }
  return out.toByteArray();
}

public static byte [ ] encodeJPEG ( BufferedImage image , float quality ) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ; ImageOutputStream iosout = new MemoryCacheImageOutputStream ( out ) ; if ( Debug . debugging ( "jpeghelper" ) ) { Debug . output ( "Got output stream..." + out ) ; } Iterator < ImageWriter > iter = ImageIO . getImageWritersByFormatName ( "jpeg" ) ; ImageWriter writer = iter . next ( ) ; ImageWriteParam iwp = writer . getDefaultWriteParam ( ) ; iwp . setCompressionMode ( ImageWriteParam . MODE_EXPLICIT ) ; iwp . setCompressionQuality ( quality ) ; if ( Debug . debugging ( "jpeghelper" ) ) { Debug . output ( "Got encode params..." ) ; } writer . setOutput ( iosout ) ; if ( Debug . debugging ( "jpeghelper" ) ) { Debug . output ( "Got jpeg encoder..." ) ; } IIOImage iioi = new IIOImage ( image , null , null ) ; writer . write ( null , iioi , iwp ) ; writer . dispose ( ) ; if ( Debug . debugging ( "jpeghelper" ) ) { Debug . output ( "encoded?" ) ; } return out . toByteArray ( ) ; }
return a byte array that contains the jpeg encoded image .
