JAVA 将文字转换为图片

eggtest 2011-07-07 02:47:14
//将文字转换为图片
public static boolean saveStringToPic(String content, String filePath) {
boolean rs;
int width = 1024; //300;
int height = 600;
int lineLength = 47;//23;
int lineLength2 = 47;//24;
String content1 = content;
String content2 = "";
String content3 = "";
int cnLineLength = lineLength;
for (int i = 0;i < content.length();i++) {
char ca = content.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength = cnLineLength - 1;
}
if (i >= cnLineLength) break;
}
if (content.length() > cnLineLength) {
content1 = content.substring(0, cnLineLength);
content2 = content.substring(cnLineLength);
String line2 = content2;
int cnLineLength2 = lineLength2;
for (int i = 0;i < line2.length();i++) {
char ca = line2.charAt(i);
if ((ca < '0' || ca > '9') && ca != '-' && ca != ':' && ca != '(' && ca != ')' && ca != '.' && ca != ',' && ca != ' ') {
cnLineLength2 = cnLineLength2 - 1;
}
if (i >= cnLineLength2) break;
}
if (line2.length() > cnLineLength2) {
content2 = line2.substring(0, cnLineLength2);
content3 = line2.substring(cnLineLength2);
}
}
BufferedImage bi = null;
Graphics2D g2 = null;
OutputStream outPutStream = null;
try{
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2 = (Graphics2D) bi.getGraphics();
//g2.setBackground(Color.white);
Color bg = new Color(255, 255, 255);
g2.setColor(bg);
g2.fillRect(0, 0, width, height);
g2.setColor(Color.black);
Font font = new Font("宋体", Font.BOLD, 40);
g2.setFont(font);
g2.drawString(content1, 10, 200);
g2.drawString(content2, 10, 300);
g2.drawString(content3, 10, 400);
outPutStream = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outPutStream);
encoder.encode(bi);
rs = true;
} catch (Exception e) {
e.printStackTrace();
rs = false;
} finally {
if (g2 != null){
g2.dispose();
}
if (outPutStream != null) {
try {
outPutStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return rs;
}
...全文
722 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhfy616211 2011-08-12
  • 打赏
  • 举报
回复
LZ的方法能行吗
晓猪 2011-08-12
  • 打赏
  • 举报
回复
继续关注中···
当时汉就来了 2011-07-18
  • 打赏
  • 举报
回复
不赖 又学到了!
宁波朱超 2011-07-18
  • 打赏
  • 举报
回复
回答 不知所云~ 也能得分 我日哦
大_爱 2011-07-18
  • 打赏
  • 举报
回复
偶表示不会。嘿嘿。。。
代碼一族 2011-07-18
  • 打赏
  • 举报
回复
将一段文字转换为图片 -? 我没见过 - - 楼主你会吗? 告诉我?
零起跑线 2011-07-08
  • 打赏
  • 举报
回复
你这是在问问题还在送思路和代码
[Quote=引用 2 楼 eggtest 的回复:]
怎么不知所云,就是将一段文字转换为图片保存起来.
[/Quote]
quanlei1507053 2011-07-08
  • 打赏
  • 举报
回复
验证码?
nourewang 2011-07-07
  • 打赏
  • 举报
回复
lz是传授知识呢?还是传授知识呢?
dyflovexlp 2011-07-07
  • 打赏
  • 举报
回复
ImageIO.write方法
宁波朱超 2011-07-07
  • 打赏
  • 举报
回复
完全可以,否则 注册的时候那验证码怎么来的???

不就是随机的字符 整成图片显示的嘛

不过楼主0回帖率。。。。。。
eggtest 2011-07-07
  • 打赏
  • 举报
回复
怎么不知所云,就是将一段文字转换为图片保存起来.
zn85600301 2011-07-07
  • 打赏
  • 举报
回复
不知所云~

81,094

社区成员

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

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