java文字合成图片工具类

只能合成横排文字
话不多说直接上代码

package com.xxx.common.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.color.ColorSpace;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public class ImageUtils {

	/**
	 * description
	 * @param content 绘制的文本
	 * @param x 起始位置
	 * @param y 起始位置
	 * @param scaleWordSpace 字符间距缩放比
	 * @param graphics
	 */
	public static void drawString(String content,int x,int y,double scaleWordSpace,Graphics graphics) {
		String tempStr = "";
		int tempx = x;
		int tempy = y;
		double orgStringWith = 0.0;
		while(content.length() > 0) {
			tempStr = content.substring(0, 1);
			content = content.substring(1);
			orgStringWith = graphics.getFontMetrics().stringWidth(tempStr);
			graphics.drawString(tempStr, tempx, tempy);
			tempx = (int) Math.round(tempx + orgStringWith * scaleWordSpace);
		}
	}
	
	public static BufferedImage drawStringCenter(int width, int height, String content, Font font, Color color) {
		return drawString(width, height, BufferedImage.TYPE_INT_ARGB, content, font, color, 1.0, "center");
	}
	
	public static BufferedImage drawStringCenter(int width, int height, String content,
			Font font, Color color, double scaleWordSpace) {
		return drawString(width, height, BufferedImage.TYPE_INT_ARGB, content, font, color, scaleWordSpace, "center");
	}
	
	public static BufferedImage drawString(int width, int height, String content,
			Font font, Color color, double scaleWordSpace) {
		return drawString(width, height, BufferedImage.TYPE_INT_ARGB, content, font, color, scaleWordSpace, "left");
	}
	
	public static BufferedImage drawString(int width, int height, String content,
			Font font, Color color, double scaleWordSpace, String textAlign) {
		return drawString(width, height, BufferedImage.TYPE_INT_ARGB, content, font, color, scaleWordSpace, textAlign);
	}
	
	public static BufferedImage drawString(int width, int height, int imageType, String content,
			Font font, Color color, double scaleWordSpace) {
		return drawString(width, height, imageType, content, font, color, scaleWordSpace, "left");
	}
	
	/**
	 * description
	 * @param width 画布宽
	 * @param height 画布高
	 * @param imageType 画布类型
     * @see ColorSpace
     * @see #TYPE_INT_RGB
     * @see #TYPE_INT_ARGB
     * @see #TYPE_INT_ARGB_PRE
     * @see #TYPE_INT_BGR
     * @see #TYPE_3BYTE_BGR
     * @see #TYPE_4BYTE_ABGR
     * @see #TYPE_4BYTE_ABGR_PRE
     * @see #TYPE_BYTE_GRAY
     * @see #TYPE_USHORT_GRAY
     * @see #TYPE_BYTE_BINARY
     * @see #TYPE_BYTE_INDEXED
     * @see #TYPE_USHORT_565_RGB
     * @see #TYPE_USHORT_555_RGB
	 * @param content 绘制的文本
	 * @param font 字体
	 * @param color 字体颜色
	 * @param scaleWordSpace 字符间距缩放比
	 * @param textAlign 文本对齐方式, left, center, right
	 * @return
	 */
	public static BufferedImage drawString(int width, int height, int imageType, String content,
			Font font, Color color, double scaleWordSpace, String textAlign) {
		BufferedImage image = new BufferedImage(width, height, imageType);
		Graphics2D graphics = image.createGraphics();
		graphics.setColor(color);
		graphics.setFont(font);
		graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		FontMetrics fontMetrics = new FontMetrics(font) {
			private static final long serialVersionUID = 1L;};
		Rectangle2D bounds = fontMetrics.getStringBounds(content, graphics);
		int boundsHeight = (int) bounds.getHeight();
		int x = 0;
		if ("left".equalsIgnoreCase(textAlign)) {
			x = 2;
		} else if ("center".equalsIgnoreCase(textAlign)) {
			x = (int) ((width - scaleWordSpace * graphics.getFontMetrics().stringWidth(content)) / 2);
		} else if ("right".equalsIgnoreCase(textAlign)) {
			x = (int) (width - scaleWordSpace * graphics.getFontMetrics().stringWidth(content) - 4);
		}
		int y = Math.round((height + boundsHeight - font.getSize() * 1.0f / 2) / 2 - 2);
		drawString(content, x, y, scaleWordSpace, graphics);
		graphics.dispose();
		image.flush();
		return image;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值