用JAVA将大图片文件转换出小的缩略图文件
类别: JAVA教程
用JAVA将大图片文件转换出小的缩略图文件,要求使用JDK1.4,
大家可以将这段程序代码改为JavaBean,以便在WEB环境中使用,
转换出的小缩略图效果不错!
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class imgTest {
public static void main (String argv[]) {
try {
File fi = new File("c:/image2.jpg"); //大图文件
File fo = new File("c:/imgTest.jpg"); //将要转换出的小图文件
int nw = 100;
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy);
System.out.println(w + " " +h);
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
大家可以将这段程序代码改为JavaBean,以便在WEB环境中使用,
转换出的小缩略图效果不错!
import javax.imageio.ImageIO;
import javax.imageio.IIOException;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.io.File;
import java.awt.image.AffineTransformOp;
import java.awt.geom.AffineTransform;
public class imgTest {
public static void main (String argv[]) {
try {
File fi = new File("c:/image2.jpg"); //大图文件
File fo = new File("c:/imgTest.jpg"); //将要转换出的小图文件
int nw = 100;
AffineTransform transform = new AffineTransform();
BufferedImage bis = ImageIO.read(fi);
int w = bis.getWidth();
int h = bis.getHeight();
double scale = (double)w/h;
int nh = (nw*h)/w ;
double sx = (double)nw/w;
double sy = (double)nh/h;
transform.setToScale(sx,sy);
System.out.println(w + " " +h);
AffineTransformOp ato = new AffineTransformOp(transform,null);
BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
ato.filter(bis,bid);
ImageIO.write(bid,"jpeg",fo);
} catch(Exception e) {
e.printStackTrace();
}
}
}
- 上一篇: Java1.5语言新特性简单总结
- 下一篇: sqlserver在JAVA中的应用
-= 资 源 教 程 =-
文 章 搜 索