<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 ""; }}