等比例缩略图不失真

用到imagescaling这个包


import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mortennobel.imagescaling.ResampleOp;
  
/** 
 * 图片缩放工具类 
 * @author LzwGLory
 */  
public class MyImage {  
	protected final transient static Log log = LogFactory.getLog(MyImage.class);
	
	/**
	 * 用于获得等比例缩略图
	 * @param fromFileStr 源文件
	 * @param saveToFileStr 要保存的文件
	 * @param suffix 后缀名
	 * @param width 图片的宽度
	 * @param height 图片的高度
	 * 
	 * */
	
	public static boolean getPicThumb(String fromFileStr, String saveToFileStr,String suffix,int width,int height){
  	  File inputFile = new File(fromFileStr);  
        File outFile = new File(saveToFileStr);  
        String outPath = outFile.getAbsolutePath();  
        MyImage myImage = new MyImage();  
        InputStream input;
		try {
			input = new FileInputStream(inputFile);
			byte[] byteArrayImage=myImage.readBytesFromIS(input);  
	        input.read(byteArrayImage);  
	        return  myImage.resizeImage(byteArrayImage, outPath, width, height, suffix);  
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
			log.info("缩略图创建失败!"+e.getMessage());
			return false;
		}  
  }
	
	
    /** 
     * 接收输入流输生成图片 
     * @param input 
     * @param writePath 
     * @param width 
     * @param height 
     * @param format 
     * @return 
     */  
    public boolean resizeImage(InputStream input, String writePath,  
            Integer width, Integer height, String format) {  
        try {  
            BufferedImage inputBufImage = ImageIO.read(input);  
            log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());  
            ResampleOp resampleOp = new ResampleOp(width, height);// 转换  
            BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,  
                    null);  
            ImageIO.write(rescaledTomato, format, new File(writePath));  
            log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());  
            return true;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return false;  
        }  
  
    }  
  
    /** 
     * 接收File输出图片 
     * @param file 
     * @param writePath 
     * @param width 
     * @param height 
     * @param format 
     * @return 
     */  
    public boolean resizeImage(File file, String writePath, Integer width,  
            Integer height, String format) {  
        try {  
            BufferedImage inputBufImage = ImageIO.read(file);  
            inputBufImage.getType();  
            log.info("转前图片高度和宽度:" + inputBufImage.getHeight() + ":"+ inputBufImage.getWidth());  
            ResampleOp resampleOp = new ResampleOp(width, height);// 转换  
            BufferedImage rescaledTomato = resampleOp.filter(inputBufImage,  
                    null);  
            ImageIO.write(rescaledTomato, format, new File(writePath));  
            log.info("转后图片高度和宽度:" + rescaledTomato.getHeight() + ":"+ rescaledTomato.getWidth());  
            return true;  
        } catch (IOException e) {  
            e.printStackTrace();  
            return false;  
        }  
  
    }  
  
    /** 
     * 接收字节数组生成图片 
     * @param RGBS 
     * @param writePath 
     * @param width 
     * @param height 
     * @param format 
     * @return 
     */  
    public boolean resizeImage(byte[] RGBS, String writePath, Integer width,  
            Integer height, String format) {  
        InputStream input = new ByteArrayInputStream(RGBS);  
        return this.resizeImage(input, writePath, width, height, format);  
    }  
  
    public byte[] readBytesFromIS(InputStream is) throws IOException {  
        int total = is.available();  
        byte[] bs = new byte[total];  
        is.read(bs);  
        return bs;  
    }  
   
    
    
    
    //测试:只测试了字节流的方式,其它的相对简单,没有一一测试  
    public static void main(String[] args) throws IOException {  
    	
        int width = 800;  
        int height = 600;  
        String fromFileStr="D:\\apache-tomcat-7.0.52\\webapps\\gc_net\\uploads\\rollingPic\\20140325\\32341395672128725.jpg";
        String saveToFileStr="D:\\apache-tomcat-7.0.52\\webapps\\gc_net\\uploads\\rollingPic\\20140325\\to2.jpg";
        String suffix="jpg";
        MyImage.getPicThumb(fromFileStr, saveToFileStr, suffix, width, height);
    }  
}  


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值