Tags

,

public static void saveOldWay(String ref, BufferedImage img) {
BufferedOutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(ref));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(img);
int quality = 5;
quality = Math.max(0, Math.min(quality, 100));
param.setQuality((float) quality / 100.0f, false);
encoder.setJPEGEncodeParam(param);
encoder.encode(img);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Saves a BufferedImage to the given file, pathname must not have any
* periods “.” in it except for the one before the format, i.e. C:/images/fooimage.png
* @param img
* @param saveFile
*/
public static void saveImage(BufferedImage img, String ref) {
try {
String format = (ref.endsWith(“.png”)) ? “png” : “jpg”;
ImageIO.write(img, format, new File(ref));
} catch (IOException e) {
e.printStackTrace();
}
}

Reference: http://www.javalobby.org/articles/ultimate-image/#5

Advertisements