package com.image;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public final class ImageUtils {
	private static final String PICTRUE_FORMATE_JPG = "jpg";

	private ImageUtils() {
	}

	public static void pressText(String targetImg, String pressText1,
			String pressText2, String pressText3, int fontSize, Color color) {
		try {
			File file = new File(targetImg);
			File dstFile = new File(targetImg + "_dst.jpg");

			Image image = ImageIO.read(file);
			int width = image.getWidth(null);
			int height = image.getHeight(null);
			BufferedImage bufferedImage = new BufferedImage(width, height,
					BufferedImage.TYPE_INT_RGB);
			Graphics2D g = bufferedImage.createGraphics();
			g.drawImage(image, 0, 0, width, height, null);
			g.setFont(new Font("微软雅黑", Font.BOLD, fontSize));
			g.setColor(color);
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
					1));
			int x = 135;
			int y = 163;
			int width_1 = fontSize * getLength(pressText1);
			int height_1 = fontSize;
			int widthDiff = width - width_1;
			int heightDiff = height - height_1;
			if (x < 0) {
				x = widthDiff / 2;
			} else if (x > widthDiff) {
				x = widthDiff;
			}
			if (y < 0) {
				y = heightDiff / 2;
			} else if (y > heightDiff) {
				y = heightDiff;
			}

			g.drawString(pressText1, x, y + height_1);

			x = 135;
			y = 355;
			width_1 = fontSize * getLength(pressText1);
			height_1 = fontSize;
			widthDiff = width - width_1;
			heightDiff = height - height_1;
			if (x < 0) {
				x = widthDiff / 2;
			} else if (x > widthDiff) {
				x = widthDiff;
			}
			if (y < 0) {
				y = heightDiff / 2;
			} else if (y > heightDiff) {
				y = heightDiff;
			}
			g.drawString(pressText2, x, y + height_1);

			x = 135;
			y = 550;
			width_1 = fontSize * getLength(pressText1);
			height_1 = fontSize;
			widthDiff = width - width_1;
			heightDiff = height - height_1;
			if (x < 0) {
				x = widthDiff / 2;
			} else if (x > widthDiff) {
				x = widthDiff;
			}
			if (y < 0) {
				y = heightDiff / 2;
			} else if (y > heightDiff) {
				y = heightDiff;
			}
			g.drawString(pressText3, x, y + height_1);

			g.dispose();
			ImageIO.write(bufferedImage, PICTRUE_FORMATE_JPG, dstFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static int getLength(String text) {
		int textLength = text.length();
		int length = textLength;
		for (int i = 0; i < textLength; i++) {
			if (String.valueOf(text.charAt(i)).getBytes().length > 1) {
				length++;
			}
		}
		return (length % 2 == 0) ? length / 2 : length / 2 + 1;
	}

	public static void main(String[] args) throws IOException {
		String taijiongPath = "C:/taijiong.jpg";
		pressText(taijiongPath, "你想加入java牛的java版微博开发活动weibo4j么?", "还差1牛币?",
				"啊哈哈哈哈!!!现在涨成100000牛币了!!!", 18, Color.WHITE);
	}
}
最近下载更多
Fzhou  LV1 2021年6月20日
dafeiyu  LV10 2021年4月15日
firstronin  LV2 2020年12月13日
126150  LV1 2019年12月12日
2252536772  LV21 2019年6月21日
Destiny微斯人  LV6 2018年7月25日
就是这么强大啊  LV2 2018年5月28日
kongzx  LV4 2018年4月18日
shanshilong  LV19 2018年4月10日
liu164053  LV5 2017年12月27日
最近浏览更多
wuziayng1232  LV10 2022年7月8日
1793004086  LV1 2022年4月1日
Fzhou  LV1 2021年6月20日
shown866 2021年6月6日
暂无贡献等级
dafeiyu  LV10 2021年4月15日
qiubinbin 2021年3月10日
暂无贡献等级
2196316269  LV10 2021年2月24日
北站扯淡  LV10 2020年12月14日
firstronin  LV2 2020年12月13日
王少shao  LV1 2020年10月12日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友