I'm using javax.imageio.ImageIO
to save a BufferedImage
as a jpeg file.
In particular, I created the following Java function:
public static void getScreenShot(BufferedImage capture, Path folder, String filename) {
try {
ImageIO.write(capture, "jpeg", new File(folder.toString()+"/"+filename+".jpg"));
} catch (AWTException | IOException ex) {
Logger.getLogger(ScreenShotMaker.class.getName()).log(Level.SEVERE, null, ex);
}
}
Likewise any image manipulation software, I wish to change the compression level of the jpeg file. However, I'm searching for this option that seems to be missing in ImageIO
.
Can I set the compression level and how?
You have to use JPEGImageWriteParam
and then save the image with ImageWriter.write()
. Before to write, set the output via ImageWriter.setOutput
.
Set the compression level as follows:
JPEGImageWriteParam jpegParams = new JPEGImageWriteParam(null);
jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpegParams.setCompressionQuality(1f);
Where 1f
is a float number that stands for 100% quality. Default value is around 70% if I don't remember wrong.
EDIT
Then, you have to do as follows to get an instance of an ImageWriter
. There are two ways, a short and a long one (I keep both, just in case).
The short way (suggested by lapo in one comment) is:
final ImageWriter writer = ImageIO.getImageWritersByFormatName("jpg").next();
// specifies where the jpg image has to be written
writer.setOutput(new FileImageOutputStream(
new File(folder.toString() + "/" + filename + ".jpg")));
// writes the file with given compression level
// from your JPEGImageWriteParam instance
writer.write(null, new IIOImage(capture, null, null), jpegParams);
or longer way
// use IIORegistry to get the available services
IIORegistry registry = IIORegistry.getDefaultInstance();
// return an iterator for the available ImageWriterSpi for jpeg images
Iterator<ImageWriterSpi> services = registry.getServiceProviders(ImageWriterSpi.class,
new ServiceRegistry.Filter() {
@Override
public boolean filter(Object provider) {
if (!(provider instanceof ImageWriterSpi)) return false;
ImageWriterSpi writerSPI = (ImageWriterSpi) provider;
String[] formatNames = writerSPI.getFormatNames();
for (int i = 0; i < formatNames.length; i++) {
if (formatNames[i].equalsIgnoreCase("JPEG")) {
return true;
}
}
return false;
}
},
true);
//...assuming that servies.hasNext() == true, I get the first available service.
ImageWriterSpi writerSpi = services.next();
ImageWriter writer = writerSpi.createWriterInstance();
// specifies where the jpg image has to be written
writer.setOutput(new FileImageOutputStream(
new File(folder.toString() + "/" + filename + ".jpg")));
// writes the file with given compression level
// from your JPEGImageWriteParam instance
writer.write(null, new IIOImage(capture, null, null), jpegParams);
A more succinct way is to get the ImageWriter
directly from ImageIO
:
ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality(0.7f);
ImageOutputStream outputStream = createOutputStream(); // For example implementations see below
jpgWriter.setOutput(outputStream);
IIOImage outputImage = new IIOImage(image, null, null);
jpgWriter.write(null, outputImage, jpgWriteParam);
jpgWriter.dispose();
The call to ImageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
is needed in order to explicitly set the compression's level (quality).
In ImageWriteParam.setCompressionQuality()
1.0f
is maximum quality, minimum compression, while 0.0f
is minimum quality, maximum compression.
ImageWriter.setOutput
should be passed an ImageOutputStream
. While the method accepts Object
, according to documentation it's usually not supported:
Use of a general
Object
other than anImageOutputStream
is intended for writers that interact directly with an output device or imaging protocol. The set of legal classes is advertised by the writer's service provider'sgetOutputTypes
method; most writers will return a single-element array containing onlyImageOutputStream.class
to indicate that they accept only anImageOutputStream
.
Most cases should be handled by these two classes:
FileImageOutputStream
- an implementation ofImageOutputStream
that writes its output directly to aFile
orRandomAccessFile
.MemoryCacheImageOutputStream
- an implementation ofImageOutputStream
that writes its output to a regularOutputStream
. Usually used withByteArrayOutputStream
(thanks for the tip, @lmiguelmh!).
来源:https://stackoverflow.com/questions/17108234/setting-jpg-compression-level-with-imageio-in-java