81,092
社区成员
发帖
与我相关
我的任务
分享
public class WordToPic {
//
public static int getLength(String text) {
int length = 0;
for (int i = 0; i < text.length(); i++) {
if (new String(text.charAt(i) + "").getBytes().length > 1) {
length += 2;
} else {
length += 1;
}
}
return length / 2;
}
public static String TextToPic(String text,int width,int height,int fontSize){
try {
String filepath="D:/apache-tomcat-5.5.17/webapps/webapp/UserFiles/Image/texttopic/image"+getDate()+".png";
//String filepath="/www/newaudi_cms/app/UserFiles/Image/texttopic/image.png";
File file = new File(filepath);
//Font font = new Font("甲骨文", 36, fontSize);
System.out.println("topic="+text);
Font font = new Font("方正平和简体", Font.BOLD, fontSize);
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(Color.WHITE);
g2.clearRect(0, 0, width, height);
g2.setFont(font);
g2.setPaint(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));
g2.drawString(text, (width - (getLength(text) * fontSize)) / 2 + 0, (height - fontSize) / 2 + 40);
g2.dispose();
ImageIO.write(bi, "png", file);
return "image"+getDate()+".png";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public static String getDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(new Date());
}
public static void main(String[] args) throws IOException {
//pressText("我是文字", "d:\\1.bmp", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
TextToPic("我是文字",500,100,50);
}
}
private static void paintString(Graphics2D g2d, String str, int x, int y, int fontSize) {
FontMetrics metrics = SwingUtilities2.getFontMetrics(null,g2d.getFont());
for (char ca : str.toCharArray()) {
int px = metrics.stringWidth(""+ca);
g2d.drawString(""+ca, x+(fontSize-px)/2, y);
x += fontSize;
}
}
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
public class WordToPic {
//
public static int getLength(String text) {
int length = 0;
for (int i = 0; i < text.length(); i++) {
if (new String(text.charAt(i) + "").getBytes().length > 1) {
length += 2;
} else {
length += 1;
}
}
return length / 2;
}
public static String TextToPic(String text,int width,int height,int fontSize){
try {
String filepath="D:/apache-tomcat-5.5.17/webapps/webapp/UserFiles/Image/texttopic/image"+getDate()+".png";
//String filepath="/www/newaudi_cms/app/UserFiles/Image/texttopic/image.png";
File file = new File(filepath);
//Font font = new Font("甲骨文", 36, fontSize);
System.out.println("topic="+text);
Font font = new Font("方正平和简体", Font.BOLD, fontSize);
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
g2.setBackground(null);
g2.clearRect(0, 0, width, height);
g2.setFont(font);
g2.setPaint(Color.BLACK);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.3f));
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
paintString(g2, text, (width - (text.length() * fontSize)) / 2 + 0, (height - fontSize) / 2 + 40, fontSize);
g2.dispose();
ImageIO.write(bi, "png", file);
return "image"+getDate()+".png";
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
private static void paintString(Graphics2D g2d, String str, int x, int y, int fontSize) {
for (char ca : str.toCharArray()) {
g2d.drawString(""+ca, x, y);
x += fontSize;
}
}
public static String getDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
return formatter.format(new Date());
}
public static void main(String[] args) throws IOException {
//pressText("我是文字", "d:\\1.bmp", "黑体", 36, Color.white, 80, 0, 0, 0.3f);
TextToPic("我是文字1234",500,100,50);
}
}
try {
bi = ImageIO.read(URL);
} catch (IOException e1) {
e1.printStackTrace();
}