Java之图片添加文字

在实际开发中,有时候会遇到操作图片的需求,比如说在已有的图片上添加文字,生成新的图片等需求,下面代码是我根据项目需求做的在图片上添加文字的功能:

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();
		}
	}

}
添加文字前图片:


添加文字后图片:



这样就完成了图片添加文字的需求,当然,文字添加的位置要根据项目需求去做。

核心就是使用Graphics2D的drawString、drawImage、drawLine来完成文字、图片、分割线的添加



  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Java可以使用Java2D API来实现图片文字水印。下面是一个简单的示例代码: ```java import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageWatermark { public static void main(String[] args) throws IOException { // 读取原始图片 BufferedImage image = ImageIO.read(new File("original.jpg")); // 创建一个空白的图片,大小和原始图片一样 BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); // 获取图片Graphics2D对象 Graphics2D g = result.createGraphics(); // 将原始图片绘制到空白图片上 g.drawImage(image, 0, 0, null); // 设置水印文字的相关属性 String text = "Hello, world!"; Font font = new Font("Arial", Font.BOLD, 36); Color color = Color.WHITE; // 绘制水印文字 g.setFont(font); g.setColor(color); int x = (image.getWidth() - g.getFontMetrics().stringWidth(text)) / 2; int y = image.getHeight() / 2; g.drawString(text, x, y); // 保存水印图片 ImageIO.write(result, "jpg", new File("watermark.jpg")); } } ``` 在这个示例代码中,我们首先读取原始图片,然后创建一个空白的图片,大小和原始图片一样。接着,我们获取空白图片Graphics2D对象,将原始图片绘制到空白图片上。然后,我们设置水印文字的相关属性,包括文字内容、字体和颜色,并在空白图片上绘制水印文字。最后,我们将水印图片保存到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程之艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值