|
|
@ -1,12 +1,13 @@
|
|
|
|
package org.jeecg.modules.ocr.utils;
|
|
|
|
package org.jeecg.modules.ocr.utils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.coobird.thumbnailator.Thumbnails;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.image.AffineTransformOp;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.*;
|
|
|
|
import java.io.IOException;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @Description
|
|
|
|
* @Description
|
|
|
@ -17,49 +18,52 @@ public class ImageUtils {
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
当涉及到图像压缩时,Java中有几个可以使用的库,比如ImageIO、Thumbnails和Java Advanced Imaging API(JAI)。以下是一个使用ImageIO库来进行图片压缩的示例:
|
|
|
|
当涉及到图像压缩时,Java中有几个可以使用的库,比如ImageIO、Thumbnails和Java Advanced Imaging API(JAI)。以下是一个使用ImageIO库来进行图片压缩的示例:
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
String inputImagePath = "C:\\Users\\Denim\\Desktop\\385cb7b31e14c05c1aa1613170118772.jpeg"; // 输入图片的路径
|
|
|
|
|
|
|
|
String outputImagePath = "C:\\Users\\Denim\\Desktop\\thumbnail\\sl-385cb7b31e14c05c1aa1613170118772.jpeg"; // 压缩后图片的保存路径
|
|
|
|
|
|
|
|
int maxWidth = 800; // 压缩后图片的最大宽度
|
|
|
|
|
|
|
|
int maxHeight = 600; // 压缩后图片的最大高度
|
|
|
|
|
|
|
|
compressImage(inputImagePath, outputImagePath, maxWidth, maxHeight);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 压缩图片
|
|
|
|
* 等比例压缩
|
|
|
|
* @param inputImagePath
|
|
|
|
* @param sourceImagePath
|
|
|
|
* @param outputImagePath
|
|
|
|
* @param outputImagePath
|
|
|
|
* @param maxWidth
|
|
|
|
* @param scale
|
|
|
|
* @param maxHeight
|
|
|
|
* @param quality
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void compressImage(String inputImagePath, String outputImagePath, int maxWidth, int maxHeight) {
|
|
|
|
public static void compressImage(String sourceImagePath, String outputImagePath,Float scale,Float quality){
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
// 读取原始图片
|
|
|
|
Thumbnails.of(new File(sourceImagePath))
|
|
|
|
File inputFile = new File(inputImagePath);
|
|
|
|
.scale(scale) //图片大小(长宽)压缩比例 从0-1,1表示原图
|
|
|
|
BufferedImage inputImage = ImageIO.read(inputFile);
|
|
|
|
.outputQuality(quality) //图片质量压缩比例 从0-1,越接近1质量越好
|
|
|
|
// 计算压缩比例,并确定输出图片的大小
|
|
|
|
.toOutputStream(new FileOutputStream(outputImagePath));
|
|
|
|
double widthRatio = (double) maxWidth / inputImage.getWidth();
|
|
|
|
|
|
|
|
double heightRatio = (double) maxHeight / inputImage.getHeight();
|
|
|
|
|
|
|
|
double ratio = Math.min(widthRatio, heightRatio);
|
|
|
|
|
|
|
|
int newWidth = (int) (inputImage.getWidth() * ratio);
|
|
|
|
|
|
|
|
int newHeight = (int) (inputImage.getHeight() * ratio);
|
|
|
|
|
|
|
|
// 创建缩放后的图片对象
|
|
|
|
|
|
|
|
BufferedImage outputImage = new BufferedImage(newWidth, newHeight, inputImage.getType());
|
|
|
|
|
|
|
|
// 绘制缩放后的图像
|
|
|
|
|
|
|
|
Graphics2D graphics2D = outputImage.createGraphics();
|
|
|
|
|
|
|
|
graphics2D.drawImage(inputImage, 0, 0, newWidth, newHeight, null);
|
|
|
|
|
|
|
|
graphics2D.dispose();
|
|
|
|
|
|
|
|
// 写入压缩后的图片到输出路径
|
|
|
|
|
|
|
|
ImageIO.write(outputImage, "jpg", new File(outputImagePath));
|
|
|
|
|
|
|
|
System.out.println("图片压缩完成!");
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
在这个示例中,`compressImage`方法接受输入图片的路径、输出图片的保存路径以及所需的最大宽度和最大高度。它通过使用`ImageIO`库来读取原始图片并创建一个缩放后的`BufferedImage`对象。然后,通过使用`Graphics2D`对象,将原始图片绘制到缩放后的图像中,并将其写入到输出路径。压缩比例是根据最大宽度和最大高度计算得出的,保持图像的宽高比例。
|
|
|
|
// 获取图片的旋转角度信息
|
|
|
|
请确保您已经按照您的需求修改了输入路径、输出路径以及最大宽度和最大高度。此示例假设要压缩的图片格式为JPEG,输出也是JPEG格式,但您可以根据需求进行修改。
|
|
|
|
private static int getOrientation(File imageFile) throws IOException {
|
|
|
|
*/
|
|
|
|
// TODO: 从图片的元数据中获取旋转角度信息,具体实现略
|
|
|
|
|
|
|
|
// 这里假设返回的旋转角度为0
|
|
|
|
|
|
|
|
return 90;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据旋转角度调整图片
|
|
|
|
|
|
|
|
private static BufferedImage adjustOrientation(BufferedImage image, int orientation) {
|
|
|
|
|
|
|
|
// TODO: 根据旋转角度调整图片,具体实现略
|
|
|
|
|
|
|
|
// 这里假设不进行任何旋转调整
|
|
|
|
|
|
|
|
return image;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 压缩图片
|
|
|
|
|
|
|
|
private static BufferedImage compressImage(BufferedImage image, int targetWidth, int targetHeight) {
|
|
|
|
|
|
|
|
Image scaledImage = image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
|
|
|
|
|
|
|
|
BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Graphics2D g2d = compressedImage.createGraphics();
|
|
|
|
|
|
|
|
g2d.drawImage(scaledImage, 0, 0, null);
|
|
|
|
|
|
|
|
g2d.dispose();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return compressedImage;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|