Skip to content

Commit

Permalink
Using Strategy Design And add new MODE
Browse files Browse the repository at this point in the history
  • Loading branch information
GARZHI committed Sep 22, 2021
1 parent 43f7a89 commit a623747
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 65 deletions.
88 changes: 88 additions & 0 deletions src/main/java/CompressStrategy/AutoStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package CompressStrategy;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AutoStrategy implements CompressStrategy {
String folderPath=null;
Rename RENAME_STRATEGY = null;

int limit_X = 0;
int limit_Y = 0;

@Override
public void Input(BufferedReader bf) throws IOException {
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;
}

@Override
public void Compress() throws IOException {
//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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package CompressStrategy;

import java.io.File;

public class CompressObject {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/CompressStrategy/CompressStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package CompressStrategy;

import net.coobird.thumbnailator.name.Rename;

import java.io.BufferedReader;
import java.io.IOException;

public interface CompressStrategy {
void Input(BufferedReader bf) throws IOException;
void Compress() throws IOException;
}
38 changes: 38 additions & 0 deletions src/main/java/CompressStrategy/ScaleStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package CompressStrategy;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

public class ScaleStrategy implements CompressStrategy {
String folderPath=null;
Rename RENAME_STRATEGY = null;
double scale = 1.0;

@Override
public void Input(BufferedReader bf) throws IOException {
System.out.println("请输入压缩图片存放的文件夹路径(最好是都要修改的):");
folderPath = bf.readLine();
folderPath = folderPath.replaceAll("\\\\" , "\\\\\\\\");

System.out.println("请输入缩放的比例(单位%):");
int scalePercent = Integer.parseInt(bf.readLine());
scale = scalePercent / 100.0;

System.out.println("是否覆盖原文件? T/F");
//有个可能BUG 但是不想修就是输不是T的 == F
RENAME_STRATEGY = (bf.readLine().equals("T"))? Rename.NO_CHANGE : Rename.PREFIX_DOT_THUMBNAIL;
}

@Override
public void Compress() throws IOException {
System.out.println("批量压缩中");
Thumbnails.of(new File(folderPath).listFiles())
.scale(scale)
.outputFormat("jpg")
.toFiles(RENAME_STRATEGY);
}
}
44 changes: 44 additions & 0 deletions src/main/java/CompressStrategy/SizeStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package CompressStrategy;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

public class SizeStrategy implements CompressStrategy {
String folderPath=null;
Rename RENAME_STRATEGY = null;

int target_X = 0;
int target_Y = 0;

@Override
public void Input(BufferedReader bf) throws IOException {
System.out.println("请输入压缩图片存放的文件夹路径(最好是都要修改的):");
folderPath = bf.readLine();
folderPath = folderPath.replaceAll("\\\\" , "\\\\\\\\");

System.out.println("请输入图片目标长度(单位px):");
target_X = Integer.parseInt(bf.readLine());

System.out.println("请输入图片目标宽度(单位px):");
target_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;
}

@Override
public void Compress() throws IOException {
//tip 卧槽居然默认就是保比例的
System.out.println("批量压缩中");
Thumbnails.of(new File(folderPath).listFiles())
.size(target_X,target_Y)
.keepAspectRatio(false)
.outputFormat("jpg")
.toFiles(RENAME_STRATEGY);
}
}
106 changes: 41 additions & 65 deletions src/main/java/client.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CompressStrategy.*;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Expand All @@ -8,32 +8,58 @@
import java.util.stream.Collectors;

//tip 优化 IMAGEIO 那里其实读取了两遍,但Thumb没有读取像素的功能我也表示很绝望
//有空可以改成ENUM
public class client {
public final static int MODE_SCALE = 1;
public final static int MODE_SIZE = 2;
public final static int MODE_AUTO = 3;

public static void main(String[] args) {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

String folderPath=null;
Rename RENAME_STRATEGY = null;
CompressStrategy compressStrategy = null;

int limit_X = 0;
int limit_Y = 0;
int MODE=0;

//请保证文件夹内都是要修改的
try{
System.out.println("请输入压缩图片存放的文件夹(最好是都要修改的):");
folderPath = bf.readLine();
folderPath = folderPath.replaceAll("\\\\" , "\\\\\\\\");
System.out.println("请选择模式: \n" +
"1 (批量比例缩放)\n" +
"2 (强制缩放到指定长X宽,也就是不等比)\n" +
"3 (自动化等比缩放(旧版))");
MODE = Integer.parseInt(bf.readLine());

//MODE规范性检测
if(MODE!=1 && MODE!=2 && MODE!=3){
System.out.println("不存在属于该编号的模式:"+ MODE);
System.out.println("模式错误,按任意键结束程序");
try { bf.readLine(); }catch (Exception e){ e.printStackTrace(); }
}

System.out.println("请输入图片最大长度(单位px):");
limit_X = Integer.parseInt(bf.readLine());
//Strategy Set
switch (MODE){
case MODE_SCALE:
compressStrategy = new ScaleStrategy();
break;
case MODE_SIZE:
compressStrategy = new SizeStrategy();
break;
case MODE_AUTO:
compressStrategy = new AutoStrategy();
break;
default:
System.out.println("不存在属于该编号的模式:"+ MODE);
System.out.println("模式错误,按任意键结束程序");
try { bf.readLine(); }catch (Exception e){ e.printStackTrace(); }
return;
}

System.out.println("请输入图片最大宽度(单位px):");
limit_Y = Integer.parseInt(bf.readLine());
//输入
compressStrategy.Input(bf);

System.out.println("是否覆盖原文件? T/F");
//压缩
compressStrategy.Compress();

//有个可能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问题");
Expand All @@ -44,56 +70,6 @@ public static void main(String[] args) {

//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("运行完毕,按任意键结束程序");
//任意读入一个东西
Expand Down

0 comments on commit a623747

Please sign in to comment.