我有一个显示字符串图像的 Web 端点......当以下代码运行(在 tomcat 中)时,它会在 OSX 的任务栏中生成一个 java 图标。不知道是不是有问题,或者发生了什么。寻找某种解释
@RequestMapping("/text/{text}")
public void textImage(HttpServletResponse response, @PathVariable("text") String text){
response.setContentType("image/png");
try{
OutputStream os = response.getOutputStream();
BufferedImage bufferedImage = new BufferedImage( (text.length()*10) , 14, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setBackground(Color.WHITE);
g2d.setPaint(Color.BLACK);
Font font = new Font("sansserif", Font.PLAIN, 12);
g2d.setFont(font);
g2d.drawString(text, 0, 12);
ImageIO.write(bufferedImage, "png", os);
} catch(Exception e) {
// nothing we can do, simply log the error
logger.error("Could not draw string: ", e);
}
}