views:

195

answers:

1

We have some code kicking around that uses this old internal Sun package for manipulating images, essentially encoding JPEGs to a specific size and quality after reading in / decoding from an inputstream. Code examples below. I would appreciate a best practice replacement example using proper java.* or javax.* APIs.

private void encodeJPEG(BufferedImage bi, BufferedOutputStream out, float quality) throws
  ImageFormatException, IOException {
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
 param.setQuality(quality, false);
 param.setDensityUnit(DENSITY_UNIT);
 param.setXDensity(DENSITY);
 param.setYDensity(DENSITY);
 encoder.setJPEGEncodeParam(param);
 encoder.encode(bi);
}


private void initJPEG(File jpegFile) throws FileNotFoundException, IOException,
  IOException {
FileInputStream inputStream = new FileInputStream(jpegFile);
try {
  JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(inputStream);
  buffImage = decoder.decodeAsBufferedImage();
}
.....error handling.....

}

+2  A: 

I haven't tried this approach, but you can take a look the Java Image I/O API. The JPEGImageWriteParam class would be of particular interest.

Vineet Reynolds