I am using java and the following code. Is there a way to use RenderingHints to accomplish this?
try {
sourceImage = ImageIO.read(new File("images.jpg"));
BufferedImage dstImage = null;开发者_如何学C
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(colorSpace, null);
dstImage = op.filter(sourceImage, null);
ImageIO.write(dstImage, "jpeg", new File("output.jpg"));
System.out.println("processing complete");
} catch (IOException e) {
e.printStackTrace();
}
I got this working using ImageWriteParam.setCompressionQuality
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.5f); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File("output.jpg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(dstImage, null, null);
writer.write(null, image, iwp);
writer.dispose();
精彩评论