|
@@ -13,9 +13,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.awt.*;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/attachmngs")
|
|
@@ -51,43 +55,32 @@ public class AttachmngsController {
|
|
|
System.out.println(date);
|
|
|
|
|
|
// 水印
|
|
|
-// String watermark = longitude + "," + latitude + "-" + date;
|
|
|
-
|
|
|
String address = mapUtils.pointsToLocationsAll(longitude + "," + latitude);
|
|
|
-
|
|
|
- SysUser user = SecurityUtils.getLoginUser().getUser();
|
|
|
-// String watermark = longitude + "," + latitude + "-" + date + "-" + user.getNickName();
|
|
|
String watermark = address + "||" + date + "||" + carRegNo;
|
|
|
- System.out.println( );
|
|
|
-// String watermark = date;
|
|
|
File file2 = AddWatermarkUtil.transferToFile(file);
|
|
|
- // addWaterMark(file2, file2, watermark);
|
|
|
-
|
|
|
-
|
|
|
- // 如果图片大于一兆压缩
|
|
|
-// if (AddWatermarkUtil.fileToTransfer(file2).getBytes().length > 240800) {
|
|
|
-// // 压缩
|
|
|
- Thumbnails.of(file2)
|
|
|
- .scale(0.5f) //图片大小(长宽)压缩比例 从0-1,1表示原图
|
|
|
- .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
|
|
|
- .toFile(file2);
|
|
|
-// }
|
|
|
-
|
|
|
|
|
|
+ System.out.println("水印前大小:" + file2.length() / 1024);
|
|
|
AddWatermarkUtil.waterPress(file2, file2, Color.DARK_GRAY, 48, watermark);
|
|
|
// AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark);
|
|
|
|
|
|
-
|
|
|
-// if (AddWatermarkUtil.fileToTransfer(file2).getBytes().length > 1048576) {
|
|
|
+// MultipartFile multipartFile = AddWatermarkUtil.fileToTransfer(file2);
|
|
|
+ System.out.println("压缩前大小:" + file2.length() / 1024);
|
|
|
+// ByteArrayOutputStream f = new ByteArrayOutputStream();
|
|
|
+ String fileName = file2.getName();
|
|
|
+// if (file2.length() > 50 * 1024) {
|
|
|
// 压缩
|
|
|
- Thumbnails.of(file2)
|
|
|
- .scale(0.5f) //图片大小(长宽)压缩比例 从0-1,1表示原图
|
|
|
- .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
|
|
|
- .toFile(file2);
|
|
|
+ Thumbnails.of(file2)
|
|
|
+ .scale(0.6f) //图片大小(长宽)压缩比例 从0-1,1表示原图
|
|
|
+ .toFile("/usr/local/project/DaoHe/imgFiles/" + fileName + ".jpg");
|
|
|
+ File file1 = new File("/usr/local/project/DaoHe/imgFiles/" + fileName + ".jpg");
|
|
|
+ System.out.println("压缩中大小:" + file1.length() / 1024);
|
|
|
// }
|
|
|
+// compressPicCycle(file2, 50L, 0.5);
|
|
|
+ System.out.println("压缩完成大小:" + file1.length() / 1024);
|
|
|
|
|
|
// 转二进制
|
|
|
- byte[] bytes = AddWatermarkUtil.fileToTransfer(file2).getBytes();
|
|
|
+ byte[] bytes = AddWatermarkUtil.fileToTransfer(file1).getBytes();
|
|
|
+// byte[] bytes = f.toByteArray();
|
|
|
|
|
|
// 存储到数据库
|
|
|
AttachMngs attachMngs = new AttachMngs();
|
|
@@ -109,6 +102,24 @@ public class AttachmngsController {
|
|
|
return AjaxResult.success("操作成功", "https://yd.echepei.com/prod-api/attachmngs/img/" + attachId);
|
|
|
}
|
|
|
|
|
|
+ private static void compressPicCycle(File desPath, long desFileSize, double accuracy) throws IOException {
|
|
|
+ File srcFileJPG = desPath;
|
|
|
+ long srcFileSizeJPG = srcFileJPG.length();
|
|
|
+ //2.判断大小,如果小于50kb,不用压缩,如果大于等于50kb,需要压缩
|
|
|
+ if (srcFileSizeJPG <= desFileSize * 1024) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //计算宽高
|
|
|
+ BufferedImage bim = ImageIO.read(srcFileJPG);
|
|
|
+ int srcWidth = bim.getWidth();
|
|
|
+ int srcHeight = bim.getHeight();
|
|
|
+ int destWidth = new BigDecimal(srcWidth).multiply(new BigDecimal(accuracy)).intValue();
|
|
|
+ int destHeight = new BigDecimal(srcHeight).multiply(new BigDecimal(accuracy)).intValue();
|
|
|
+ Thumbnails.of(desPath).size(destWidth,destHeight).outputQuality(accuracy).toFile(desPath);
|
|
|
+ compressPicCycle(desPath,desFileSize,accuracy);
|
|
|
+ System.out.println("压缩中大小:" + desPath.length() / 1024);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取图片
|
|
|
*
|