Skip to content

Commit

Permalink
add: pic executor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrettluo committed Oct 6, 2023
1 parent e14292a commit 70b27e4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/jiaruiblog/enums/DocType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public enum DocType {
HTML,
MD,
TXT,
// pic类的文档
JPG,
JPEG,
PNG,
// unknown
UNKNOWN;

Expand All @@ -36,6 +40,12 @@ public static DocType getDocType(String suffixName) {
return HTML;
case ".txt":
return TXT;
case ".jpeg":
return JPEG;
case ".jpg":
return JPG;
case ".png":
return PNG;
default:
return UNKNOWN;
}
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/jiaruiblog/task/executor/PicExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.jiaruiblog.task.executor;

import com.jiaruiblog.task.data.TaskData;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
* @ClassName PicExecutor
* @Description jepg, jpg, gif ... to png
* @Author luojiarui
* @Date 2023/10/6 23:36
* @Version 1.0
**/
public class PicExecutor extends TaskExecutor{

public static final String PNG = "png";
public static final Integer TARGET_WIDTH = 120;
public static final Integer TARGET_HEIGHT = 200;

@Override
protected void readText(InputStream is, String textFilePath) throws IOException {
// do nothing for pic
// read text from pic by orc etc.
}

@Override
protected void makeThumb(InputStream is, String picPath) throws IOException {
BufferedImage bim = ImageIO.read(is);
bim = resizeImage(bim, TARGET_WIDTH, TARGET_HEIGHT);
File output = new File(picPath);
ImageIO.write(bim, PNG, output);
}

@Override
protected void makePreviewFile(InputStream is, TaskData taskData) {
// do nothing for pic
}


/**
* 通过BufferedImage图片流调整图片大小
* 指定压缩后长宽
*/
public static BufferedImage resizeImage(BufferedImage originalImage, int targetWidth, int targetHeight) {
Image resultingImage = originalImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_AREA_AVERAGING);
BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
outputImage.getGraphics().drawImage(resultingImage, 0, 0, null);
return outputImage;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ private static TaskExecutor createTaskExecutor(DocType docType) {
case TXT:
taskExecutor = new TxtExecutor();
break;
case JPG:
case JPEG:
case PNG:
taskExecutor = new PicExecutor();
break;
default:
break;
}
Expand Down

0 comments on commit 70b27e4

Please sign in to comment.