這第一步固然是在網上下載一款字體啦,而後把下載的字體放在對應的文件位置,接下來就是代碼了
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
@RestController
public class LoadFontController {
@GetMapping("/test")
public void test() throws IOException, FontFormatException {
File file = new File("E://包圖小白體.ttf");
FileInputStream fs = new FileInputStream(file);
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, fs);
fs.close();
//Font font = new Font("微軟雅黑", Font.PLAIN, 35); //水印字體
Font font = new Font(dynamicFont.getName(), Font.PLAIN, 35); //水印字體
String srcImgPath = "E://15.jpg"; //源圖片地址
String tarImgPath = "E://" + System.currentTimeMillis() + ".jpg"; //待存儲的地址
String waterMarkContent = "時光蹉跎,看淡歲月你我"; //水印內容
Color color = new Color(225, 225, 60); //水印圖片色彩以及透明度
addWaterMark(srcImgPath, tarImgPath, waterMarkContent, color, font);
}
public void addWaterMark(String srcImgPath, String tarImgPath, Color markContentColor, String content, Font font) throws IOException {
// 讀取原圖片信息
File srcImgFile = new File(srcImgPath);//獲得文件
Image 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); //設置字體
//設置水印的座標 若是水印長度超過圖片寬度則換行
int fontLen = getWatermarkLength(content, g); // 水印文字總長度
int line = fontLen / srcImgWidth;//文字長度相對於圖片寬度應該有多少行
int y = (line + 1) * font.getSize();
System.out.println("水印文字總長度:" + fontLen + ",圖片寬度:" + srcImgWidth + ",字符個數:" + content.length());
//文字疊加,自動換行疊加
int tempX = 50; // 文字初始x座標位置
int tempY = y; // 文字初始y座標位置
int tempCharLen;//單字符長度
int tempLineLen = 0;//單行字符總長度臨時計算
StringBuffer sb = new StringBuffer();
for (int i = 0; i < content.length(); i++) {
char tempChar = content.charAt(i);
tempCharLen = getCharLen(tempChar, g);
tempLineLen += tempCharLen;
if (tempLineLen >= srcImgWidth - 70) {
//長度已經滿一行,進行文字疊加
g.drawString(sb.toString(), tempX, tempY);
sb.delete(0, sb.length());//清空內容,從新追加
tempY += font.getSize();
tempLineLen = 0;
}
sb.append(tempChar);//追加字符
}
g.drawString(sb.toString(), tempX, tempY);//最後疊加餘下的文字
g.dispose();
// 輸出圖片
FileOutputStream outImgStream = new FileOutputStream(tarImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
System.out.println("添加水印完成");
outImgStream.flush();
outImgStream.close();
}
/** * 獲取水印文字總長度 * * @param waterMarkContent * @param g * @return */
public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
}
/** * 獲取水印對應文字的位置 * * @param c * @param g * @return */
public int getCharLen(char c, Graphics2D g) {
return g.getFontMetrics(g.getFont()).charWidth(c);
}
}複製代碼
通過本人親測,妥妥的java