-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: net.coobird:thumbnailator:0.4.8" level="project" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>groupId</groupId> | ||
<artifactId>ImageCompressDemo</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>8</source> | ||
<target>8</target> | ||
</configuration> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-assembly-plugin</artifactId> | ||
<version>2.5.5</version> | ||
<configuration> | ||
<archive> | ||
<manifest> | ||
<mainClass>client</mainClass> | ||
</manifest> | ||
</archive> | ||
<descriptorRefs> | ||
<descriptorRef>jar-with-dependencies</descriptorRef> | ||
</descriptorRefs> | ||
</configuration> | ||
|
||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>net.coobird</groupId> | ||
<artifactId>thumbnailator</artifactId> | ||
<version>0.4.8</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import java.io.File; | ||
|
||
public class CompressObject { | ||
public File imgFile; | ||
public float scale; | ||
|
||
public CompressObject(File imgFile, float scale){ | ||
this.imgFile = imgFile; | ||
this.scale = scale; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import net.coobird.thumbnailator.Thumbnails; | ||
import net.coobird.thumbnailator.name.Rename; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.*; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
//tip 优化 IMAGEIO 那里其实读取了两遍,但Thumb没有读取像素的功能我也表示很绝望 | ||
public class client { | ||
public static void main(String[] args) { | ||
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
String folderPath=null; | ||
Rename RENAME_STRATEGY = null; | ||
|
||
int limit_X = 0; | ||
int limit_Y = 0; | ||
|
||
//请保证文件夹内都是要修改的 | ||
try{ | ||
System.out.println("请输入压缩图片存放的文件夹(最好是都要修改的):"); | ||
folderPath = bf.readLine(); | ||
folderPath = folderPath.replaceAll("\\\\" , "\\\\\\\\"); | ||
|
||
System.out.println("请输入图片最大长度(单位px):"); | ||
limit_X = Integer.parseInt(bf.readLine()); | ||
|
||
System.out.println("请输入图片最大宽度(单位px):"); | ||
limit_Y = Integer.parseInt(bf.readLine()); | ||
|
||
System.out.println("是否覆盖原文件? T/F"); | ||
|
||
//有个可能BUG 但是不想修就是输不是T的 == F | ||
RENAME_STRATEGY = (bf.readLine().equals("T"))? Rename.NO_CHANGE : Rename.PREFIX_DOT_THUMBNAIL; | ||
}catch (IOException ioe){ | ||
ioe.printStackTrace(); | ||
System.out.println("程序执行错误,IO问题"); | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
System.out.println("程序执行错误,问题不明"); | ||
} | ||
|
||
//C:\Users\ASUS\Desktop\测试文件夹\007.jpg | ||
|
||
// 流式压缩 | ||
try { | ||
//tip asList | ||
List<File> imgList = Arrays.asList(new File(folderPath).listFiles()); | ||
|
||
final int limit_height = limit_Y; | ||
final int limit_width = limit_X; | ||
|
||
List<CompressObject> toConductList = imgList.stream().map(r->{ | ||
float scale=1; | ||
|
||
//计算能够过限制的最大scale | ||
try { | ||
BufferedImage sourceImg = ImageIO.read(new FileInputStream(r)); | ||
int imgH = sourceImg.getHeight(); | ||
int imgW = sourceImg.getWidth(); | ||
|
||
if(imgH>limit_height && imgW>limit_width){ | ||
float scaleA = limit_height / (float)imgH; | ||
float scaleB = limit_width / (float)imgW; | ||
|
||
scale = Math.min(scaleA,scaleB); | ||
}else if(imgH>limit_height){ | ||
scale = limit_height / (float)imgH; | ||
}else if(imgW>limit_width){ | ||
scale = limit_width / (float)imgW; | ||
}else { | ||
scale = (float) 1.0; | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return new CompressObject(r,scale); | ||
}).collect(Collectors.toList()); | ||
|
||
|
||
//压缩 | ||
for (CompressObject srcImg : toConductList){ | ||
System.out.println("开始压缩:"+srcImg.imgFile.getName()); | ||
Thumbnails.of(srcImg.imgFile) | ||
.scale(srcImg.scale) | ||
.outputFormat("jpg") | ||
.toFiles(RENAME_STRATEGY); | ||
System.out.println("压缩完毕,压缩比:"+srcImg.scale); | ||
} | ||
}catch (Exception e){ | ||
e.printStackTrace(); | ||
System.out.println("出现故障"); | ||
} | ||
|
||
System.out.println("运行完毕,按任意键结束程序"); | ||
//任意读入一个东西 | ||
try { bf.readLine(); }catch (Exception e){ e.printStackTrace(); } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#Generated by Maven | ||
#Sat Sep 18 19:06:21 CST 2021 | ||
version=1.0-SNAPSHOT | ||
groupId=groupId | ||
artifactId=ImageCompressDemo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
D:\ImageCompressDemo\src\main\java\client.java | ||
D:\ImageCompressDemo\src\main\java\CompressObject.java |