JAVA Graphics2D 制图 透明色

暗夜螃蟹 2010-11-26 10:41:12


//JAVA 用 Graphics2D 制图 想把生成的图片变成透明色,不知道如何实现;请教高手!~~
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"+getDate()+".png";

File file = new File(filepath);

// Font font = new Font("甲骨文", 36, fontSize);

System.out.println("topic=" + text);
Font font = new Font("汉仪双线简体", Font.BOLD, fontSize);
// Font font = new Font("新宋体", Font.BOLD, fontSize);
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) bi.getGraphics();
//g2.setBackground(new Color(0,0xFF,0xFF,0xFF));
g2.setBackground(new Color(0,0xFF,0xFF,0xFF));
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.clearRect(0, 0, width, height);
g2.setFont(font);
g2.setPaint(Color.black);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.3f));
//
AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER , 0.5f);
g2.setComposite(ac);


// g2.drawString(text, fontSize, fontSize);
paintString(g2, text,2,42, fontSize);
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());
}

private static void paintString(Graphics2D g2d, String str, int x, int y,
int fontSize) {
FontMetrics metrics = g2d.getFontMetrics();
for (char ca : str.toCharArray()) {
int px = metrics.stringWidth("" + ca);
g2d.drawString("" + ca, x + (fontSize - px) / 2, y);
x += fontSize;
}
}

public static Date parsePlainDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

return sdf.parse(source, new ParsePosition(0));
}


public static void main(String[] args) throws IOException, ParseException {
// pressText("我是文字", "d:\\1.bmp", "黑体", 36, Color.white, 80, 0, 0,
// 0.3f);
TextToPic("我是文字", 500, 100, 50);
//
System.out.print(parsePlainDate(getDate()));
}

}


...全文
682 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗夜螃蟹 2010-11-26
  • 打赏
  • 举报
回复

真是太谢谢了
sunyiz 2010-11-26
  • 打赏
  • 举报
回复
你用ps打开看看……
别用window自带的画图工具……那个是不能编辑透明像素的
暗夜螃蟹 2010-11-26
  • 打赏
  • 举报
回复
生成的是黑色的背景。。。。
暗夜螃蟹 2010-11-26
  • 打赏
  • 举报
回复

总是这么急时出现,哇哈哈
sunyiz 2010-11-26
  • 打赏
  • 举报
回复
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
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"+getDate()+".png";

File file = new File(filepath);

// Font font = new Font("甲骨文", 36, fontSize);

System.out.println("topic=" + text);
Font font = new Font("汉仪双线简体", Font.BOLD, fontSize);
// Font font = new Font("新宋体", Font.BOLD, fontSize);
BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = (Graphics2D) bi.getGraphics();

g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.setFont(font);
g2.setPaint(Color.black);

paintString(g2, text,2,42, fontSize);
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());
}

private static void paintString(Graphics2D g2d, String str, int x, int y,
int fontSize) {
FontMetrics metrics = g2d.getFontMetrics();
for (char ca : str.toCharArray()) {
int px = metrics.stringWidth("" + ca);
g2d.drawString("" + ca, x + (fontSize - px) / 2, y);
x += fontSize;
}
}

public static Date parsePlainDate(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

return sdf.parse(source, new ParsePosition(0));
}


public static void main(String[] args) throws IOException, ParseException {
// pressText("我是文字", "d:\\1.bmp", "黑体", 36, Color.white, 80, 0, 0,
// 0.3f);
TextToPic("我是文字", 500, 100, 50);
//
System.out.print(parsePlainDate(getDate()));
}

}


貌似这样就OK了…………

81,092

社区成员

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

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