Compare commits
2 Commits
92444d64b7
...
542c9e10c1
Author | SHA1 | Date |
---|---|---|
|
542c9e10c1 | 1 year ago |
|
a806630cbc | 1 year ago |
@ -0,0 +1,49 @@
|
||||
package cn.jyjz.xiaoyao.ocr.util;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @Title:
|
||||
* @Description: 图片设置
|
||||
* @Author 郭向斌
|
||||
* @Date 2024-03-18 15:29
|
||||
* @Version V1.0
|
||||
*/
|
||||
public class ImageUtils {
|
||||
public static String generateThumbnail(String inputImagePath, Integer newWidth) throws IOException {
|
||||
if(newWidth==null){
|
||||
newWidth=182;
|
||||
}
|
||||
String directoryPath = inputImagePath.substring(0, inputImagePath.lastIndexOf("/") + 1);
|
||||
File inputFile = new File(inputImagePath);
|
||||
String outputImagePath = directoryPath+ "thumbnail_" + inputFile.getName();
|
||||
BufferedImage bufferedImage = Thumbnails.of(inputFile).scale(1).asBufferedImage();
|
||||
int width = bufferedImage.getWidth();
|
||||
int height = bufferedImage.getHeight();
|
||||
int newHeight = (int) Math.round((double) height * newWidth / width);
|
||||
|
||||
Thumbnails.of(inputFile).size(newWidth, newHeight).outputFormat( getFileExtension(inputFile.getName())).toFile(new File(outputImagePath));
|
||||
return outputImagePath;
|
||||
}
|
||||
|
||||
|
||||
public static String getFileExtension(String fileName) {
|
||||
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
} else {
|
||||
return "jpg"; // 没有后缀名或者文件名以点号开头
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String url="C:\\Users\\郭向斌\\Downloads\\1-星浩.png";
|
||||
String s = generateThumbnail(url,180);
|
||||
System.out.println(s);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in new issue