10-04
07
java生成jpeg/png图片时,设置图片质量
作者:Java伴侣 日期:2010-04-07
复制内容到剪贴板 程序代码
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
public class ImageIOTest {
public static void main(String[] args) throws IOException {
int width = 800;
int height = 800;
BufferedImage image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Random random = new Random();
for (int i = 0; i < 500; i++) {
g2d.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
g2d.setStroke(new BasicStroke(random.nextFloat() * 5));
int r = random.nextInt(width / 2);
g2d.drawArc(random.nextInt(width), random.nextInt(height),
r, r, 0, 360);
}
g2d.dispose();
ImageWriter writer = null;
ImageTypeSpecifier type =
ImageTypeSpecifier.createFromRenderedImage(image);
Iterator iter = ImageIO.getImageWriters(type, "jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}
if (writer == null) {
return;
}
IIOImage iioImage = new IIOImage(image, null, null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
for (int i = 0; i < 10; i++) {
param.setCompressionQuality((float) ((i+1) / 10.0));
ImageOutputStream outputStream =
ImageIO.createImageOutputStream(new File("C:/test" + (i+1) + ".jpg"));
writer.setOutput(outputStream);
writer.write(null, iioImage, param);
}
}
}
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
public class ImageIOTest {
public static void main(String[] args) throws IOException {
int width = 800;
int height = 800;
BufferedImage image = new BufferedImage(
width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, width, height);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Random random = new Random();
for (int i = 0; i < 500; i++) {
g2d.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
g2d.setStroke(new BasicStroke(random.nextFloat() * 5));
int r = random.nextInt(width / 2);
g2d.drawArc(random.nextInt(width), random.nextInt(height),
r, r, 0, 360);
}
g2d.dispose();
ImageWriter writer = null;
ImageTypeSpecifier type =
ImageTypeSpecifier.createFromRenderedImage(image);
Iterator iter = ImageIO.getImageWriters(type, "jpg");
if (iter.hasNext()) {
writer = (ImageWriter)iter.next();
}
if (writer == null) {
return;
}
IIOImage iioImage = new IIOImage(image, null, null);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
for (int i = 0; i < 10; i++) {
param.setCompressionQuality((float) ((i+1) / 10.0));
ImageOutputStream outputStream =
ImageIO.createImageOutputStream(new File("C:/test" + (i+1) + ".jpg"));
writer.setOutput(outputStream);
writer.write(null, iioImage, param);
}
}
}
主要是通过
javax.imageio.plugins.jpeg.JPEGImageWriteParam
public void setCompressionQuality(float quality)
来设置图片质量。
评论: 0 | 引用: 0 | 查看次数: 1220
发表评论