<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.1</span><span style=\"margin-left: 60px\">国家:AAA</span><br>\n" +
"<span>省份:AAAA</span><span style=\"margin-left: 88px\">位置:XXXXXXX</span></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();
}