发表更新1 分钟读完 (大约198个字)
java图片添加文字/图片水印
读取要处理的图片
1 2
| ClassPathResource resource = new ClassPathResource("template/clockbg.png"); BufferedImage image = ImageIO.read(resource.getInputStream());
|
创建画笔
1
| Graphics2D pen = image.createGraphics();
|
添加文字
1 2 3
| pen.setFont(new Font("微软雅黑", Font.PLAIN, 40)); pen.setColor(Color.black); pen.drawString(timeContent, START_X, y);
|
添加网络图片
1 2
| BufferedImage headImage = ImageIO.read(new URL(param.getImage())); pen.drawImage(headImage, START_X, y - INTERVAL_Y, 100, 100, null);
|
获取字体行数
1 2 3 4 5 6 7
| private int getRowNum(String content) { int rowNum = content.length() / FONT_NUM; if (content.length() % FONT_NUM != 0) { rowNum ++; } return rowNum; }
|
文字区域换行
1 2 3 4 5 6 7 8 9 10 11
| String content = "恭喜您完成《" + param.getTrainName() + "》课程与认证。"; int rowNum = getRowNum(content); for (int i = 0; i < rowNum; i++) { if (i == rowNum - 1) { pen.drawString(content.substring(FONT_NUM * i), START_X, y); } else { pen.drawString(content.substring(FONT_NUM * i, FONT_NUM * (i + 1)), START_X, y); } y += INTERVAL_Y; }
|
docker中添加中文字体
1 2 3
| ADD yahei.ttf /usr/share/fonts/ttf-yahei/yahei.ttf
RUN apk add fontconfig
|