Java之图片添加文字
2017-09-27 17:19
302 查看
在实际开发中,有时候会遇到操作图片的需求,比如说在已有的图片上添加文字,生成新的图片等需求,下面代码是我根据项目需求做的在图片上添加文字的功能:
添加文字后图片:
d8a6
这样就完成了图片添加文字的需求,当然,文字添加的位置要根据项目需求去做。
核心就是使用Graphics2D的drawString、drawImage、drawLine来完成文字、图片、分割线的添加
public class GenerateDynamicHealthImage { Log log = LogFactory.getLog(GenerateDynamicHealthImage.class); /** * 生成图片方法 * @author WQ * @param inPath * @param outPath * @param content * @param img * @param font * @param start_point_x * @param start_point_y */ public void generateImage(String inPath, String outPath, String[][] content, Image img, Font font, int start_point_x, int start_point_y) { try { File file = new File(inPath); Image image = ImageIO.read(file); int height = image.getHeight(null); int width = image.getWidth(null); int text_x = 0; int text_y = 0; int img_x = 0; int img_y = 520; int line_y = 498; Color color = new Color(89, 89, 89); BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = bufferedImage.createGraphics(); //文字去锯齿 graphics.drawImage(image, 0, 0, width, height, null); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); if (img != null) { graphics.setFont(font); text_x = start_point_x; text_y = start_point_y; for (int i = 0; i < content.length; i++) { String[] tempData = content[i]; color = new Color(89, 89, 89); text_x = start_point_x; if (i == 1){ text_y += 110; img_y += 140; } else if (i != 0 && i > 1){ text_y += 100; img_y += 130; } line_y += 132; for (int j = 0; j < tempData.length; j++) { if (j == 1) { text_y += 50; color = new Color(171, 171, 171); graphics.drawLine(12, line_y, 750 - 12, line_y); } else if (j == 2) { text_x = width - 124; text_y -= 20; img_x = 50; if (tempData[2].equals("过高")){ color = new Color(255, 80, 80); } else { color = new Color(71, 207, 162); } graphics.drawImage(img, img_x, img_y, null); } graphics.setColor(color); graphics.drawString(tempData[j], text_x, text_y); } } } else { System.out.println("构建数据的图片对象为空..."); } FileOutputStream fileOutputStream = new FileOutputStream(outPath); ImageIO.write(bufferedImage, "png", fileOutputStream); System.out.println("图片添加文字完成" + width + "---" + height); fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { log.info("io exception :" + e); } } public static void main(String[] args) { Font font = new Font("微软雅黑", Font.PLAIN, 30); String inPath = "D:/TestGenerateImage/DynamicImage/testImage.png"; String outPath = "D:/TestGenerateImage/DynamicImage/testImage1.png"; /*String inPath = "D:/TestGenerateImage/DynamicImage/Test.jpg"; String outPath = "D:/TestGenerateImage/DynamicImage/Test1.jpg";*/ String[][] textConent = { { "血压(非空腹) : 6.0mmol/L", "时间: 2017年9月16日 10:52", "正常" }, { "血压(非空腹) : 13.0mmol/L", "时间: 2017年9月16日 15:47", "过高" }, { "血压(非空腹) : 6.3mmol/L", "时间: 2017年9月16日 16:13", "正常" } , { "血压(非空腹) : 6.6mmol/L", "时间: 2017年9月17日 10:24", "正常" }, { "血压(非空腹) : 15.1mmol/L", "时间: 2017年9月17日 11:10", "过高" } }; int x = 150; int y = 540; try { new GenerateDynamicHealthImage().generateImage(inPath, outPath, textConent, ImageIO.read(new File( "D:/TestGenerateImage/testIcon1.png")), font, x, y); } catch (IOException e) { e.printStackTrace(); } } }添加文字前图片:
添加文字后图片:
d8a6
这样就完成了图片添加文字的需求,当然,文字添加的位置要根据项目需求去做。
核心就是使用Graphics2D的drawString、drawImage、drawLine来完成文字、图片、分割线的添加
相关文章推荐
- java给图片添加水印文字
- java添加透明的图片和文字水印
- Java代码实现给图片添加文字水印 详细教程一
- 用Java实现在图片上添加水印图片和水印文字
- java实现给图片添加水印和文字
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- 用java实现给图片增加图片水印或者文字水印(也支持视频图像帧添加水印)
- java程序给指定的图片添加水印文字并完美消除文字锯齿
- java实现给图片添加水印和文字
- 关于java 向图片添加文字时的乱码问题
- Java添加水印+图片水印+文字水印
- 使用Java添加图片水印和文字水印
- Java实现给图片添加图片水印,文字水印及马赛克的方法示例
- java图片添加水印,包括文字水印和图片水印
- Java给图片添加图片、文字水印
- javacpp-opencv图像处理之2:实时视频添加图片水印,实现不同大小图片叠加,图像透明度控制,文字和图片双水印
- 用java 对图片的截图、拼接、合成处理、添加文字
- JAVA给图片添加图片水印或文字水印
- Java添加水印+图片水印+文字水印
- java 通过 itext生成pdf添加,插入文字,图片