前提:要想实现图片的高清剪切,需要保证原始图片是高清图片,且本程序运行剪切后可大幅度缩小图片的大小
一、主程序main()方法:
import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
public static void main(String[] args) throws Exception {
try {
/**
* 指定剪切后图片的宽高
*/
cutImage1("D:\\Mercedes-Benz.jpg", "D:\\Mercedes-Benz1.jpg", 500, 400);
/**
* 指定放大/缩小的比例
*/
cutImage2("D:\\Mercedes-Benz.jpg", "D:\\Mercedes-Benz2.jpg", 0.9);
} catch (Exception e) {
e.printStackTrace();
}
}
二、按照指定宽高剪切:
/**
* 按照指定宽高剪切图片
*
* @param fromFilePath 原始图片完整路径
* @param saveToFilePath 缩略图片保存路径
* @param width 剪切后图片的宽
* @param height 剪切后图片的高
* @throws Exception
*/
private static void cutImage1(String fromFilePath, String saveToFilePath, int width, int height) throws Exception {
// 校验原始图片
File file = new File(fromFilePath);
if (!file.isFile()) {
throw new Exception(file + " is not image file error in cutImage!");
}
BufferedImage buffer = ImageIO.read(file);
/*
* 核心算法,计算图片的压缩比
*
* w 和 h 为原始图片的宽和高
*
* width 和 height 为压缩/放大后图片的宽和高
*/
int w = buffer.getWidth();
int h = buffer.getHeight();
double ratiox = 1.0;
double ratioy = 1.0;
ratiox = w * ratiox / width;
ratioy = h * ratioy / height;
// 缩小图片
if (ratiox >= 1) {
if (ratioy < 1) {
ratiox = height * 1.0 / h;
} else {
if (ratiox > ratioy) {
ratiox = height * 1.0 / h;
} else {
ratiox = width * 1.0 / w;
}
}
} else {
// 放大图片
if (ratioy < 1) {
if (ratiox > ratioy) {
ratiox = height * 1.0 / h;
} else {
ratiox = width * 1.0 / w;
}
} else {
ratiox = width * 1.0 / w;
}
}
/*
* 对于图片的放大或缩小倍数计算完成,ratiox大于1,则表示放大,否则表示缩小
*/
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratiox, ratiox), null);
buffer = op.filter(buffer, null);
// 从放大的图像中心截图
buffer = buffer.getSubimage((buffer.getWidth() - width) / 2, (buffer.getHeight() - height) / 2, width, height);
try {
ImageIO.write(buffer, "jpg", new File(saveToFilePath));
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: " + ex.getMessage());
}
}
三、按照指定放大/缩小因子来剪切:
/**
* 等比例放大/缩小图片
*
* @param fromFilePath 原始图片完整路径
* @param saveToFilePath 缩略图片保存路径
* @param scale 缩放比例
* @throws Exception
*/
private static void cutImage2(String fromFilePath, String saveToFilePath, double scale) throws Exception {
// 校验原始图片
File file = new File(fromFilePath);
if (!file.isFile()) {
throw new Exception(file + " is not image file error in cutImage!");
}
BufferedImage buffer = ImageIO.read(file);
/*
* width和height为压缩后图片的宽和高
*/
int width = (int) (buffer.getWidth() * scale);
int height = (int) (buffer.getHeight() * scale);
AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(scale, scale), null);
buffer = op.filter(buffer, null);
buffer = buffer.getSubimage(0, 0, width, height);
try {
ImageIO.write(buffer, "jpg", new File(saveToFilePath));
} catch (Exception ex) {
throw new Exception(" ImageIo.write error in CreatThum.: " + ex.getMessage());
}
}
四、图片展示:
原始图片-Mercedes-Benz.jpg:
指定大小后-Mercedes-Benz1.jpg:
指定放大/缩小因子-Mercedes-Benz2.jpg:
图片总览:
图片属性:
1,原始图片:
2,指定宽高:
3,指定放大/缩小因子: