This commit is contained in:
Looly
2022-07-29 23:05:20 +08:00
parent a36a970341
commit 7769822e11
17 changed files with 237 additions and 27 deletions

View File

@@ -299,7 +299,7 @@ public class BackgroundRemoval {
}
}
final Map<String, Integer> map = new HashMap<>(list.size());
final Map<String, Integer> map = new HashMap<>(list.size(), 1);
for (final String string : list) {
Integer integer = map.get(string);
if (integer == null) {
@@ -309,7 +309,7 @@ public class BackgroundRemoval {
}
map.put(string, integer);
}
String max = "";
String max = StrUtil.EMPTY;
long num = 0;
for (final Map.Entry<String, Integer> entry : map.entrySet()) {
final String key = entry.getKey();
@@ -326,7 +326,7 @@ public class BackgroundRemoval {
return ImgUtil.toHex(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]),
Integer.parseInt(strings[2]));
}
return "";
return StrUtil.EMPTY;
}
// -------------------------------------------------------------------------- private

View File

@@ -1356,6 +1356,19 @@ public class ImgUtil {
return out.toByteArray();
}
/**
* 根据文字创建透明背景的PNG图片
*
* @param str 文字
* @param font 字体{@link Font}
* @param fontColor 字体颜色,默认黑色
* @param out 图片输出地
* @throws IORuntimeException IO异常
*/
public static void createTransparentImage(String str, Font font, Color fontColor, ImageOutputStream out) throws IORuntimeException {
writePng(createImage(str, font, null, fontColor, BufferedImage.TYPE_INT_ARGB), out);
}
/**
* 根据文字创建PNG图片
*

View File

@@ -1,7 +1,7 @@
package cn.hutool.swing.img;
import cn.hutool.core.io.FileUtil;
import cn.hutool.swing.img.ImgUtil;
import cn.hutool.core.io.IORuntimeException;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -147,4 +147,26 @@ public class ImgUtilTest {
System.out.println(mainColor);
}
@Test
@Ignore
public void createImageTest() throws IORuntimeException, IOException {
ImgUtil.createImage(
"版权所有",
new Font("黑体", Font.BOLD, 50),
Color.WHITE,
Color.BLACK,
ImageIO.createImageOutputStream(new File("d:/test/createImageTest.png"))
);
}
@Test
@Ignore
public void createTransparentImageTest() throws IORuntimeException, IOException {
ImgUtil.createTransparentImage(
"版权所有",
new Font("黑体", Font.BOLD, 50),
Color.BLACK,
ImageIO.createImageOutputStream(new File("d:/test/createTransparentImageTest.png"))
);
}
}