diff --git a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java
index 9dcbfa410..c98afc437 100644
--- a/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/img/ImgUtil.java
@@ -43,9 +43,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Random;
+import java.util.*;
/**
* 图片处理工具类:
@@ -2002,6 +2000,56 @@ public class ImgUtil {
);
}
+ /**
+ * 获取给定图片的主色调,背景填充用
+ *
+ * @param image {@link BufferedImage}
+ * @param rgbFilters 过滤多种颜色
+ * @return {@link String} #ffffff
+ */
+ public static String getMainColor(BufferedImage image, int[]... rgbFilters) {
+ int r, g, b;
+ Map countMap = new HashMap<>();
+ int width = image.getWidth();
+ int height = image.getHeight();
+ int minx = image.getMinX();
+ int miny = image.getMinY();
+ for (int i = minx; i < width; i++) {
+ for (int j = miny; j < height; j++) {
+ int pixel = image.getRGB(i, j);
+ r = (pixel & 0xff0000) >> 16;
+ g = (pixel & 0xff00) >> 8;
+ b = (pixel & 0xff);
+ if (rgbFilters != null && rgbFilters.length > 0) {
+ for (int[] rgbFilter : rgbFilters) {
+ if (r == rgbFilter[0] && g == rgbFilter[1] && b == rgbFilter[2]) {
+ break;
+ }
+ }
+ }
+ countMap.merge(r + "-" + g + "-" + b, 1L, Long::sum);
+ }
+ }
+ String maxColor = null;
+ long maxCount = 0;
+ for (Map.Entry entry : countMap.entrySet()) {
+ String key = entry.getKey();
+ Long count = entry.getValue();
+ if (count > maxCount) {
+ maxColor = key;
+ maxCount = count;
+ }
+ }
+ String[] splitRgbStr = maxColor.split("-");
+ String rHex = Integer.toHexString(Integer.valueOf(splitRgbStr[0]));
+ String gHex = Integer.toHexString(Integer.valueOf(splitRgbStr[1]));
+ String bHex = Integer.toHexString(Integer.valueOf(splitRgbStr[2]));
+ rHex = rHex.length() == 1 ? "0" + rHex : rHex;
+ gHex = gHex.length() == 1 ? "0" + gHex : gHex;
+ bHex = bHex.length() == 1 ? "0" + bHex : bHex;
+ return "#" + rHex + gHex + bHex;
+ }
+
// ------------------------------------------------------------------------------------------------------ 背景图换算
/**
diff --git a/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java
index afc0e9117..7cd4930cb 100644
--- a/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/img/ImgUtilTest.java
@@ -13,6 +13,8 @@ import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
public class ImgUtilTest {
@@ -47,7 +49,7 @@ public class ImgUtilTest {
public void cutTest() {
ImgUtil.cut(FileUtil.file("d:/face.jpg"), FileUtil.file("d:/face_result.jpg"), new Rectangle(200, 200, 100, 100));
}
-
+
@Test
@Ignore
public void rotateTest() throws IOException {
@@ -88,27 +90,27 @@ public class ImgUtilTest {
public void sliceByRowsAndColsTest() {
ImgUtil.sliceByRowsAndCols(FileUtil.file("d:/test/logo.jpg"), FileUtil.file("d:/test/dest"), 1, 5);
}
-
+
@Test
@Ignore
public void convertTest() {
ImgUtil.convert(FileUtil.file("e:/test2.png"), FileUtil.file("e:/test2Convert.jpg"));
}
-
+
@Test
@Ignore
public void writeTest() {
final byte[] bytes = ImgUtil.toBytes(ImgUtil.read("d:/test/logo_484.png"), "png");
FileUtil.writeBytes(bytes, "d:/test/result.png");
}
-
+
@Test
@Ignore
public void compressTest() {
ImgUtil.compress(FileUtil.file("d:/test/dest.png"),
FileUtil.file("d:/test/1111_target.jpg"), 0.1f);
}
-
+
@Test
@Ignore
public void copyTest() {
@@ -136,4 +138,11 @@ public class ImgUtilTest {
new File("d:/test/3.jpg"),
new Color(200, 0, 0), 10);
}
+
+ @Test
+ public void getMainColor() throws MalformedURLException {
+ BufferedImage read = ImgUtil.read(new URL("https://pic2.zhimg.com/v2-94f5552f2b142ff575306850c5bab65d_b.png"));
+ String mainColor = ImgUtil.getMainColor(read, new int[]{64,84,116});
+ System.out.println(mainColor);
+ }
}