亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Java使用圖片顯示電子郵件地址

系統 1860 0

今天在逛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( 1 ,? 1 ,?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,? 2 ,? 19 );??
  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,? 2 ,? 19 );??
  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),? 2 ,? 19 );??
  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. }??

Java使用圖片顯示電子郵件地址


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 永久黄网站色视频免费观看 | 国产一区二区久久 | 国产亚洲精品色一区 | 日韩社区 | 久99频这里只精品23热 视频 | 亚洲一区欧洲一区 | 久久春色 | 欧美123| 亚洲你xx我xx网站 | 午夜欧美精品久久久久久久 | 色综合久久中文字幕网 | 日韩欧美天堂 | 欧美精品亚洲精品 | 美女超逼 | 亚洲男人天堂久久 | 三人性free孕交欧美 | 国产乱仑| 欧美午夜性刺激在线观看免费 | 91日韩视频 | 久久网欧美 | 一本到视频在线观看 | 毛片网站在线 | 国产69精品久久久久99不卡 | 成人性毛片 | 国产一区二区三区高清视频 | 久久久久蜜桃 | 欧美中文字幕在线播放 | 色天使色婷婷在线影院亚洲 | 国产精品视频国产永久视频 | 成人看的午夜免费毛片 | 性久久 | 高清国产美女一级a毛片录 高清国产美女一级毛片 | 日本韩国欧美一区 | 午夜国产在线 | 日本一级高清不卡视频在线 | 九色综合网 | 特黄a大片免费视频 | 亚洲无总热门 | 亚洲精品久久久久网站 | 四虎午夜剧场 | 亚洲曰本大成网站mmm |