mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-16 21:19:35 +08:00
ImgUtil 添加获取图片主色调的方法。
作用,填充网页空白有用,或者自定义处理图像填充白边。
This commit is contained in:
parent
68f73c9e64
commit
1e0a88ffda
@ -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.*;
|
||||
|
||||
/**
|
||||
* 图片处理工具类:<br>
|
||||
@ -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<String, Long> 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<String, Long> 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;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------ 背景图换算
|
||||
|
||||
/**
|
||||
|
@ -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 {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user