最近接了一个需求,需求是给了一张图片,需要向图片当中插入文字,具体文字插入什么根据程序来定,例如下图:
需要对成果名称,完成单位,完成人等进行填充文字。
![](https://i-blog.csdnimg.cn/blog_migrate/37759775d6b061fdf069c5bf70b9e747.png)
打开画图工具,然后选择文本
![](https://i-blog.csdnimg.cn/blog_migrate/e4293393f56f2f82eb2a6c6e09f1aa96.png)
然后我们利用文本框框来测出他的边距,有了边距我们才能定位写文字的位置,宽度1105
![](https://i-blog.csdnimg.cn/blog_migrate/5db59b7d4dd2b6b018e8f71c034e0cc9.png)
高度530
![](https://i-blog.csdnimg.cn/blog_migrate/e8287bc749f90778e722a0d369fef910.png)
实现出来的代码如下:
@GetMapping("/downImg")
public void downImg(HttpServletResponse response) {
downloadService.downImg(response);
}
如下代码当中除了IoUtil使用的是hutool的工具类外,其他的均属于Java基础类(不需要引入其他依赖就能使用的)。
public void downImg(HttpServletResponse response) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/中医药科技成果登记证书.png");
Image src = ImageIO.read(inputStream);
// 获取图片的高和宽
int wideth = src.getWidth(null);
int height = src.getHeight(null);
// 新增一个图片缓冲
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 设置字体颜色(颜色也可以直接new定义rgba,例如new Color(17, 16, 44))
g.setColor(Color.BLACK);
// size字体大小,Font.BOLD字体加粗
g.setFont(new Font("宋体", Font.BOLD, 50));
// 写入成果名称,由宽度减去我们测的宽度度,就等于要开始写的位置
g.drawString("测试测试", wideth - 1105, height - 530);
// 释放资源
g.dispose();
responseSetting(response, "中医药科技", ".png", "image/png");
outputStream = response.getOutputStream();
ImageIO.write(image, "png", outputStream);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IoUtil.close(inputStream);
IoUtil.close(outputStream);
}
}
这个是一个自己封装的设置response的方法
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class ServletResponseUtil {
public static final String EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public static final String ZIP = "application/octet-stream;charset=UTF-8";
public static final String PDF = "application/pdf;charset=UTF-8";
public static final String ZIP_SUFFIX = ".zip";
public static final String EXCEL_SUFFIX = ".xlsx";
public static final String PDF_SUFFIX = ".pdf";
/**
* 设置响应头信息
*
* @param response
* @param fileName 文件名
* @param suffix 文件后缀
* @param contentType 文件内容类型
*/
public static void responseSetting(HttpServletResponse response, String fileName, String suffix, String contentType) {
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String newFileName = null;
try {
newFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
newFileName = fileName;
}
// 当客户端请求的资源是一个可下载的资源(这里的“可下载”是指浏览器会弹出下载框或者下载界面)时,对这个可下载资源的描述(例如下载框中的文件名称)就是来源于该头域。
//response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + newFileName + suffix);
response.setHeader("Content-disposition", "attachment;filename=''" + newFileName + suffix);
// 服务器告诉浏览器它发送的数据属于什么文件类型,也就是响应数据的MIME类型
response.setContentType(contentType);
response.setCharacterEncoding("utf-8");
// 关闭缓存(HTTP/1.1)
response.setHeader("Cache-Control", "no-store");
// 关闭缓存(HTTP/1.0)
response.setHeader("Pragma", "no-cache");
// 缓存有效时间
response.setDateHeader("Expires", 0);
}
}
测试:
![](https://i-blog.csdnimg.cn/blog_migrate/b0a997d2e2efe8f8b1871054325e85e5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb09fe2e7370d3cca86c2bcddaee6536.png)
关于Font类:public Font(String name, int style, int size)
- name:字体,中文字体名:宋体,楷体,黑体等;英文字体名:Arial,Times New Roman等;
- style:风格
- Font.PLAIN (普通)
- Font.BOLD (加粗)
- Font.ITALIC (斜体)
- Font.BOLD+Font.ITALIC(斜体加粗)
- size:文字大小