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

   <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);
}
}

JAVA实现根据图片生成缩略图、裁剪、压缩图片的更多相关文章

  1. 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放

    如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...

  2. java 图片生成缩略图后,转化成流

    功能:图片生成缩略图后,转化成流 public class ImageUtils { /** * * @param in1 * 文件流 * @param uploadFileName * 文件名称 * ...

  3. phpcms v9图片生成缩略图变成黑色解决方法

    今天客户反映,上传的图片生成缩略图有的图片变成黑色,出现问题就百度了一下,有不少网友也遇到这样的问题,但是官方论坛也没有给出解决办法,那还得靠自己解决了,于是就研究phpcms v9 图片压缩代码.打 ...

  4. C# 图片生成缩略图

    C# 图片生成缩略图方法: /// <summary> /// 生成缩略图 /// </summary> /// <param name="fileName&q ...

  5. 最新javascript自动按比例显示图片,按比例压缩图片显示

    最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  6. nginx实现本地图片生成缩略图

    nginx可以实现图片的缩略图效果,很多网站为了前端静态资源相应的性能会给大图自动生成一个小图,比如我们经常会在网上看到bd_64x64.png这种格式,淘宝上的小图经常会看到xxx.jpg_100x ...

  7. js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能

    html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...

  8. C#上传图片同时生成缩略图,控制图片上传大小。

    #region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...

  9. asp.net生成缩略图、文字图片水印

    /// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] ...

  10. C#生成缩略图,C#给图片添加水印

    生成缩略图 #region 生成缩略图 /// <summary> /// 生成缩略图 /// </summary> /// <param name="orig ...

随机推荐

  1. FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅲ

    第三波,走起~~ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅰ FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅱ 单位根反演 今天打多校时 1002 被卡科技了 ...

  2. 洛谷 P6860 - 象棋与马(找性质+杜教筛)

    题面传送门 首先我们来探究一下什么样的 \((a,b)\) 满足 \(p(a,b)=1\).不难发现只要点 \((1,0)\) 能够到达,那么网格上所有点都能到达,因为由于 \((1,0)\) 能够到 ...

  3. Perl 语言入门6-9

    ---- 第6章 哈希----------- 简介 键值对.键和值都是任意标量,但键总是会被转换成字符串. 键唯一,值可重复. 应用场景:一组数据对应到另一组数据时. 如找出重复/唯一/交叉引用/查表 ...

  4. php5.6升级7

    1. 检查当前安装的 PHP查看当前 PHP 版本 php -v查看当前 PHP 相关的安装包 yum list installed | grep php2. 更换 RPM 源#Centos 5.X: ...

  5. Excel-满足指定条件并且包含数字的单元格数目,DCOUNT()

    DCOUNT函数 函数名称:DCOUNT 主要功能:返回数据库或列表的列中满足指定条件并且包含数字的单元格数目. 使用格式:DCOUNT(database,field,criteria) 参数说明:D ...

  6. OpenSSH 密码和公钥认证原理探究

    目录 配置和保护SSH H3 - 使用SSH 访问远程命令行 H4 - 什么是OpenSSH ? H4 - 登录方式: H4 - 登录并执行临时命令: H4 - 查看登录用户 H4 - 登录原理 密码 ...

  7. 13 — springboot集成mybatis-plus — 更新完毕

    1.mybatis-plus需要掌握的知识 1).mybatis-plus是什么? 不写了,老衲一般都是直接进官网 mybatis-plus官网地址:https://baomidou.com/guid ...

  8. vim中搜索指定单词(不加前后缀)

    \< : 搜索内容作为单词开头 \> : 搜索内容作为单词结尾 一起用即为将搜索内容指定为whole word e.g. : word_suffix word 如果用/word来搜索则两个 ...

  9. Java事务与JTA

    一.什么是JAVA事务 通俗的理解,事务是一组原子操作单元,从数据库角度说,就是一组SQL指令,要么全部执行成功,若因为某个原因其中一条指令执行有错误,则撤销先前执行过的所有指令.更简答的说就是:要么 ...

  10. Advanced C++ | Virtual Copy Constructor

    这个不懂,等看会了再写...