java中如何生成缩略图

来源:互联网 发布:php 明天0点的时间戳 编辑:程序博客网 时间:2024/05/17 08:39

其实这个问题很简单,JDK文档查一下,google一下都可以解决这个问题的. 由此看出我这些学弟学妹 们,要么是懒,要么没有好的习惯.下面帖一段demo程序
package uidemo;
import java.io.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class Test{
  public static void main(String[] args) throws Exception {
    File File = new File("1.jpg");
    BufferedImage src = javax.imageio.ImageIO.read(File); //获取BufferedImage对象
    //得到源图的长度和宽度
    int height = src.getHeight();
    int wideth = src.getWidth();
    BufferedImage tag = new BufferedImage(wideth / 2, height / 2,
                                          BufferedImage.TYPE_INT_RGB);
    tag.getGraphics().drawImage(src, 0, 0, wideth / 2, height / 2, null);
    FileOutputStream out = new FileOutputStream("newfile.jpg");
    //因为测试用的jpg格式的图像,所以这里要采用JPEG编码
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(tag);
    out.close();
  }
}
另外有个开源项目
JMagick
大家可以访问看看,这里提供了更多的精确控制和更多的功能

该文章转载自网络大本营:http://www.xrss.cn/Dev/JAVA/200711317360.Html

原创粉丝点击