当前位置: 技术问答>java相关
求助:怎么缩放绘制一个Image对象?
来源: 互联网 发布时间:2015-05-20
本文导语: 就是在用Graphics绘画的时候按照一定的比例 将Image对象画出来 //bow | 再给你一个Thumbnail的例子 public class Thumbnail extends ImageIcon { public Thumbnail(ImageIcon originalIcon) { double scale ...
就是在用Graphics绘画的时候按照一定的比例
将Image对象画出来
//bow
将Image对象画出来
//bow
|
再给你一个Thumbnail的例子
public class Thumbnail extends ImageIcon {
public Thumbnail(ImageIcon originalIcon) {
double scale = 0.5; // 50 %
int w = originalIcon.getWidth() * scale;
int h = originalIcon.getHeight() * scale;
BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
AffineTransform trans = new AffineTransform();
trans.scale(scale, scale);
Graphics2D g = outImage.createGraphics();
g.drawImage(originalIcon.getImage(), trans, null);
g.dispose();
setImage(outImage)
}
}
一般步骤如下:
1. Load the image form a file (or byte array) using java.awt.Toolkit.createImage
2. Create a new BufferedImage with the desired size
3. Get a Graphics object from the BufferedImage
4. Draw (and scale) your Image on to the Graphics object either with a method that scales your Image for you, or after calling Image.getScaledImage
5. Use a com.sun.image.codec.jpeg.JPEGImageEncoder to turn your BufferedImage into a JPEG file
public class Thumbnail extends ImageIcon {
public Thumbnail(ImageIcon originalIcon) {
double scale = 0.5; // 50 %
int w = originalIcon.getWidth() * scale;
int h = originalIcon.getHeight() * scale;
BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
AffineTransform trans = new AffineTransform();
trans.scale(scale, scale);
Graphics2D g = outImage.createGraphics();
g.drawImage(originalIcon.getImage(), trans, null);
g.dispose();
setImage(outImage)
}
}
一般步骤如下:
1. Load the image form a file (or byte array) using java.awt.Toolkit.createImage
2. Create a new BufferedImage with the desired size
3. Get a Graphics object from the BufferedImage
4. Draw (and scale) your Image on to the Graphics object either with a method that scales your Image for you, or after calling Image.getScaledImage
5. Use a com.sun.image.codec.jpeg.JPEGImageEncoder to turn your BufferedImage into a JPEG file
|
javasun的tutorial中有关于图形图像的.
http://java.sun.com/docs/books/tutorial/2d/images/filtering.html
http://java.sun.com/docs/books/tutorial/2d/images/filtering.html