今天在逛oschina的时候看见里面有一个代码分享的功能还不错,红薯老大贴出了一段代码个人觉得很实用转出来分享下。
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* 根据文本生成图片的工具
* @author Winter Lau
* @date 2009-7-30 下午12:58:26
*/
public class TextImageUtils {
private final static IndexColorModel icm = createIndexColorModel();
static IndexColorModel createIndexColorModel() {
BufferedImage ex = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) ex.getColorModel();
int SIZE = 256;
byte[] r = new byte[SIZE];
byte[] g = new byte[SIZE];
byte[] b = new byte[SIZE];
byte[] a = new byte[SIZE];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
java.util.Arrays.fill(a, (byte)255);
r[0] = g[0] = b[0] = a[0] = 0; //transparent
return new IndexColorModel(8, SIZE, r, g, b, a);
}
/**
* 生成电子邮件图片
* @param email
* @param out
* @throws IOException
*/
public static void MakeEmailImage(String email, OutputStream out) throws IOException {
int height = 22;
BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
Font mFont = new Font("Verdana", Font.PLAIN, 14);
g.setFont(mFont);
g.drawString(email, 2, 19);
FontMetrics fm = g.getFontMetrics();
int new_width = fm.charsWidth(email.toCharArray(), 0, email.length()) + 4;
int new_height = fm.getHeight();
BufferedImage nbi = new BufferedImage(new_width, new_height,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g2 = (Graphics2D)nbi.getGraphics();
g2.setColor(new Color(0,0,0,0));//透明
g2.fillRect(0,0,new_width,new_height);
g2.setFont(mFont);
g2.setColor(new Color(200,0,0));
g2.drawString(email, 2, new_height-4);
ImageIO.write(nbi, "gif", out);
}
/**
* 生成电话号码图片
* @param phone
* @param out
* @throws IOException
*/
public static void MakePhoneImage(String phone, OutputStream out) throws IOException {
int height = 22;
BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
Font mFont = new Font("Verdana", Font.BOLD, 20);
g.setFont(mFont);
g.drawString(phone, 2, 19);
FontMetrics fm = g.getFontMetrics();
int new_width = fm.charsWidth(phone.toCharArray(), 0, phone.length()) + 4;
int new_height = fm.getHeight();
BufferedImage nbi = new BufferedImage(new_width, new_height,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g2 = (Graphics2D)nbi.getGraphics();
g2.setColor(new Color(0,0,0,0));//透明
g2.fillRect(0,0,new_width,new_height);
g2.setFont(mFont);
g2.setColor(new Color(200,0,0));
g2.drawString(phone, 2, new_height-4);
ImageIO.write(nbi, "gif", out);
}
/**
* 生成产品关键特征
* @param attribute
* @param out
* @throws IOException
*/
public static void MakeProductAttribute(String attribute, OutputStream out) throws IOException{
int height = 22;
BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();
Font mFont = new Font("宋体", Font.BOLD, 13);
g.setFont(mFont);
g.drawString(new String(attribute), 2, 19);
FontMetrics fm = g.getFontMetrics();
int new_width = fm.charsWidth(attribute.toCharArray(), 0, attribute.length()) + 4;
int new_height = fm.getHeight();
BufferedImage nbi = new BufferedImage(new_width, new_height,
BufferedImage.TYPE_BYTE_INDEXED, icm);
Graphics2D g2 = (Graphics2D)nbi.getGraphics();
g2.setColor(new Color(0,0,0,0));//透明
g2.fillRect(0,0,new_width,new_height);
g2.setFont(mFont);
g2.setColor(new Color(200,0,0));
g2.drawString(attribute, 2, new_height-4);
ImageIO.write(nbi, "gif", out);
}
public static void main(String[] args) throws IOException {
String num = "020-85551111";
FileOutputStream fos = new FileOutputStream("D:/phone.gif");
try{
MakePhoneImage(num, fos);
}finally{
fos.close();
}
String email = "xxxxx@oschina.net";
FileOutputStream fos2 = new FileOutputStream("D:/email.gif");
try{
MakeEmailImage(email, fos2);
}finally{
fos2.close();
}
}
}
分享到:
相关推荐
国CMS6.0加密电话EMAIL并图片显示插件 插件简介:通过加密来对电话,EMAIL进行加解密,最后以图片形式输入 。 这个作用主要是对电话,EMAIL进行加行图片显示,以防别人通过搜索引擎搜索出来这些信息。 更多的...
主要介绍了C#实现将Email地址转成图片显示的方法,涉及C#操作图片的相关技巧,需要的朋友可以参考下
JAVA实现的Email客户端,感觉还好用,有兴趣的可以研究一下
此时在下面左边的列表里面会显示出生成要下载的图片地址列表。 第六步:点击“选择存储位置”按钮,找到一个位置,用来存放配置文件及要下载的图片的位置。不要忘记文件名处要填写一个名字,自己随意起就好了。填好...
很多功能尚需完善,比如内置浏览器,以备图片下载功能验证使用;图片预览,下载/抓屏进度图片化显示等。我因为时间关系,没做到很完善,并且当抓屏图片过大并数量过多(25*25以上)时,进行图片拼接的时候内存占用问题...
此时在下面左边的列表里面会显示出生成要下载的图片地址列表。 第六步:点击“选择存储位置”按钮,找到一个位置,用来存放配置文件及要下载的图片的位置。不要忘记文件名处要填写一个名字,自己随意起就好了。填好...
个人改写的Elsevier英文论文Latex模板和使用说明,以及供Latex初学者使用Mathtype输入数学公式的方法。
3.新增上传成功后显示上传图片详细信息,并可直接查看图片和获得图片外链地址功能 4.后台新增图片外链网址修改功能 5.后台新增用户使用天数修改 6.后台新增代理折扣和金额修改功能 7.修正退出后浏览所有图片漏洞 8....
使用Outlook或者Web网页方式同时给多人发送邮件,收件人地址栏会将所有收件人的Email地址都显示出来,发出的邮件既容易进垃圾邮件,还会将其他人的Email地址都显示给别人。 电子邮件逐个发的特点在于,对于同时...
可以在插入图片时指定图片的URL地址、图片大小、对齐方式、边框粗细等。(推荐使用!) 使用“上传文件”功能,上传本地图片,上传后会直接显示出来。 在编辑HTML源代码状态下,手工输入图片代码。支持[IMG][/IMG]...
可以在插入图片时指定图片的URL地址、图片大小、对齐方式、边框粗细等。(推荐使用!) 使用“上传文件”功能,上传本地图片,上传后会直接显示出来。 在编辑HTML源代码状态下,手工输入图片代码。支持[IMG][/IMG]...
读取excel中收件人列表,转存到内存DataTable中 邮件正文采用html格式,可以发送图片,保存预定好的邮件格式 根据邀请函模板生成每个人的邀请函doc文件,转换格式为pdf文件 ...个人邮箱设置,发送步骤、进度显示
广告图片显示不了,把广告图片地址改成绝对地址就行了 本站的链接请不要删除,你可以到我的网站上提交你的网站 我们会把你的网站放在我们 [推荐网站] 上显示 [前台更新]如下: 1.增加-今日来路排行榜前10名 2....
同时,集成了TinyURL的短地址服务,让使用者更加方便。 主要功能: 1.本地/远程两种图片上传方式; 2.后台文件管理系统; 3.调整图片大小; 4.社交网站分享按钮; 5.TinyURL短地址服务; 6.支持自定义主题模板...
13.上传成功后显示上传图片详细信息,并可直接查看图片和获得图片外链地址功能 14.图片外链网址修改功能 15.用户使用天数修改 16.代理折扣和金额修改功能 17.系统使用教程和意见建议反馈 购买空间的用户模块...
Chevereto图片托管是一款非常简洁的PHP图片上传程序,可以将本地电脑上或者是网络上的图片上传到服务器上,集成了TinyURL的短地址服务,让使用者更加方便。Chevereto图片托管 3.10.13 更新日志:2018-05-24- 对每个...
5.首页图片缩略图采用目标站地址 6.增加了网站名称,关键字,描述,公告,qq,email,icp,版权等后台设置 7.修正了首页图片乱码错位问题 后台: 1.后台安全方面做了防注处理 2.添加了显示修改数据功能 3.添加了...
#题目 ...当点击“提交”按钮时,跳转到新的页面显示录入信息。 **基本要求:** 用户名为6-10个小写字母(小写使用正则式验证,且用户名不能为“wustzz” –用Ajax技术来检测);...图片要传递到新的页面显示。