Java版颈椎自动矫正图

系统 1585 0

本来上周双休日没什么事情,准备干点遗留了很久的正经事,结果半途忍不住手贱看新闻,于是被11.28事件触动,随大流的针对棒子及棒子粉抽疯了两天。

没想到到星期一上班脖子就不得劲,顺便查了点资料,发现个山寨版的颈椎矫正图,觉得挺有意思。

如下图:

Java版颈椎自动矫正图

于是回家后想到自己也做个玩。

问题是,咱爷们不说程序员吧,好歹也是个垒码的,直接PS文字图未免有碍观瞻,于是抽空写了个Java自动生成版的。

代码如下:

  1. package org.test;

  2. import java.awt.AlphaComposite;
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import java.awt.Frame;
  7. import java.awt.Graphics;
  8. import java.awt.Graphics2D;
  9. import java.awt.Image;
  10. import java.awt.RenderingHints;
  11. import java.awt.event.WindowAdapter;
  12. import java.awt.event.WindowEvent;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.IOException;

  16. import javax.imageio.ImageIO;

  17. /**
  18. *Copyright2008
  19. *
  20. *LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
  21. *usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
  22. *theLicenseat
  23. *
  24. *http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  27. *distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
  28. *WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
  29. *Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
  30. *theLicense.
  31. *
  32. *@projectloonframework
  33. *@authorchenpeng
  34. *@email:ceponline@yahoo.com.cn
  35. *@version0.1
  36. */
  37. public class MessageImage extends Canvas{

  38. /**
  39. *
  40. */
  41. private static final long serialVersionUID=1L;

  42. private BufferedImagefontImage;

  43. private Graphics2Dg2d;
  44. private ImagebackImage;
  45. final static private int WIDTH= 600 ;

  46. final static private int HEIGHT= 480 ;


  47. public MessageImage( final Stringmessages){

  48. fontImage= new BufferedImage(WIDTH,HEIGHT, 2 );
  49. g2d=fontImage.createGraphics();
  50. try {
  51. backImage=ImageIO.read( new File( "back.png" ));
  52. } catch (IOExceptione){
  53. e.printStackTrace();
  54. }
  55. g2d.drawImage(backImage, 0 , 0 , null );
  56. setAlpha(g2d, 0.7 );
  57. int size= 25 ;
  58. int newLine=(WIDTH/size)- 10 ;
  59. char []messageChars=messages.toCharArray();
  60. boolean d= true ;
  61. StringBuildersbr= new StringBuilder();
  62. int count= 0 ;
  63. int len=messageChars.length- 1 ;
  64. StringfontStyle= "幼圆" ;
  65. Colorcolor=Color.white;
  66. for ( int i= 0 ,j= 0 ;i<=len;i++,j++){
  67. sbr.append(messageChars[i]);
  68. if (j==newLine||(messageChars[i]== '/n' )){
  69. g2d.drawImage(createImageMessages( 1 , false ,sbr.toString(),
  70. color,fontStyle, 1 ,size,d),count+= 30 ,
  71. (HEIGHT-(sbr.length()*size))-(size* 4 ), null );
  72. d=!d;
  73. sbr.delete( 0 ,sbr.length());
  74. j= 0 ;
  75. } else if (i==len){
  76. g2d.drawImage(createImageMessages( 1 , false ,sbr.toString(),
  77. color,fontStyle, 1 ,size,d),count+= 30 ,
  78. (HEIGHT-(sbr.length()*size))-(size* 4 ), null );
  79. }
  80. }
  81. setAlpha(g2d, 0.6 );
  82. Stringmes= "Java版颈椎自动矫正图" ;
  83. fontStyle= "华文新魏" ;
  84. size= 20 ;
  85. g2d.drawImage(createImageMessages( 0 , true ,mes,Color.red,fontStyle,
  86. 1 ,size, false ),WIDTH-(mes.length()*size)-(size* 2 ),
  87. HEIGHT-(size* 2 ), null );
  88. }

  89. /**
  90. *创建一组图片文字
  91. *
  92. *@paramaspect
  93. *@paramisRow
  94. *@parammessages
  95. *@paramcolor
  96. *@paramname
  97. *@paramstyle
  98. *@paramsize
  99. *@paramd
  100. *@return
  101. */
  102. public static BufferedImagecreateImageMessages( final int aspect,
  103. final boolean isRow, final Stringmessages, final Colorcolor,
  104. final Stringname, final int style, final int size, final boolean d){
  105. final int flength=messages.length();
  106. final int nowSize=size*flength;
  107. BufferedImagefontImages;
  108. if (isRow)
  109. fontImages= new BufferedImage(nowSize,size, 2 );
  110. else
  111. fontImages= new BufferedImage(size,nowSize, 2 );
  112. Graphics2Dgraphics2d=fontImages.createGraphics();
  113. char []messageChars=messages.toCharArray();
  114. if (d){
  115. char []temp= new char [flength];
  116. for ( int i= 0 ,j=flength- 1 ;i<flength;i++,j--){
  117. temp[j]=messageChars[i];
  118. }
  119. messageChars=temp;
  120. }
  121. if (isRow)
  122. for ( int i= 0 ;i<flength;i++){
  123. graphics2d.drawImage(createImageMessage(aspect,
  124. messageChars[i],color,name,style,size,d),
  125. i*size, 0 , null );
  126. }
  127. else
  128. for ( int i= 0 ;i<flength;i++){
  129. graphics2d.drawImage(createImageMessage(aspect,
  130. messageChars[i],color,name,style,size,d), 0 ,i
  131. *size, null );
  132. }
  133. graphics2d.dispose();
  134. System.gc();
  135. return fontImages;
  136. }

  137. /**
  138. *创建单独图片文字
  139. *
  140. *@paramaspect
  141. *@parammessage
  142. *@paramcolor
  143. *@paramname
  144. *@paramstyle
  145. *@paramsize
  146. *@paramd
  147. *@return
  148. */
  149. public static BufferedImagecreateImageMessage( final int aspect,
  150. final char message, final Colorcolor, final Stringname,
  151. final int style, final int size, final boolean d){
  152. final int nowSize=size+ 1 ;
  153. BufferedImagefontImage= new BufferedImage(nowSize,nowSize, 2 );
  154. Graphics2Dgraphics2d=fontImage.createGraphics();
  155. graphics2d.setColor(color);
  156. graphics2d.setFont( new Font(name,style,size));
  157. //设定图像显示状态
  158. RenderingHintshints= new RenderingHints(
  159. RenderingHints.KEY_TEXT_ANTIALIASING,
  160. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  161. hints.put(RenderingHints.KEY_DITHERING,
  162. RenderingHints.VALUE_DITHER_ENABLE);
  163. hints.put(RenderingHints.KEY_RENDERING,
  164. RenderingHints.VALUE_RENDER_SPEED);
  165. hints.put(RenderingHints.KEY_ANTIALIASING,
  166. RenderingHints.VALUE_ANTIALIAS_ON);
  167. hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
  168. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  169. hints.put(RenderingHints.KEY_COLOR_RENDERING,
  170. RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  171. hints.put(RenderingHints.KEY_DITHERING,
  172. RenderingHints.VALUE_DITHER_DISABLE);
  173. graphics2d.setRenderingHints(hints);
  174. graphics2d.drawString(String.valueOf(message), 0 ,size- 3 );
  175. graphics2d.dispose();
  176. switch (aspect){
  177. case 0 :
  178. break ;
  179. case 1 :
  180. fontImage=MessageImage.rotateImage(fontImage, 90 ,d);
  181. break ;
  182. case 2 :
  183. fontImage=MessageImage.rotateImage(fontImage, 180 ,d);
  184. break ;
  185. case 3 :
  186. fontImage=MessageImage.rotateImage(fontImage, 360 ,d);
  187. break ;
  188. }
  189. return fontImage;
  190. }

  191. public void paint(Graphicsg){
  192. g.drawImage(fontImage, 0 , 0 , null );
  193. }

  194. /**
  195. *水平翻转当前图像
  196. *
  197. *@return
  198. */
  199. public static BufferedImagerotateImage( final BufferedImageimage){
  200. int w=image.getWidth();
  201. int h=image.getHeight();
  202. BufferedImageimg;
  203. Graphics2Dgraphics2d;
  204. (graphics2d=(img= new BufferedImage(w,h,image.getColorModel()
  205. .getTransparency())).createGraphics()).drawImage(image, 0 , 0 ,
  206. w,h,w, 0 , 0 ,h, null );
  207. graphics2d.dispose();
  208. return img;
  209. }

  210. /**
  211. *旋转图像为指定角度
  212. *
  213. *@paramdegree
  214. *@return
  215. */
  216. public static BufferedImagerotateImage( final BufferedImageimage,
  217. final int angdeg, final boolean d){
  218. int w=image.getWidth();
  219. int h=image.getHeight();
  220. int type=image.getColorModel().getTransparency();
  221. BufferedImageimg;
  222. Graphics2Dgraphics2d;
  223. (graphics2d=(img= new BufferedImage(w,h,type)).createGraphics())
  224. .setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  225. RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  226. graphics2d.rotate(d?-Math.toRadians(angdeg):Math.toRadians(angdeg),
  227. w/ 2 ,h/ 2 );
  228. graphics2d.drawImage(image, 0 , 0 , null );
  229. graphics2d.dispose();
  230. return img;
  231. }

  232. final static public void setAlpha( final Graphics2Dg, final double d){
  233. AlphaCompositealphacomposite=AlphaComposite
  234. .getInstance( 3 ,( float )d);
  235. g.setComposite(alphacomposite);
  236. }

  237. public static void main(String[]args){
  238. java.awt.EventQueue.invokeLater( new Runnable(){
  239. public void run(){
  240. Frameframe= new Frame();
  241. frame.addWindowListener( new WindowAdapter(){
  242. public void windowClosing(WindowEvente){
  243. System.exit( 0 );
  244. }
  245. });
  246. frame
  247. .add( new MessageImage(
  248. "每天面对计算机/n不是垒码、就是泡论坛、再不然就是和脑残作斗争,爆个哈韩吧,攻击个棒子网什么的,再闲时就写写Blog,转转Google,"
  249. + "总之24小时离不开电脑,离不开网络(已被纳入我国精神病症状|||),长时间不活动,这颈椎可怎么受的了?没办法,想点办法解决吧。"
  250. + "最近有人说图片能治疗颈椎病,我却偏不信那个邪,就几句话能费人多大的力气去看?说到底还真能累死活人不成?"
  251. + "这左转右绕上窜下跳的,除了费点眼睛,怎么可能把脖子一块运动起来?您说是不是这道理?" ));
  252. frame.setSize(WIDTH+ 5 ,HEIGHT+ 25 );
  253. frame.setResizable( false );
  254. frame.setTitle( "Java版颈椎自动矫正图" );
  255. frame.setLocationRelativeTo( null );
  256. frame.setVisible( true );
  257. }
  258. });
  259. }

  260. }



效果图如下:

Java版颈椎自动矫正图

仔细说起来,这就和各大小说门户生成图片版小说的原理一般无二,毫无技术难度可言,不过毕竟写了,也就丢出来在Blog凑个数。

那位有时间的话(或者等以后鄙人闲的没事),可以做个自动导出,弄套资治通鉴全文之类的图片做颈椎矫正,试验下连续的看完整本颈椎是轻松了,还是碎掉了……




Java版颈椎自动矫正图


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论