瀏覽代碼

图片水印改为旋转

LongYuFei 1 年之前
父節點
當前提交
5bc00dcbf4

+ 12 - 2
ruoyi-admin/src/main/java/com/ruoyi/web/controller/wx/AttachmngsController.java

@@ -6,6 +6,7 @@ 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.apache.commons.compress.utils.IOUtils;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
@@ -59,10 +60,19 @@ public class AttachmngsController {
         System.out.println(date);
 
         // 水印
-        String watermark = longitude + "," + latitude + "-" + date;
+        // String watermark = longitude + "," + latitude + "-" + date;
+        String watermark =  date;
         File file2 = AddWatermarkUtil.transferToFile(file);
         // addWaterMark(file2, file2, watermark);
-        AddWatermarkUtil.waterPress(file2, file2, Color.GRAY, 16, watermark);
+
+        Thumbnails.of(file2)
+                .scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
+                .outputQuality(0.3f) //图片质量压缩比例 从0-1,越接近1质量越好
+                .toFile(file2);
+
+
+        // AddWatermarkUtil.waterPress(file2, file2, Color.DARK_GRAY, 48, watermark);
+        AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark);
 
         // 转二进制
         byte[] bytes = AddWatermarkUtil.fileToTransfer(file2).getBytes();

+ 10 - 8
ruoyi-common/src/main/java/com/ruoyi/common/utils/AddWatermarkUtil.java

@@ -81,7 +81,9 @@ public class AddWatermarkUtil {
                     sb.append(tempChar);
                 }
                 // 通过设置后两个输入参数给水印定位
-                g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY - 50);
+                //右下角
+                g.drawString(sb.toString(), srcImgWidth - maxLength, srcImgHeight - (contentLength - j - 1) * tempY - 50);
+                // g.drawString(sb.toString(), 20, srcImgHeight - (contentLength - j - 1) * tempY - 50);
             }
             g.dispose();
 
@@ -118,15 +120,15 @@ public class AddWatermarkUtil {
      * @param text
      * @throws IOException
      */
-    public static void addWaterMark(File inputFile, File outputFile, String text) throws IOException {
+    public static void addWaterMark(File inputFile, File outputFile, Color markContentColor, int fontSize, String text) throws IOException {
         Image image = ImageIO.read(inputFile);
         int imgWidth = image.getWidth(null);// 获取图片的宽
         int imgHeight = image.getHeight(null);// 获取图片的高
 
         int angel = 315;// 旋转角度
-        int xpadding = 40;// 每个水印水平间隔
-        int ypadding = 40;// 每个水印垂直间隔
-        int fontSize = 10;
+        int xpadding = 280;// 每个水印水平间隔
+        int ypadding = 280;// 每个水印垂直间隔
+        // int fontSize = 10;
 
         BufferedImage bi = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
 
@@ -150,11 +152,11 @@ public class AddWatermarkUtil {
         int stringWidth = g.getFontMetrics(g.getFont()).charsWidth(text.toCharArray(), 0, text.length());
 
         // 旋转水印
-        // g.rotate(Math.toRadians(angel), (double) imgWidth / 2, (double) imgHeight / 2);
+        g.rotate(Math.toRadians(angel), (double) imgWidth / 2, (double) imgHeight / 2);
         // 水印透明度
-        // g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
+        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5F));
         // 字体色
-        g.setColor(Color.RED);
+        g.setColor(markContentColor);
 
         int x = -imgHeight / 2;
         int y = -imgWidth / 2;