Skip to content

Commit

Permalink
Git Init
Browse files Browse the repository at this point in the history
  • Loading branch information
GARZHI committed Sep 22, 2021
0 parents commit 43f7a89
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__net_coobird_thumbnailator_0_4_8.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ImageCompressDemo.iml
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>
48 changes: 48 additions & 0 deletions pom.xml
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>
11 changes: 11 additions & 0 deletions src/main/java/CompressObject.java
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;
}
}
102 changes: 102 additions & 0 deletions src/main/java/client.java
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(); }
}
}
Binary file not shown.
Binary file added target/ImageCompressDemo-1.0-SNAPSHOT.jar
Binary file not shown.
Binary file added target/classes/CompressObject.class
Binary file not shown.
Binary file added target/classes/client.class
Binary file not shown.
5 changes: 5 additions & 0 deletions target/maven-archiver/pom.properties
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
Empty file.
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
Empty file.

0 comments on commit 43f7a89

Please sign in to comment.