java生成生成图片缩略图

/**
 *
 */
package com.fkhwl.fkhserver.core.utils;
 
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
 
import javax.imageio.ImageIO;
 
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
 
/**
 * @ClassName: ThumbnailTools
 * @Description: 缩略图生成工具
 * @author
 * @date 2014年9月25日 下午5:18:33 
 */
public class ThumbnailTools {
 
    private int fileSize;
    private String inPath; // 输入图路径
    private String outPath; // 输出图路径
    private int width = 100; // 默认输出图片宽
    private int height = 100; // 默认输出图片高
    private String inFileName; // 输入图文件名
    private String outFileName; // 输出图文件名
    private boolean proportion = true; // 是否等比缩放标记(默认为等比缩放)
    private String prefix = "thumbnail_";
 
    public int getWidth() {
        return width;
    }
 
    public void setWidth(int width) {
        this.width = width;
    }
 
    public int getHeight() {
        return height;
    }
 
    public void setHeight(int height) {
        this.height = height;
    }
     
    public ThumbnailTools setSize(int size){
        this.width = size;
        this.height = size;
        return this;
    }
     
    public ThumbnailTools setSize(int width, int height){
        this.width = width;
        this.height = height;
        return this;
    }
     
    public String getInPath() {
        return inPath;
    }
 
    public void setInPath(String inPath) {
        this.inPath = inPath;
    }
 
    public String getOutPath() {
        return outPath;
    }
 
    public void setOutPath(String outPath) {
        this.outPath = outPath;
    }
 
    public boolean isProportion() {
        return proportion;
    }
 
    public void setProportion(boolean proportion) {
        this.proportion = proportion;
    }
     
    public ThumbnailTools(String path){
        this.inPath = path;
        this.outPath = path;
    }
 
    public ThumbnailTools(String inPath, String outPath){
        this.inPath = inPath;
        this.outPath = outPath;
    }
     
    /**
     * 生成缩略图
     * @param fileName 文件名
     * @return boolean
     */
    public boolean generate(String fileName) throws Exception{
        this.generate(fileName, null);
        return Boolean.TRUE;
    }
 
    /**
     * 生成缩略图
     * @param fileName 文件名
     * @param outFileName 输出文件名
     * @return boolean
     */
    public boolean generate(String fileName, String outFileName) throws Exception{
        File file = new File(inPath+fileName);
        this.inFileName = fileName;
        this.outFileName = null == outFileName ? prefix+inFileName : outFileName;
        this.execute(new FileInputStream(file));
        return Boolean.TRUE;
    }
     
    public boolean generate(InputStream inputStream, String outPath) throws Exception{
        this.execute(inputStream);
         
        return Boolean.TRUE;
    }
     
    private void execute(InputStream inputStream) throws Exception {
        this.fileSize = inputStream.available();
        BufferedImage oldImage = ImageIO.read(inputStream);
        if (oldImage.getWidth() == -1) {
            System.out.println("Input image cant't read or format error.");
            return;
        }
         
        int newWidth;
        int newHeight;
        // 是否是等比缩放
        if (this.proportion == true) {
            // 为等比缩放计算输出的图片宽度及高度
            double rate1 = ((double) oldImage.getWidth()) / (double) width;
            double rate2 = ((double) oldImage.getHeight()) / (double) height;
            // 根据缩放比率大的进行缩放控制
            double rate = rate1 > rate2 ? rate1 : rate2;
            newWidth = (int) (((double) oldImage.getWidth()) / rate);
            newHeight = (int) (((double) oldImage.getHeight()) / rate);
        } else {
            newWidth = width; // 输出的图片宽度
            newHeight = height; // 输出的图片高度
        }
        BufferedImage tag = new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);
 
        // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
        tag.getGraphics().drawImage(oldImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
        String newFilePath = outPath+outFileName;
        File newFile = new File(newFilePath);
        FileOutputStream out = new FileOutputStream(newFile);
        // JPEGImageEncoder可适用于其他图片类型的转换
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(tag);
        inputStream.close();
        out.close();
         
        System.out.println(newFilePath+" ok, size: "+(fileSize/1024)+"kb to "+(newFile.length()/1024)+"kb");
    }
     
    public static void main(String[] args) {
        try {
            new ThumbnailTools("D:/").setSize(300, 200).generate("1.jpg", "2.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

大家在运行的时候或许会报错,因为该程序需要java两个jar包的支持

在Eclipse中处理图片,需要引入两个包:
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
报错:
Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar

 

此时解决办法:
Eclipse默认把这些受访问限制的API设成了ERROR。只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。

posted on 2014-12-10 08:53  java界的奥特曼  阅读(339)  评论(0编辑  收藏  举报