图片压缩

1,调用java API按等比例缩放图片

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class ImgCompress {
private BufferedImage img;
private int width;
private int height;
private String path;
private String name;

@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
System.out.println("开始:" + new Date().toLocaleString());
ImgCompress imgCom = new ImgCompress("C:\Users\fly\Desktop\Picture\QQ.png");
imgCom.resizeFix(1000, 1000);
System.out.println("结束:" + new Date().toLocaleString());
}

/**
* 图片压缩
* @param file
* @throws IOException
*/
public static void drawImage(File file, int proportion) throws IOException {
ImgCompress imgCom = new ImgCompress(file);
imgCom.resizeFix(proportion, proportion);
}

/**
* 图片压缩
* @param path
* @throws IOException
*/
public static void drawImage(String path, String name) throws IOException {
ImgCompress imgCom = new ImgCompress(path, name);
imgCom.resizeFix(1000, 1000);
}

/**
* 构造函数
*/
public ImgCompress(String path, String name) throws IOException {
File file = new File(path, name);// 读入文件
this.path = path;
this.name = name;
img = ImageIO.read(file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}

/**
* 构造函数
*/
public ImgCompress(String fileName) throws IOException {
File file = new File(fileName);// 读入文件
img = ImageIO.read(file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}

/**
* 构造函数
*/
public ImgCompress(File file) throws IOException {
this.path = file.getPath();
img = ImageIO.read(file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 按照宽度还是高度进行压缩
* @param w int 最大宽度
* @param h int 最大高度
*/
public void resizeFix(int w, int h) throws IOException {
if (new Double(width) / height > new Double(w) / h) {
resizeByWidth(w);
} else {
resizeByHeight(h);
}
}
/**
* 以宽度为基准,等比例放缩图片
* @param w int 新宽度
*/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height * w / width);
resize(w, h);
}
/**
* 以高度为基准,等比例缩放图片
* @param h int 新高度
*/
public void resizeByHeight(int h) throws IOException {
int w = (int) (width * h / height);
resize(w, h);
}
/**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
*/
public void resize(int w, int h) throws IOException {
// SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
BufferedImage image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB );
image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
File destFile = null;
if(StringUtils.isBlank(name)){
destFile= new File(path);
}else{
destFile = new File(path, name);
}
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
// 可以正常实现bmp、png、gif转jpg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image); // JPEG编码
out.close();
}
}

2,调用google提供Thumbnails.jar图片处理API
import net.coobird.thumbnailator.Thumbnails;
public class ThumPic {

public static void main(String[] args) throws IOException{
System.out.println("开始:" + new Date().getTime());
File fromPic = new File("C:\Users\fly\Desktop\img\2.jpg");
File toPic = new File("C:\Users\fly\Desktop\img_1\2_1.jpg");
File waterPic = new File("C:\Users\fly\Desktop\img\logo.png");

//按指定大小把图片进行缩和放(会遵循原图高宽比例)
//此处把图片压成400×500的缩略图
Thumbnails.of(fromPic).size(1500,1500).toFile(toPic);//变为400*300,遵循原图比例缩或放到400*某个高度

//按比例缩小,压缩到原图的0.2尺寸
//Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);
//按比例放大,宽高*2
//Thumbnails.of(fromPic).scale(2f).toFile(toPic);

//图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量
//Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);

//watermark(位置,水印图,透明度)
//Thumbnails.of(fromPic).size(1280, 1024).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(waterPic), 0.5f).outputQuality(0.8f).toFile(toPic);

//图片中心400*400的区域
//Thumbnails.of(fromPic).sourceRegion(Positions.CENTER, 400,400).size(200, 200).keepAspectRatio(false).toFile(toPic);

System.out.println("结束:" + new Date().getTime());
}
原文地址:https://www.cnblogs.com/louxindong/p/5683933.html