如何将一个较大的图像加载到一个面积较小的组件上,就是希望图像能按比例缩小。
解决方案 »
- 求助!java解压.z文件
- 测试你未来的另一半,超准的
- 如何用java实现这个算法,200分,急
- 求 MapXtreme for java 4.5下载地址,找了好久都没找到,谢谢了!
- 为什么用FileWriter,BufferedWriter把jpg文件写入另一jpg文件失败呢?
- 请问怎样将一个*.DLL文件,翻译成源码,谢谢/
- 奇怪的问题,我的jdk1.2.2,用javac命令可以编译成class文件,但使用java运行时却说找不到类文件
- 急问题:一个关于类继承的概念问题,它的执行顺序百思不得其解
- 如何学好JBuileder,从JAVA看起还是直接来?能否推荐一本好的JBuilder书?这厢有礼了。
- http请求大文件
- 一个循环问题,怎么样都跳不出!!
- 请教怎么才能让字体变为红色?
你能再说详细点吗,举个例子怎么弄,万分感谢。
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;public class LoadImageTest
{
public static void main(String[] args) throws IOException
{
Image image = loadImage(new File("C:/test.jpg"), 1.2);
ImageIcon icon = new ImageIcon(image); JLabel label = new JLabel(icon);
JFrame f = new JFrame();
f.getContentPane().add(label, BorderLayout.CENTER);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); } private static Image loadImage(File imgFile, double scale) throws IOException
{
BufferedImage image = ImageIO.read(imgFile);
RenderingHints renderingHints = new RenderingHints(
RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); AffineTransformOp scaleOp = new AffineTransformOp(
AffineTransform.getScaleInstance(scale, scale), renderingHints);
BufferedImage targetImage = new BufferedImage(
(int)(image.getWidth() * scale),
(int)(image.getHeight() * scale), image.getType());
scaleOp.filter(image, targetImage);
return targetImage;
}
}
int height,
int hints)
Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely.
If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.
Parameters:
width - the width to which to scale the image.
height - the height to which to scale the image.
hints - flags to indicate the type of algorithm to use for image resampling.
Returns:
a scaled version of the image.