JAVA实现根据图片生成缩略图、裁剪、压缩图片
依赖(用来复制文件,可以根据自己的来)
<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实现根据图片生成缩略图、裁剪、压缩图片的更多相关文章
- 如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放
如何安装nginx_lua_module模块,升级nginx,nginx-lua-fastdfs-GraphicsMagick动态生成缩略图,实现图片自动裁剪缩放 参考网站:nginx-lua-fas ...
- java 图片生成缩略图后,转化成流
功能:图片生成缩略图后,转化成流 public class ImageUtils { /** * * @param in1 * 文件流 * @param uploadFileName * 文件名称 * ...
- phpcms v9图片生成缩略图变成黑色解决方法
今天客户反映,上传的图片生成缩略图有的图片变成黑色,出现问题就百度了一下,有不少网友也遇到这样的问题,但是官方论坛也没有给出解决办法,那还得靠自己解决了,于是就研究phpcms v9 图片压缩代码.打 ...
- C# 图片生成缩略图
C# 图片生成缩略图方法: /// <summary> /// 生成缩略图 /// </summary> /// <param name="fileName&q ...
- 最新javascript自动按比例显示图片,按比例压缩图片显示
最新javascript自动按比例显示图片,按比例压缩图片显示 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...
- nginx实现本地图片生成缩略图
nginx可以实现图片的缩略图效果,很多网站为了前端静态资源相应的性能会给大图自动生成一个小图,比如我们经常会在网上看到bd_64x64.png这种格式,淘宝上的小图经常会看到xxx.jpg_100x ...
- js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能
html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...
- C#上传图片同时生成缩略图,控制图片上传大小。
#region 上传图片生成缩略图 /// <summary> /// 上传图片 /// </summary> /// <param name="sender& ...
- asp.net生成缩略图、文字图片水印
/// <summary> /// 会产生graphics异常的PixelFormat /// </summary> private static PixelFormat[] ...
- C#生成缩略图,C#给图片添加水印
生成缩略图 #region 生成缩略图 /// <summary> /// 生成缩略图 /// </summary> /// <param name="orig ...
随机推荐
- fluidity详解
fluidity详解 1.fluidity编译过程 1.1.femtools库调用方法 编译fluidity/femtools目录下所有文件,打包为libfemtools.a静态库文件: 通过-lfe ...
- MYSQL5.8----M2
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_s ...
- 【Python小试】使用列表解析式简化代码
列表解析式的好处: 代码简洁 可读性强 运行快 示例 来自<Python编程>中的一个例子:同时投掷两颗面数不同的骰子(如一个6面的D6和一个10面的D10)n次,统计两个骰子点数之和,并 ...
- R语言与医学统计图形-【18】ggplot2几何对象汇总
ggplot2绘图系统--几何对象汇总 前面介绍了常见的几种基本的几何对象,并且介绍了scale.stat等其他要素.后续将介绍position.themes.coord和faceting等函数. 这 ...
- MYSQL5.8-----3
666666666666666666666666 如多带有通配符的,要使用一下格式 select * from user where usename like "%1\%" ESC ...
- SonarQube的部分规则探讨
引言:为了更好的使项目代码规范化,减少Bug的出现,因此最近引入了SonarQube来帮助检测代码问题,这里就分享部分有趣的规则. 注:因为保密原则,文章贴出来的代码都是我按照格式仿写的,并非公司源码 ...
- linux "/tmp/crontab.14QJ49":1: bad minute errors in crontab file, can't install" 错误
目录 报错及原因 crontab语句格式 报错及原因 这个错误的原因是crontab格式错误 "/tmp/crontab.sdXvj4":5: bad minute errors ...
- 【Android】No Android SDK found(mac)+ 真机调试
[1]No Android SDK found 如果没下载SDK,可以去google官方下载 如果因为上网问题,这里提供两个网址,有人整理好了,这里先谢谢他们,下面两个择其一下载 http://to ...
- 监控Linux服务器网站状态的SHELL脚本
1,监控httpd状态码的shell脚本代码. #!/bin/sh #site: www.jquerycn.cn # website[0]=www.jquerycn.cn/chuzu/' #网站1 m ...
- Redis缓存穿透、缓存击穿以及缓存雪崩
作为一个内存数据库,redis也总是免不了有各种各样的问题,这篇文章主要是针对其中三个问题进行讲解:缓存穿透.缓存击穿和缓存雪崩.并给出一些解决方案.这三个问题是基本问题也是面试常问问题. 这篇文章我 ...