工具类:图片压缩处理
2015-10-26 00:00
267 查看
摘要: 图片压缩处理
package com.hengshan.yundonggo.util;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.*;
/**
* 图片压缩处理
*
* @author dgy
*
*/
public class ImgCompress {
private Image img;
private int width;
private int height;
public static String startCompress(File photo, String username, String imgname) {
String src = null;
try {
ImgCompress imgCom = new ImgCompress(photo);
src = imgCom.resizeFix(400, 400, username, imgname);
} catch (Exception e) {
e.printStackTrace();
}
return src;
}
/**
* 构造函数
*/
public ImgCompress(File photo) throws IOException {
img = ImageIO.read(photo); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 按照宽度还是高度进行压缩
*
* @param w
* int 最大宽度
* @param h
* int 最大高度
*/
public String resizeFix(int w, int h, String username, String imgname) throws IOException {
String src;
if (width / height > w / h) {
src = resizeByWidth(w, username, imgname);
} else {
src = resizeByHeight(h, username, imgname);
}
return src;
}
/**
* 以宽度为基准,等比例放缩图片
*
* @param w
* int 新宽度
*/
public String resizeByWidth(int w, String username, String imgname) throws IOException {
int h = (int) (height * w / width);
String src = resize(w, h, username, imgname);
return src;
}
/**
* 以高度为基准,等比例缩放图片
*
* @param h
* int 新高度
*/
public String resizeByHeight(int h, String username, String imgname) throws IOException {
int w = (int) (width * h / height);
String src = resize(w, h, username, imgname);
return src;
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w
* int 新宽度
* @param h
* int 新高度
*/
@SuppressWarnings("restriction")
public String resize(int w, int h, String username, String imgname) throws IOException {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的优先级比速度高生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
// 保存到服务器
String path1 = "E:" + File.separator + "img" + File.separator + username;
File photo = new File(path1);
if (!photo.exists() && !photo.isDirectory()) {
photo.mkdirs();
}
String path = path1 + File.separator + imgname + ".jpg";
File destFile = new File(path);
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
return username + "/" + imgname + ".jpg";
}
public static void main(String[] args) {
File file = new File("D://hy.jpg");
String src = ImgCompress.startCompress(file, "suoluetu", file.getName());
System.out.println(src);
}
}
package com.hengshan.yundonggo.util;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.*;
/**
* 图片压缩处理
*
* @author dgy
*
*/
public class ImgCompress {
private Image img;
private int width;
private int height;
public static String startCompress(File photo, String username, String imgname) {
String src = null;
try {
ImgCompress imgCom = new ImgCompress(photo);
src = imgCom.resizeFix(400, 400, username, imgname);
} catch (Exception e) {
e.printStackTrace();
}
return src;
}
/**
* 构造函数
*/
public ImgCompress(File photo) throws IOException {
img = ImageIO.read(photo); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 按照宽度还是高度进行压缩
*
* @param w
* int 最大宽度
* @param h
* int 最大高度
*/
public String resizeFix(int w, int h, String username, String imgname) throws IOException {
String src;
if (width / height > w / h) {
src = resizeByWidth(w, username, imgname);
} else {
src = resizeByHeight(h, username, imgname);
}
return src;
}
/**
* 以宽度为基准,等比例放缩图片
*
* @param w
* int 新宽度
*/
public String resizeByWidth(int w, String username, String imgname) throws IOException {
int h = (int) (height * w / width);
String src = resize(w, h, username, imgname);
return src;
}
/**
* 以高度为基准,等比例缩放图片
*
* @param h
* int 新高度
*/
public String resizeByHeight(int h, String username, String imgname) throws IOException {
int w = (int) (width * h / height);
String src = resize(w, h, username, imgname);
return src;
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w
* int 新宽度
* @param h
* int 新高度
*/
@SuppressWarnings("restriction")
public String resize(int w, int h, String username, String imgname) throws IOException {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的优先级比速度高生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
// 保存到服务器
String path1 = "E:" + File.separator + "img" + File.separator + username;
File photo = new File(path1);
if (!photo.exists() && !photo.isDirectory()) {
photo.mkdirs();
}
String path = path1 + File.separator + imgname + ".jpg";
File destFile = new File(path);
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
return username + "/" + imgname + ".jpg";
}
public static void main(String[] args) {
File file = new File("D://hy.jpg");
String src = ImgCompress.startCompress(file, "suoluetu", file.getName());
System.out.println(src);
}
}
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- 处理驱动器和文件夹
- upload上传单张图片
- 图片引发的溢出危机(图)
- 计算机信息处理
- 处理 SSI 文件时出错的解决方法
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#使用DeflateStream解压缩数据文件的方法
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- C#调用WinRar执行rar、zip压缩的方法
- perl 中文处理技巧