java 图片压缩工具

时间:2022-04-28 14:49:41
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.juling.file.FileUtil;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
* 图片压缩工具
*
* @author DXM
*
*/
public class ImageUtil {

/**
* 压缩图片尺寸
*
* @param src
* @param max
* @param scale
* @return
* @throws IOException
*/
private static BufferedImage zoomImage(String src, int max, float scale) throws IOException {

File srcfile = new File(src);
if (!srcfile.exists()) {
System.out.println("文件不存在");
return null;
}

BufferedImage im = ImageIO.read(srcfile);

/* 原始图像的宽度和高度 */
int width = im.getWidth();
int height = im.getHeight();

/* 调整后的图片的宽度和高度 */
while (width > max || height > max) {
width = (int) (width * scale);
height = (int) (height * scale);
}

/* 新生成结果图片 */
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
return result;
}

/**
* 压缩图片尺寸
*
* @param src
* @param width
* @param height
* @return
* @throws IOException
*/
private static BufferedImage zoomImage(String src, int width, int height) throws IOException {

File srcfile = new File(src);
if (!srcfile.exists()) {
System.out.println("文件不存在");
return null;
}

BufferedImage im = ImageIO.read(srcfile);

/* 新生成结果图片 */
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
return result;
}

/**
* 压缩图片尺寸
*
* @param src
* @param max
* @return
* @throws IOException
*/
private static BufferedImage zoomImage(String src, int max) throws IOException {

File srcfile = new File(src);
if (!srcfile.exists()) {
System.out.println("文件不存在");
return null;
}

BufferedImage im = ImageIO.read(srcfile);

/* 原始图像的宽度和高度 */
int width = im.getWidth();
int height = im.getHeight();

/* 调整后的图片的宽度和高度 */
if (width > max) {
double bl = max * 1.0 / width * 1.0;
width = (int) (width * bl);
height = (int) (height * bl);
}

if (height > max) {
double bl = max / height;
width = (int) (width * bl);
height = (int) (height * bl);
}

/* 新生成结果图片 */
BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
return result;
}

/**
* 压缩图片质量
*
* @param bi
* @param qualityScale
* @throws IOException
* @throws ImageFormatException
*/
private static boolean compressImage(BufferedImage bi, float qualityScale, String src, String dest, boolean keep) throws IOException {

if (null == bi) {
return false;
}

FileOutputStream fos = new FileOutputStream(dest);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi);

/* 压缩质量 */
jep.setQuality(qualityScale, true);
encoder.encode(bi, jep);

/* 近JPEG编码 */
fos.close();

if (!keep) {
FileUtil.delFile(new File(src));
}

return true;
}

public static boolean compress(String src, String dest, int max, float qualityScale, boolean keep) {

try {

BufferedImage bi = zoomImage(src, max);
return compressImage(bi, qualityScale, src, dest, keep);

} catch (Exception e) {
e.printStackTrace();
}

return false;
}

public static boolean compress(String src, String dest, int max, float sizeScale, float qualityScale, boolean keep) {

try {

BufferedImage bi = zoomImage(src, max, sizeScale);
return compressImage(bi, qualityScale, src, dest, keep);

} catch (Exception e) {
e.printStackTrace();
}

return false;
}

public static boolean compress(String src, String dest, int width, int height, float qualityScale, boolean keep) {

try {

BufferedImage bi = zoomImage(src, width, height);
return compressImage(bi, qualityScale, src, dest, keep);

} catch (Exception e) {
e.printStackTrace();
}

return false;
}

public static void main(String[] args) {

/* 这儿填写你存放要缩小图片的文件夹全地址 */
String inputFoler = "C:\\Users\\新美\\Pictures\\DAimG_2016033130668574C426.jpg";

/* 这儿填写你转化后的图片存放的文件夹 */
String outputFolder = "E:\\test\\DAimG_2016033130668574C426.jpg";

ImageUtil.compress(inputFoler, outputFolder, 1600, 0.9f, 0.9f, true);

}

}