生成图片缩略图

/**
 * 
 */
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();
        }
    }
}


转载于:https://my.oschina.net/huewenhua/blog/681586

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值