依赖(用来复制文件,可以根据自己的来)

   <dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
ImageScaleUtils.java
import org.apache.commons.io.FileUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException; /**
* @author
* @date 2021/10/11
*/
public class ImageScaleUtils { /**
* 缩小图片
*
* @param srcFile 原图片
* @param destFile 目标图片
* @param boxWidth 缩略图最大宽度
* @param boxHeight 缩略图最大高度
* @throws IOException
*/
public static void resizeFix(File srcFile, File destFile, int boxWidth,
int boxHeight) throws IOException {
BufferedImage srcImgBuff = ImageIO.read(srcFile);
int width = srcImgBuff.getWidth();
int height = srcImgBuff.getHeight();
if (width <= boxWidth && height <= boxHeight) {
FileUtils.copyFile(srcFile, destFile);
return;
}
int zoomWidth;
int zoomHeight;
if ((float) width / height > (float) boxWidth / boxHeight) {
zoomWidth = boxWidth;
zoomHeight = Math.round((float) boxWidth * height / width);
} else {
zoomWidth = Math.round((float) boxHeight * width / height);
zoomHeight = boxHeight;
}
BufferedImage imgBuff = scaleImage(srcImgBuff, width, height,
zoomWidth, zoomHeight);
writeFile(imgBuff, destFile);
} /**
* 裁剪并压缩
*
* @param srcFile 原文件
* @param destFile 目标文件
* @param boxWidth 缩略图最大宽度
* @param boxHeight 缩略图最大高度
* @param cutTop 裁剪TOP
* @param cutLeft 裁剪LEFT
* @param cutWidth 裁剪宽度
* @param catHeight 裁剪高度
* @throws IOException
*/
public static void resizeFix(File srcFile, File destFile, int boxWidth,
int boxHeight, int cutTop, int cutLeft, int cutWidth, int catHeight)
throws IOException {
BufferedImage srcImgBuff = ImageIO.read(srcFile);
srcImgBuff = srcImgBuff.getSubimage(cutTop, cutLeft, cutWidth,
catHeight);
int width = srcImgBuff.getWidth();
int height = srcImgBuff.getHeight();
if (width <= boxWidth && height <= boxHeight) {
writeFile(srcImgBuff, destFile);
return;
}
int zoomWidth;
int zoomHeight;
if ((float) width / height > (float) boxWidth / boxHeight) {
zoomWidth = boxWidth;
zoomHeight = Math.round((float) boxWidth * height / width);
} else {
zoomWidth = Math.round((float) boxHeight * width / height);
zoomHeight = boxHeight;
}
BufferedImage imgBuff = scaleImage(srcImgBuff, width, height,
zoomWidth, zoomHeight);
writeFile(imgBuff, destFile);
} public static void writeFile(BufferedImage imgBuf, File destFile)
throws IOException {
File parent = destFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
ImageIO.write(imgBuf, "jpeg", destFile);
} private static BufferedImage scaleImage(BufferedImage srcImgBuff,
int width, int height, int zoomWidth, int zoomHeight) {
int[] colorArray = srcImgBuff.getRGB(0, 0, width, height, null, 0,
width);
BufferedImage outBuff = new BufferedImage(zoomWidth, zoomHeight,
BufferedImage.TYPE_INT_RGB);
// 宽缩小的倍数
float wScale = (float) width / zoomWidth;
int wScaleInt = (int) (wScale + 0.5);
// 高缩小的倍数
float hScale = (float) height / zoomHeight;
int hScaleInt = (int) (hScale + 0.5);
int area = wScaleInt * hScaleInt;
int x0, x1, y0, y1;
int color;
long red, green, blue;
int x, y, i, j;
for (y = 0; y < zoomHeight; y++) {
// 得到原图高的Y坐标
y0 = (int) (y * hScale);
y1 = y0 + hScaleInt;
for (x = 0; x < zoomWidth; x++) {
x0 = (int) (x * wScale);
x1 = x0 + wScaleInt;
red = green = blue = 0;
for (i = x0; i < x1; i++) {
for (j = y0; j < y1; j++) {
color = colorArray[width * j + i];
red += getRedValue(color);
green += getGreenValue(color);
blue += getBlueValue(color);
}
}
outBuff.setRGB(x, y, comRGB((int) (red / area),
(int) (green / area), (int) (blue / area)));
}
}
return outBuff;
} private static int getRedValue(int rgbValue) {
return (rgbValue & 0x00ff0000) >> 16;
} private static int getGreenValue(int rgbValue) {
return (rgbValue & 0x0000ff00) >> 8;
} private static int getBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
} private static int comRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
} public static void main(String[] args) throws Exception{
resizeFix(
new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"), new File(
"C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum-1.jpg"), 310, 310); resizeFix(
new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"), new File(
"C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum-2.jpg"), 310, 310,140,200,300,400);
}
}

最新文章

  1. C# 抓取网站数据
  2. Python之禅+八荣八耻
  3. linux基础知识(四)
  4. verilogHDL设计中的同步时序逻辑
  5. dubbo源码分析一:整体分析
  6. MySQL 数据显示宽度
  7. hibernate的orphanRemoval
  8. PHP详解$_SEVER常用变量
  9. Windows上mxnet实战深度学习:Neural Net
  10. python学习===如何理解python中的return
  11. Django学习-1-管理我的django程序
  12. python 基础之注释变量常量
  13. windows攻击实验
  14. Linux ACL 权限之进阶篇
  15. springcloud-知识点总结(一):Eureka
  16. PHP 正则表达式---匹配模式
  17. 58VIP账号发贴器
  18. express学习-express搭建后台
  19. 带包的java类在cmd环境下的执行办法
  20. C# Post发送数据返回页面结果

热门文章

  1. CF1540B Tree Array
  2. Perl语言入门14-17
  3. python-django-分页处理
  4. day33 前端之css
  5. c#中实现串口通信的几种方法
  6. deque、queue和stack深度探索(下)
  7. Windows 下 Node.js 开发环境搭建
  8. mysql之对象创建
  9. my43_mysql内存相关概念
  10. Javascript 数组对象常用的API