From 1ee5043af9ee5e53acb52a70fd93c4656e8bbb44 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 21 九月 2023 11:59:37 +0800
Subject: [PATCH] Merge branch 'dev' of http://139.186.142.91:10010/r/productDev/preselect into dev

---
 server/service/src/main/java/com/doumee/core/utils/ImageUtil.java |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 172 insertions(+), 0 deletions(-)

diff --git a/server/service/src/main/java/com/doumee/core/utils/ImageUtil.java b/server/service/src/main/java/com/doumee/core/utils/ImageUtil.java
new file mode 100644
index 0000000..be93da1
--- /dev/null
+++ b/server/service/src/main/java/com/doumee/core/utils/ImageUtil.java
@@ -0,0 +1,172 @@
+package com.doumee.core.utils;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.imageio.ImageIO;
+import javax.imageio.ImageWriter;
+import javax.imageio.stream.ImageOutputStream;
+import javax.net.ssl.HttpsURLConnection;
+
+public class ImageUtil {
+    private static List<File> fileList = new ArrayList<>();
+
+    public static void main(String[] args) {
+        //convertAllImages("F:\\before.png", "F:\\after.png");
+        fileList.add(new File("C:\\Users\\T14\\Desktop\\1\\1.jpg"));
+        convertAllImages(fileList);
+    }
+
+    /**
+     * 鍘婚櫎鏂囦欢鍒楄〃閲屽浘鐗囩殑姘村嵃骞舵浛鎹�
+     *
+     * @Param fileList 鏂囦欢鍒楄〃
+     */
+    public static void convertAllImages(List<File> fileList) {
+        try {
+            for (File file : fileList) {
+                if (!file.getName().endsWith("png") && !file.getName().endsWith("jpg")) {
+                    continue;
+                }
+                BufferedImage bi = ImageIO.read(file); //鐢↖mageIO娴佽鍙栧儚绱犲潡
+                if (bi != null) {
+                    removeWatermark(bi);
+                    String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);//鐢熸垚鐨勫浘鐗囨牸寮�
+                    ImageIO.write(bi, formatName, file);//鐢↖mageIO娴佺敓鎴愮殑澶勭悊鍥炬浛鎹㈠師鍥剧墖
+                }
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 浠庢簮鐩綍鑾峰彇鍥剧墖澶勭悊鍚庡鍑哄埌鐩爣鐩綍
+     *
+     * @param dir     婧愮洰褰�
+     * @param saveDir 鐩爣鐩綍
+     */
+    private static void convertAllImages(String dir, String saveDir) {
+        File dirFile = new File(dir);
+        File saveDirFile = new File(saveDir);
+        dir = dirFile.getAbsolutePath();
+        saveDir = saveDirFile.getAbsolutePath();
+        loadImages(new File(dir));
+        for (File file : fileList) {
+            String filePath = file.getAbsolutePath();
+            String dstPath = saveDir + filePath.substring(filePath.indexOf(dir) + dir.length());
+            replace(file.getAbsolutePath(), dstPath);
+        }
+    }
+
+    /**
+     * 鍔犺浇鍥剧墖
+     */
+    private static void loadImages(File f) {
+        if (f != null) {
+            if (f.isDirectory()) {
+                File[] fileArray = f.listFiles();
+                if (fileArray != null) {
+                    for (File file : fileArray) {
+                        loadImages(file); //閫掑綊璋冪敤
+                    }
+                }
+            } else {
+                String name = f.getName();
+                if (name.endsWith("png") || name.endsWith("jpg")) {
+                    fileList.add(f);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鐢熸垚婧愬浘鐗囩殑澶勭悊鍥�
+     *
+     * @param srcFile 婧愬浘鐗囪矾寰�
+     * @param dstFile 鐩爣鍥剧墖璺緞
+     */
+    private static void replace(String srcFile, String dstFile) {
+        try {
+            URL http;
+            if (srcFile.trim().startsWith("https")) {
+                http = new URL(srcFile);
+                HttpsURLConnection conn = (HttpsURLConnection) http.openConnection();
+                conn.setRequestMethod("GET");
+            } else if (srcFile.trim().startsWith("http")) {
+                http = new URL(srcFile);
+                HttpURLConnection conn = (HttpURLConnection) http.openConnection();
+                conn.setRequestMethod("GET");
+            } else {
+                http = new File(srcFile).toURI().toURL();
+            }
+            BufferedImage bi = ImageIO.read(http.openStream());
+            if (bi != null) {
+                removeWatermark(bi);
+                exportImage(bi, srcFile, dstFile);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 鐢盜mageIO娴佺敓鎴愭簮鍥剧墖鐨勫鐞嗗浘
+     *
+     * @param bi       ImageIO
+     * @param fileName 婧愬浘鐗囧甫鍚庣紑鐨勬枃浠跺悕
+     * @param dstFile  鐩爣鍥剧墖璺緞
+     */
+    private static void exportImage(BufferedImage bi, String fileName, String dstFile) {
+        try {
+            String type = fileName.substring(fileName.lastIndexOf(".") + 1);
+            Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(type);
+            ImageWriter writer = it.next();
+            File f = new File(dstFile);
+            ImageOutputStream ios = ImageIO.createImageOutputStream(f);
+            writer.setOutput(ios);
+            writer.write(bi);
+            bi.flush();
+            ios.flush();
+            ios.close();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 鍘婚櫎姘村嵃
+     */
+    private static void removeWatermark(BufferedImage bi) {
+        Color wColor = new Color(197, 196, 191);
+        Color hColor = new Color(140, 88, 100);
+//        Color wColor = new Color(254, 254, 254);
+//        Color hColor = new Color(197, 196, 191);
+        //鐧藉簳姘村嵃
+        for (int i = 0; i < bi.getWidth(); i++) {
+            for (int j = 0; j < bi.getHeight(); j++) {
+                int color = bi.getRGB(i, j);
+                Color oriColor = new Color(color);
+                int red = oriColor.getRed();
+                int greed = oriColor.getGreen();
+                int blue = oriColor.getBlue();
+                if (red == 254 && greed == 254 && blue == 254) {
+                    continue;
+                }
+                if (red > 220 && greed > 180 && blue > 80) {
+                    bi.setRGB(i, j, wColor.getRGB());
+                }
+                if (red <= 240 && greed >= 200 && blue >= 150) {
+                    bi.setRGB(i, j, wColor.getRGB());
+                }
+            }
+        }
+    }
+}

--
Gitblit v1.9.3