AttachmngsController.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.ruoyi.web.controller.wx;
  2. import com.ruoyi.common.MapUtils;
  3. import com.ruoyi.common.core.domain.AjaxResult;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.utils.AddWatermarkUtil;
  6. import com.ruoyi.common.utils.SecurityUtils;
  7. import com.ruoyi.system.domain.AttachMngs;
  8. import com.ruoyi.system.service.IAttachMngsService;
  9. import net.coobird.thumbnailator.Thumbnails;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.imageio.ImageIO;
  15. import javax.servlet.http.HttpServletRequest;
  16. import java.awt.*;
  17. import java.awt.image.BufferedImage;
  18. import java.io.*;
  19. import java.math.BigDecimal;
  20. @RestController
  21. @RequestMapping("/attachmngs")
  22. public class AttachmngsController {
  23. @Autowired
  24. private IAttachMngsService attachMngsService;
  25. @Autowired
  26. private MapUtils mapUtils;
  27. /**
  28. * 上传图片
  29. *
  30. * @param file 图片
  31. * @param longitude 经度
  32. * @param latitude 纬度
  33. * @return
  34. * @throws Exception
  35. */
  36. @PostMapping
  37. public AjaxResult updateAttachmngs(@RequestParam("avatarfile") MultipartFile file,
  38. @RequestParam("attachId") Long attachId,
  39. @RequestParam("longitude") String longitude,
  40. @RequestParam("latitude") String latitude,
  41. @RequestParam("carRegNo") String carRegNo,
  42. @RequestParam("updateDate") String date,
  43. HttpServletRequest request) throws Exception {
  44. if (file.isEmpty()) {
  45. return AjaxResult.error("上传失败请重试1");
  46. }
  47. System.out.println("水印时间");
  48. System.out.println(date);
  49. // 水印
  50. String address = mapUtils.pointsToLocationsAll(longitude + "," + latitude);
  51. String watermark = address + "||" + date + "||" + carRegNo;
  52. File file2 = AddWatermarkUtil.transferToFile(file);
  53. System.out.println("水印前大小:" + file2.length() / 1024);
  54. AddWatermarkUtil.waterPress(file2, file2, Color.WHITE, 48, watermark);
  55. // AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark);
  56. // MultipartFile multipartFile = AddWatermarkUtil.fileToTransfer(file2);
  57. System.out.println("压缩前大小:" + file2.length() / 1024);
  58. // ByteArrayOutputStream f = new ByteArrayOutputStream();
  59. String fileName = file2.getName();
  60. // if (file2.length() > 50 * 1024) {
  61. // 压缩
  62. Thumbnails.of(file2)
  63. .scale(0.6f) //图片大小(长宽)压缩比例 从0-1,1表示原图
  64. .toFile("/usr/local/project/DaoHe/imgFiles/" + fileName + ".jpg");
  65. File file1 = new File("/usr/local/project/DaoHe/imgFiles/" + fileName + ".jpg");
  66. System.out.println("压缩中大小:" + file1.length() / 1024);
  67. // }
  68. // compressPicCycle(file2, 50L, 0.5);
  69. System.out.println("压缩完成大小:" + file1.length() / 1024);
  70. // 转二进制
  71. byte[] bytes = AddWatermarkUtil.fileToTransfer(file1).getBytes();
  72. // byte[] bytes = f.toByteArray();
  73. // 存储到数据库
  74. AttachMngs attachMngs = new AttachMngs();
  75. attachMngs.setSysId(1L);
  76. attachMngs.setAttachId(attachId);
  77. attachMngs.setAttachFile(toObjects(bytes));
  78. Integer i = attachMngsService.insertAttachMngs(attachMngs);
  79. if (i != 1) {
  80. return AjaxResult.error("上传失败请重试2");
  81. }
  82. // 拼接url
  83. String serverName = request.getServerName();
  84. int serverPort = request.getServerPort();
  85. // return AjaxResult.success("操作成功", "http://" + serverName + ":" + serverPort + "/attachmngs/img/" + attachId);
  86. return AjaxResult.success("操作成功", "https://yd.echepei.com/prod-api/attachmngs/img/" + attachId);
  87. }
  88. private static void compressPicCycle(File desPath, long desFileSize, double accuracy) throws IOException {
  89. File srcFileJPG = desPath;
  90. long srcFileSizeJPG = srcFileJPG.length();
  91. //2.判断大小,如果小于50kb,不用压缩,如果大于等于50kb,需要压缩
  92. if (srcFileSizeJPG <= desFileSize * 1024) {
  93. return;
  94. }
  95. //计算宽高
  96. BufferedImage bim = ImageIO.read(srcFileJPG);
  97. int srcWidth = bim.getWidth();
  98. int srcHeight = bim.getHeight();
  99. int destWidth = new BigDecimal(srcWidth).multiply(new BigDecimal(accuracy)).intValue();
  100. int destHeight = new BigDecimal(srcHeight).multiply(new BigDecimal(accuracy)).intValue();
  101. Thumbnails.of(desPath).size(destWidth,destHeight).outputQuality(accuracy).toFile(desPath);
  102. compressPicCycle(desPath,desFileSize,accuracy);
  103. System.out.println("压缩中大小:" + desPath.length() / 1024);
  104. }
  105. /**
  106. * 获取图片
  107. *
  108. * @param attachId
  109. * @return
  110. */
  111. @GetMapping(value = "/img/{attachId}", produces = MediaType.IMAGE_JPEG_VALUE)
  112. public byte[] getImg(@PathVariable("attachId") String attachId) {
  113. Byte[] img = attachMngsService.getImg(attachId);
  114. return toPrimitives(img);
  115. }
  116. private Byte[] toObjects(byte[] bytesPrim) {
  117. Byte[] bytes = new Byte[bytesPrim.length];
  118. int i = 0;
  119. for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing
  120. return bytes;
  121. }
  122. private byte[] toPrimitives(Byte[] oBytes) {
  123. byte[] bytes = new byte[oBytes.length];
  124. for (int i = 0; i < oBytes.length; i++) {
  125. bytes[i] = oBytes[i];
  126. }
  127. return bytes;
  128. }
  129. }