图片压缩

<pre name="code" class="html">package cn.zovon.utils;

import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ZipImageUtils {
	
	/*按照宽高进行压缩*/
	public static String zipImage(File f,int w,int h,HttpServletRequest request) throws Exception{
		Image image = ImageIO.read(f); // 读取文件
		int height = image.getHeight(null); // 获取图片的高度
		int width = image.getWidth(null); // 获取图片的宽度
		/*if(width / height > w / h) {  
			int h1 = (int) (height * w / width);  
	    }else{  
	    	int w1 = (int) (width * h / height);  
	    } */
		BufferedImage bi = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); 
		bi.getGraphics().drawImage(image, 0, 0, w, h, null);
		long time = System.currentTimeMillis();
		String realPath = request.getSession().getServletContext().getRealPath("/resource/photo/")+"/"+time+".jpg";
		String savePath = "/resource/photo/"+time+".jpg";
		File destFile = new File(realPath);  
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode((BufferedImage) image); // JPEG编码 
        f.delete();
        out.close();
		return savePath;
	}

	/*缩略图的生成*/
	public static String zipImageFile(File f,int maxLength,HttpServletRequest request) throws Exception{    
		Image image = ImageIO.read(f); // 读取文件
		int height = image.getHeight(null); // 获取图片的高度
		int width = image.getWidth(null); // 获取图片的宽度
		int new_w = 0;  
		int new_h = 0;  
		if (width > height) { 
			 new_w = maxLength; 
             new_h = (int) Math.round(height * ((float) maxLength / width));  
		}else{
			 new_w = (int) Math.round(width * ((float) maxLength / height)); 
             new_h = maxLength; 
		}
        /**宽,高设定 */    
    	BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB);        
        tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null);    
        /** 压缩之后临时存放位置 */    
        File destFile = new File("C:\\temp\\456.jpg");  
        FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode((BufferedImage) tag); // JPEG编码 
        f.delete();
        out.close(); 
        return "";    
    }
}

package cn.zovon.utils;import java.awt.Image;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletRequest;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class ZipImageUtils {/*按照宽高进行压缩*/public static String zipImage(File f,int w,int h,HttpServletRequest request) throws Exception{Image image = ImageIO.read(f); // 读取文件int height = image.getHeight(null); // 获取图片的高度int width = image.getWidth(null); // 获取图片的宽度/*if(width / height > w / h) { int h1 = (int) (height * w / width); }else{ int w1 = (int) (width * h / height); } */BufferedImage bi = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB ); bi.getGraphics().drawImage(image, 0, 0, w, h, null);long time = System.currentTimeMillis();String realPath = request.getSession().getServletContext().getRealPath("/resource/photo/")+"/"+time+".jpg";String savePath = "/resource/photo/"+time+".jpg";File destFile = new File(realPath); FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode((BufferedImage) image); // JPEG编码 f.delete(); out.close();return savePath;}/*缩略图的生成*/public static String zipImageFile(File f,int maxLength,HttpServletRequest request) throws Exception{ Image image = ImageIO.read(f); // 读取文件int height = image.getHeight(null); // 获取图片的高度int width = image.getWidth(null); // 获取图片的宽度int new_w = 0; int new_h = 0; if (width > height) { new_w = maxLength; new_h = (int) Math.round(height * ((float) maxLength / width)); }else{ new_w = (int) Math.round(width * ((float) maxLength / height)); new_h = maxLength; } /**宽,高设定 */ BufferedImage tag = new BufferedImage(new_w, new_h,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(image, 0, 0, new_w, new_h, null); /** 压缩之后临时存放位置 */ File destFile = new File("C:\\temp\\456.jpg"); FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode((BufferedImage) tag); // JPEG编码 f.delete(); out.close(); return ""; }}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值