Graphics2D 制图

暗夜螃蟹 2010-11-23 05:45:21
现在项目想用Graphics2D来生成图片,来做贺卡的接口;
1、要求文字去毛刺。
2、要求文字以双字节接收(因为要把接收到的文字一个个读取出来,要文字的大小一样。所以统一一下);
3、要求文字的背景色为透明色,或者透明通道;


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);

}

}




求高手解决呀!~加急!~
...全文
496 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗夜螃蟹 2010-11-24
  • 打赏
  • 举报
回复

这个图片中的数字,要和汉字所占的位置一样,如何解决呀。。。也就是单双字节的问题。。
暗夜螃蟹 2010-11-24
  • 打赏
  • 举报
回复

非常感谢sunyiz的帮助!~~

sunyiz 2010-11-24
  • 打赏
  • 举报
回复
只改一个方法,试试

  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;
}
}

暗夜螃蟹 2010-11-24
  • 打赏
  • 举报
回复
我应用的字体是:汉仪双线简体
你可以用新宋体测试
sunyiz 2010-11-24
  • 打赏
  • 举报
回复
滴汗……
这个样子我可以试试,不过只能保证在这个字体下有这个效果,
换个字体 我就说不准啦
暗夜螃蟹 2010-11-24
  • 打赏
  • 举报
回复
刚试了一下你的程序:如图

还不是很好,要每个数字和汉字占的大小一样就完美了。
比如:每个汉字占50*50像素的位置,
每个数字或者字母也是像汉字一样的在50*50像素的位置占满
想要的效果:
如:
sunyiz 2010-11-24
  • 打赏
  • 举报
回复
改了一个,你试试吧

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);

}

}
ace62 2010-11-24
  • 打赏
  • 举报
回复
显示时,将数字转换成全角方式输出。就可实现等宽
暗夜螃蟹 2010-11-23
  • 打赏
  • 举报
回复
对的
sunyiz 2010-11-23
  • 打赏
  • 举报
回复
额……这样啊,就是说中文字符的宽度和英文字符/数字不一样,所以要统一是吧?
暗夜螃蟹 2010-11-23
  • 打赏
  • 举报
回复
2、图片生成的文字要大小一样的;比如:50*50像素的大小的;但成生的时候别人输入数字 1 只占了一个字符相当于,并没有在50*50中间; “中国”的中字占的字符刚好在50*50中间;这个问题不知道看明白没
sunyiz 2010-11-23
  • 打赏
  • 举报
回复
2、表示没看明白&……
sunyiz 2010-11-23
  • 打赏
  • 举报
回复
3、要求文字的背景色为透明色
我只知道一个方法,就是你以一个完全是透明像素的图片(png)
创建一个BufferedImage
		try {
bi = ImageIO.read(URL);
} catch (IOException e1) {
e1.printStackTrace();
}

而不是bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
这样生成

因为我试过用BufferedImage所有的参数企图生成一个透明的BufferedImage,都没有成功
sunyiz 2010-11-23
  • 打赏
  • 举报
回复
1、要求文字去毛刺。
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
开文字抗锯齿

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧