天天看點

Java使用圖檔顯示電子郵件位址

今天在逛oschina的時候看見裡面有一個代碼分享的功能還不錯,蕃薯老大貼出了一段代碼個人覺得很實用轉出來分享下。 

Java代碼  

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. import javax.imageio.ImageIO;  
  11. public class TextImageUtils {  
  12.     private final static IndexColorModel icm = createIndexColorModel();  
  13.     static IndexColorModel createIndexColorModel() {  
  14.         BufferedImage ex = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);  
  15.         IndexColorModel icm = (IndexColorModel) ex.getColorModel();  
  16.         int SIZE = 256;  
  17.         byte[] r = new byte[SIZE];  
  18.         byte[] g = new byte[SIZE];  
  19.         byte[] b = new byte[SIZE];  
  20.         byte[] a = new byte[SIZE];  
  21.         icm.getReds(r);  
  22.         icm.getGreens(g);  
  23.         icm.getBlues(b);  
  24.         java.util.Arrays.fill(a, (byte)255);  
  25.         r[0] = g[0] = b[0] = a[0] = 0; //transparent  
  26.         return new IndexColorModel(8, SIZE, r, g, b, a);  
  27.     }  
  28.     public static void MakeEmailImage(String email, OutputStream out) throws IOException {  
  29.         int height = 22;  
  30.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  31.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  32.         Font mFont = new Font("Verdana", Font.PLAIN, 14);  
  33.         g.setFont(mFont);  
  34.         g.drawString(email, 2, 19);  
  35.         FontMetrics fm = g.getFontMetrics();  
  36.         int new_width = fm.charsWidth(email.toCharArray(), 0, email.length()) + 4;  
  37.         int new_height = fm.getHeight();  
  38.         BufferedImage nbi = new BufferedImage(new_width, new_height,   
  39.             BufferedImage.TYPE_BYTE_INDEXED, icm);  
  40.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  41.         g2.setColor(new Color(0,0,0,0));//透明  
  42.         g2.fillRect(0,0,new_width,new_height);  
  43.         g2.setFont(mFont);  
  44.         g2.setColor(new Color(200,0,0));  
  45.         g2.drawString(email, 2, new_height-4);  
  46.         ImageIO.write(nbi, "gif", out);  
  47.     }  
  48.     public static void MakePhoneImage(String phone, OutputStream out) throws IOException {  
  49.         int height = 22;  
  50.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  51.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  52.         Font mFont = new Font("Verdana", Font.BOLD, 20);  
  53.         g.setFont(mFont);  
  54.         g.drawString(phone, 2, 19);  
  55.         FontMetrics fm = g.getFontMetrics();  
  56.         int new_width = fm.charsWidth(phone.toCharArray(), 0, phone.length()) + 4;  
  57.         int new_height = fm.getHeight();  
  58.         BufferedImage nbi = new BufferedImage(new_width, new_height,  
  59.             BufferedImage.TYPE_BYTE_INDEXED, icm);  
  60.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  61.         g2.setColor(new Color(0,0,0,0));//透明  
  62.         g2.fillRect(0,0,new_width,new_height);  
  63.         g2.setFont(mFont);  
  64.         g2.setColor(new Color(200,0,0));  
  65.         g2.drawString(phone, 2, new_height-4);        
  66.         ImageIO.write(nbi, "gif", out);  
  67.     }  
  68.     public static void MakeProductAttribute(String attribute, OutputStream out) throws IOException{  
  69.         int height = 22;  
  70.         BufferedImage bi = new BufferedImage(255,height,BufferedImage.TYPE_INT_RGB);          
  71.         Graphics2D g = (Graphics2D)bi.getGraphics();  
  72.         Font mFont = new Font("宋體", Font.BOLD, 13);  
  73.         g.setFont(mFont);  
  74.         g.drawString(new String(attribute), 2, 19);  
  75.         FontMetrics fm = g.getFontMetrics();  
  76.         int new_width = fm.charsWidth(attribute.toCharArray(), 0, attribute.length()) + 4;  
  77.         int new_height = fm.getHeight();  
  78.         BufferedImage nbi = new BufferedImage(new_width, new_height,  
  79.            BufferedImage.TYPE_BYTE_INDEXED, icm);  
  80.         Graphics2D g2 = (Graphics2D)nbi.getGraphics();  
  81.         g2.setColor(new Color(0,0,0,0));//透明  
  82.         g2.fillRect(0,0,new_width,new_height);  
  83.         g2.setFont(mFont);  
  84.         g2.setColor(new Color(200,0,0));  
  85.         g2.drawString(attribute, 2, new_height-4);  
  86.         ImageIO.write(nbi, "gif", out);  
  87.     }  
  88.     public static void main(String[] args) throws IOException {  
  89.         String num = "020-85551111";  
  90.         FileOutputStream fos = new FileOutputStream("D:/phone.gif");  
  91.         try{  
  92.             MakePhoneImage(num, fos);  
  93.         }finally{  
  94.             fos.close();  
  95.         }  
  96.         String email = "[email protected]";  
  97.         FileOutputStream fos2 = new FileOutputStream("D:/email.gif");  
  98.         try{  
  99.             MakeEmailImage(email, fos2);  
  100.         }finally{  
  101.             fos2.close();  
  102.         }  
  103.     }  
  104. }