<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency
html
String htmlTemplate = "<div>" +
"<span>IP地址:127.0.0.1span>国家:AAAspan><br>\n" +
"<span>省份:AAAAspan>位置:XXXXXXXspan>div>";
HtmlParser htmlParser = new HtmlParserImpl();
htmlParser.loadHtml(htmlTemplate);
ImageRenderer imageRenderer = new ImageRendererImpl(htmlParser);
imageRenderer.saveImage("D:\\hello-world.png");
图片添加水印
public static void waterPress(String srcImgPath, String outImgPath, String waterMarkContent, Color markContentColor, Font font) {
try {
File srcImgFile = new File(srcImgPath);
Image srcImg = null;
if (srcImgFile.exists() && srcImgFile.isFile() && srcImgFile.canRead()) {
srcImg = ImageIO.read(srcImgFile);
}
int srcImgWidth = srcImg.getWidth(null);
int srcImgHeight = srcImg.getHeight(null);
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufImg.createGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
g.setColor(markContentColor);
g.setFont(font);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int fontLength = getWatermarkLength(waterMarkContent, g);
Double textLineCount = Math.ceil(Integer.valueOf(fontLength).doubleValue() / Integer.valueOf(srcImgWidth).doubleValue());
int fontSize = font.getSize();
int textHeight = textLineCount.intValue() * fontSize;
int originX = 0;
int originY = 0;
if (1 == textLineCount.intValue()) {
originY = srcImgHeight / 2 - fontSize / 2;
originX = (srcImgWidth - fontLength) / 2;
} else {
originY = (srcImgHeight - textHeight) / 10;
}
System.out.println("水印文字总长度:" + fontLength + ",图片宽度:" + srcImgWidth + ",字符个数:" + waterMarkContent.length());
int tempX = originX;
int tempY = originY;
int tempCharLen = 0;
int tempLineLen = 0;
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < waterMarkContent.length(); i++) {
char tempChar = waterMarkContent.charAt(i);
tempCharLen = getCharLen(tempChar, g);
if (tempLineLen >= srcImgWidth) {
g.drawString(stringBuffer.toString(), tempX, tempY);
stringBuffer.delete(0, stringBuffer.length());
tempY = tempY + fontSize;
tempLineLen = 0;
}
stringBuffer.append(tempChar);
tempLineLen += tempCharLen;
}
g.drawString(stringBuffer.toString(), tempX, tempY);
g.dispose();
FileOutputStream outImgStream = new FileOutputStream(outImgPath);
ImageIO.write(bufImg, "png", outImgStream);
outImgStream.flush();
outImgStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static int getCharLen(char c, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charWidth(c);
}
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
public static void testWaterPressFile(){
Font font = new Font("微软雅黑",Font.BOLD+ Font.ITALIC, 18);
Color color = Color.cyan;
String content = "图片来源:Gaoxs";
waterPress("D:\\hello-world.png",
"D:\\hello-world22.png",
content, color, font);
}
public static void main(String[] args) {
testWaterPressFile();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113