用java將文字輸出為圖片示例
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import javax.imageio.*;
public class txt2img {
public static void main(String[] args) throws Exception {
String text = args[0];
String family = args[1];
String size = args[2];
String bold = args[3];
String italic = args[4];
String underline = args[5];
String fcolor = args[6];
String bcolor = args[7];
String filename = args[8];
HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
// 定義字體
if (family.trim().equals("") || family == null || family == "default") {
hm.put(TextAttribute.FAMILY, "Simsun");
} else {
hm.put(TextAttribute.FAMILY, family);
}
// 定義字號
if (size.trim().equals("") || size == null) {
hm.put(TextAttribute.SIZE, 18);
} else {
hm.put(TextAttribute.SIZE, Integer.parseInt(size));
}
// System.out.println("size: " + size);
// 定義是否粗體
if (bold.trim().equals("1")) {
// System.out.println("bold");
hm.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
// 定義是否斜體
if (italic.trim().equals("1")) {
// System.out.println("italic");
hm.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
}
// 定義是否有下劃線
if (underline.trim().equals("1")) {
// System.out.println("underline");
hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
}
// 設置字體顏色
if (fcolor.indexOf(",") == -1 || fcolor == null) {
hm.put(TextAttribute.FOREGROUND, new Color(0, 0, 0));
} else {
String[] fc = fcolor.split(",");
hm.put(TextAttribute.FOREGROUND, new Color(Integer.parseInt(fc[0]), Integer.parseInt(fc[1]), Integer.parseInt(fc[2])));
}
// 設置背景顏色
if (bcolor.indexOf(",") > -1 && bcolor != null) {
String[] bc = bcolor.split(",");
hm.put(TextAttribute.BACKGROUND, new Color(Integer.parseInt(bc[0]), Integer.parseInt(bc[1]), Integer.parseInt(bc[2])));
}
Font font = new Font(hm);
createImage(text, font, new File(filename));
}
// 根據str,font的樣式以及輸出文件目錄
public static void createImage(String str, Font font, File outFile) throws Exception{
// 獲取font的樣式應用在str上的整個矩形
Rectangle2D r = font.getStringBounds(str, new FontRenderContext(AffineTransform.getScaleInstance(1, 1), false, false));
int unitHeight = (int)Math.floor(r.getHeight()) + 1; // 獲取單個字符的高度
// 獲取整個str用了font樣式的寬度這裡用四捨五入後+1保證寬度絕對能容納這個字符串作為圖片的寬度
int width = (int)Math.round(r.getWidth()) + 1;
int height = unitHeight + 3; // 把單個字符的高度+3保證高度絕對能容納字符串作為圖片的高度
// 創建圖片
BufferedImage imageb = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = imageb.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 反鋸齒
g.setFont(font); // 設置畫筆字體
g.drawString(str, 0, font.getSize()); // 畫出字符串
g.dispose();
ImageIO.write(imageb, "png", outFile); // 輸出png圖片
}
}