Java使用图片显示电子邮件地址

今天在逛oschina的时候看见里面有一个代码分享的功能还不错,红薯老大贴出了一段代码个人觉得很实用转出来分享下。 
Java代码   收藏代码
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.FontMetrics;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.image.BufferedImage;  
  6. import java.awt.image.IndexColorModel;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.io.OutputStream;  
  10.   
  11. import javax.imageio.ImageIO;  
  12.   
  13. /** 
  14.  * 根据文本生成图片的工具 
  15.  * @author Winter Lau 
  16.  * @date 2009-7-30 下午12:58:26 
  17.  */  
  18. public class TextImageUtils {  
  19.   
  20.     private final static IndexColorModel icm = createIndexColorModel();  
  21.   
  22.     static IndexColorModel createIndexColorModel() {  
  23.         BufferedImage ex = new BufferedImage(11, BufferedImage.TYPE_BYTE_INDEXED);  
  24.         IndexColorModel icm = (IndexColorModel) ex.getColorModel();  
  25.         int SIZE = 256;  
  26.         byte[] r = new byte[SIZE];  
  27.         byte[] g = new byte[SIZE];  
  28.         byte[] b = new byte[SIZE];  
  29.         byte[] a = new byte[SIZE];  
  30.         icm.getReds(r);  
  31.         icm.getGreens(g);  
  32.         icm.getBlues(b);  
  33.         java.util.Arrays.fill(a, (byte)255);  
  34.         r[0] = g[0] = b[0] = a[0] = 0//transparent  
  35.         return new IndexColorModel(8, SIZE, r, g, b, a);  
  36.     }  
  37.       
  38.     /** 
  39.      * 生成电子邮件图片 
  40.      * @param email 
  41.      * @param out 
  42.      * @throws IOException 
  43.      */  
  44.     public static void MakeEmailImage(String email, OutputStream out) throws IOException {  
  45.         int height = 22;  
  46.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  47.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  48.         Font mFont = new Font("Verdana", Font.PLAIN, 14);  
  49.         g.setFont(mFont);  
  50.         g.drawString(email, 219);  
  51.         FontMetrics fm = g.getFontMetrics();  
  52.         int new_width = fm.charsWidth(email.toCharArray(), 0, email.length()) + 4;  
  53.         int new_height = fm.getHeight();  
  54.         BufferedImage nbi = new BufferedImage(new_width, new_height,   
  55.             BufferedImage.TYPE_BYTE_INDEXED, icm);  
  56.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  57.         g2.setColor(new Color(0,0,0,0));//透明  
  58.         g2.fillRect(0,0,new_width,new_height);  
  59.         g2.setFont(mFont);  
  60.         g2.setColor(new Color(200,0,0));  
  61.         g2.drawString(email, 2, new_height-4);  
  62.   
  63.         ImageIO.write(nbi, "gif", out);  
  64.     }  
  65.   
  66.     /** 
  67.      * 生成电话号码图片 
  68.      * @param phone 
  69.      * @param out 
  70.      * @throws IOException 
  71.      */  
  72.     public static void MakePhoneImage(String phone, OutputStream out) throws IOException {  
  73.         int height = 22;  
  74.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  75.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  76.         Font mFont = new Font("Verdana", Font.BOLD, 20);  
  77.         g.setFont(mFont);  
  78.         g.drawString(phone, 219);  
  79.         FontMetrics fm = g.getFontMetrics();  
  80.         int new_width = fm.charsWidth(phone.toCharArray(), 0, phone.length()) + 4;  
  81.         int new_height = fm.getHeight();  
  82.         BufferedImage nbi = new BufferedImage(new_width, new_height,  
  83.             BufferedImage.TYPE_BYTE_INDEXED, icm);  
  84.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  85.         g2.setColor(new Color(0,0,0,0));//透明  
  86.         g2.fillRect(0,0,new_width,new_height);  
  87.         g2.setFont(mFont);  
  88.         g2.setColor(new Color(200,0,0));  
  89.         g2.drawString(phone, 2, new_height-4);        
  90.         ImageIO.write(nbi, "gif", out);  
  91.     }  
  92.     /** 
  93.      * 生成产品关键特征 
  94.      * @param attribute 
  95.      * @param out 
  96.      * @throws IOException 
  97.      */  
  98.     public static void MakeProductAttribute(String attribute, OutputStream out) throws IOException{  
  99.         int height = 22;  
  100.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  101.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  102.         Font mFont = new Font("宋体", Font.BOLD, 13);  
  103.         g.setFont(mFont);  
  104.         g.drawString(new String(attribute), 219);  
  105.         FontMetrics fm = g.getFontMetrics();  
  106.         int new_width = fm.charsWidth(attribute.toCharArray(), 0, attribute.length()) + 4;  
  107.         int new_height = fm.getHeight();  
  108.         BufferedImage nbi = new BufferedImage(new_width, new_height,  
  109.            BufferedImage.TYPE_BYTE_INDEXED, icm);  
  110.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  111.         g2.setColor(new Color(0,0,0,0));//透明  
  112.         g2.fillRect(0,0,new_width,new_height);  
  113.         g2.setFont(mFont);  
  114.         g2.setColor(new Color(200,0,0));  
  115.         g2.drawString(attribute, 2, new_height-4);  
  116.         ImageIO.write(nbi, "gif", out);  
  117.     }  
  118.       
  119.     public static void main(String[] args) throws IOException {  
  120.         String num = "020-85551111";  
  121.         FileOutputStream fos = new FileOutputStream("D:/phone.gif");  
  122.         try{  
  123.             MakePhoneImage(num, fos);  
  124.         }finally{  
  125.             fos.close();  
  126.         }  
  127.         String email = "xxxxx@oschina.net";  
  128.         FileOutputStream fos2 = new FileOutputStream("D:/email.gif");  
  129.         try{  
  130.             MakeEmailImage(email, fos2);  
  131.         }finally{  
  132.             fos2.close();  
  133.         }  
  134.     }  
  135. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值