通过下面的代码可以显示一张图片
Icon logoImage=new ImageIcon(".\\photo\\cb2.jpg");
lblPhoto=new JLabel(logoImage);
不过显示的图片是原来图片的大小,我想在程序中改变图片的大小,
Icon , ImageIcon都没有改变大小的方法,
应该做?
Icon logoImage=new ImageIcon(".\\photo\\cb2.jpg");
lblPhoto=new JLabel(logoImage);
不过显示的图片是原来图片的大小,我想在程序中改变图片的大小,
Icon , ImageIcon都没有改变大小的方法,
应该做?
// 1 读图
BufferedImage image = ImageIO.read("xxx.jpg");// 2 图像长宽缩小一半 double scale = 0.5d;
int width = (int)(image .getWidth() * scale );
int height = (int)(image .getHeight() * scale );
BufferedImage dstImage = new BufferedImage(width, height, srcImage.getType());AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scale , scale);AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform, hints);affineTransformOp.filter(image, dstImage);// 3 实例化 ImageIcon
Icon icon = new ImageIcon(dstImage);
ImageIO.read好象没有String类型.
您看下错误说明嘛:E:\练习题\学生情况管理系统\java\photoDemo.java:29: 找不到符号
符号: 方法 read(java.lang.String)
位置: 类 javax.imageio.ImageIO
BufferedImage image = ImageIO.read(".\\photo\\cb2.jpg");
^
注意: E:\练习题\学生情况管理系统\java\photoDemo.java 使用或覆盖了已过时的 API。
注意: 要了解详细信息,请使用 -Xlint:deprecation 重新编译。 还有,我发现这样也不能弄成100*100固定大小是不是?,,很麻烦的,不行就不管它算了,
大不了就把空间留大点让它显示了.
ImageIO.read(new File("xxx.jpg"));你需要 100*100 大小的缩略图,可以做一个换算。int width = 100(int)(image .getWidth() * scale );
int height = 100(int)(image .getHeight() * scale );double scaleWidth = (double) width / (double) image.getWidth();
double scaleHeight = (double) height / (double) image.getHeight();BufferedImage dstImage = new BufferedImage(width, height, image.getType());AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scaleWidth , scaleHeight );
affineTransformOp.filter(image, dstImage);
int width = 100;
int height = 100;
Graphics类中有一个drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) 可以实现缩放的功能,可以把图片平铺在一个Buffer上面
这是我编译过的程序代码:
import java.awt.*;
import javax.swing.*;
import java.io.IOException;
import java.io.File;
import javax.imageio.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
public class phDemo extends JFrame{
JPanel panel;
JLabel lblPhotoDemo;
public phDemo() {
super("photo demo");
panel=new JPanel();
lblPhotoDemo=new JLabel();
try{
File file=new File( "E:\\练习题\\学生情况管理系统\\java\\photo\\cb1.jpg ");
BufferedImage image = ImageIO.read(file);
int width = 100;
int height = 100;
double scaleWidth = (double) width / (double) image.getWidth();
double scaleHeight = (double) height / (double) image.getHeight();
BufferedImage dstImage = new BufferedImage(width, height, image.getType());
AffineTransform affineTransform = new AffineTransform();
affineTransform.scale(scaleWidth , scaleHeight);
AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,2);//
affineTransformOp.filter(image, dstImage);
Icon logoImage1 = new ImageIcon(dstImage);
lblPhotoDemo=new JLabel(logoImage1); }catch(IOException e){
System.out.println(e.getMessage());
}
panel.add(lblPhotoDemo);
this.add(panel);
this.setSize(600,500);
this.show();
}
public static void main(String[] args){
new phDemo();
}
}