package com.ruoyi.web.controller.wx; import com.ruoyi.common.MapUtils; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.utils.AddWatermarkUtil; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.system.domain.AttachMngs; import com.ruoyi.system.service.IAttachMngsService; import net.coobird.thumbnailator.Thumbnails; 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") public class AttachmngsController { @Autowired private IAttachMngsService attachMngsService; @Autowired private MapUtils mapUtils; /** * 上传图片 * * @param file 图片 * @param longitude 经度 * @param latitude 纬度 * @return * @throws Exception */ @PostMapping public AjaxResult updateAttachmngs(@RequestParam("avatarfile") MultipartFile file, @RequestParam("attachId") Long attachId, @RequestParam("longitude") String longitude, @RequestParam("latitude") String latitude, @RequestParam("carRegNo") String carRegNo, @RequestParam("updateDate") String date, HttpServletRequest request) throws Exception { if (file.isEmpty()) { return AjaxResult.error("上传失败请重试1"); } System.out.println("水印时间"); System.out.println(date); // 水印 String address = mapUtils.pointsToLocationsAll(longitude + "," + latitude); String watermark = address + "||" + date + "||" + carRegNo; File file2 = AddWatermarkUtil.transferToFile(file); System.out.println("水印前大小:" + file2.length() / 1024); AddWatermarkUtil.waterPress(file2, file2, Color.WHITE, 48, watermark); // AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark); // 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.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(file1).getBytes(); // byte[] bytes = f.toByteArray(); // 存储到数据库 AttachMngs attachMngs = new AttachMngs(); attachMngs.setSysId(1L); attachMngs.setAttachId(attachId); attachMngs.setAttachFile(toObjects(bytes)); Integer i = attachMngsService.insertAttachMngs(attachMngs); if (i != 1) { return AjaxResult.error("上传失败请重试2"); } // 拼接url String serverName = request.getServerName(); int serverPort = request.getServerPort(); // return AjaxResult.success("操作成功", "http://" + serverName + ":" + serverPort + "/attachmngs/img/" + attachId); 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); } /** * 获取图片 * * @param attachId * @return */ @GetMapping(value = "/img/{attachId}", produces = MediaType.IMAGE_JPEG_VALUE) public byte[] getImg(@PathVariable("attachId") String attachId) { Byte[] img = attachMngsService.getImg(attachId); return toPrimitives(img); } private Byte[] toObjects(byte[] bytesPrim) { Byte[] bytes = new Byte[bytesPrim.length]; int i = 0; for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing return bytes; } private byte[] toPrimitives(Byte[] oBytes) { byte[] bytes = new byte[oBytes.length]; for (int i = 0; i < oBytes.length; i++) { bytes[i] = oBytes[i]; } return bytes; } }