只能合成横排文字
话不多说直接上代码
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;
}
}