本文整理汇总了Java中javax.imageio.ImageWriteParam.canWriteCompressed方法的典型用法代码示例。如果您正苦于以下问题:Java ImageWriteParam.canWriteCompressed方法的具体用法?Java ImageWriteParam.canWriteCompressed怎么用?Java ImageWriteParam.canWriteCompressed使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageWriteParam
的用法示例。
在下文中一共展示了ImageWriteParam.canWriteCompressed方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: compressSave
import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
public void compressSave(MultipartFile file, String uploadLocation) throws IOException {
//Compresses JPG file but kills the quality.
BufferedImage image = ImageIO.read(new ByteArrayInputStream(file.getBytes()));
File compressedImageFile = new File(uploadLocation);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if(param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
}
writer.write(null, new IIOImage(image, null, null), param);
}
示例2: main
import javax.imageio.ImageWriteParam; //导入方法依赖的package包/类
public static void main(String[] args) {
Locale.setDefault(Locale.US);
final BufferedImage image
= new BufferedImage(512, 512, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = image.createGraphics();
try {
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.scale(2.0, 2.0);
g2d.setColor(Color.red);
g2d.draw(new Rectangle2D.Float(10, 10, 100, 100));
g2d.setColor(Color.blue);
g2d.fill(new Rectangle2D.Float(12, 12, 98, 98));
g2d.setColor(Color.green);
g2d.setFont(new Font(Font.SERIF, Font.BOLD, 14));
for (int i = 0; i < 15; i++) {
g2d.drawString("Testing Compression ...", 20, 20 + i * 16);
}
final String[] fileSuffixes = ImageIO.getWriterFileSuffixes();
final Set<String> testedWriterClasses = new HashSet<String>();
for (String suffix : fileSuffixes) {
if (!IGNORE_FILE_SUFFIXES.contains(suffix)) {
final Iterator<ImageWriter> itWriters
= ImageIO.getImageWritersBySuffix(suffix);
final ImageWriter writer;
final ImageWriteParam writerParams;
if (itWriters.hasNext()) {
writer = itWriters.next();
if (testedWriterClasses.add(writer.getClass().getName())) {
writerParams = writer.getDefaultWriteParam();
if (writerParams.canWriteCompressed()) {
testCompression(image, writer, writerParams, suffix);
}
}
} else {
throw new RuntimeException("Unable to get writer !");
}
}
}
} catch (IOException ioe) {
throw new RuntimeException("IO failure", ioe);
}
finally {
g2d.dispose();
}
}