本来上周双休日没什么事情,准备干点遗留了很久的正经事,结果半途忍不住手贱看新闻,于是被11.28事件触动,随大流的针对棒子及棒子粉抽疯了两天。
没想到到星期一上班脖子就不得劲,顺便查了点资料,发现个山寨版的颈椎矫正图,觉得挺有意思。
如下图:
于是回家后想到自己也做个玩。
问题是,咱爷们不说程序员吧,好歹也是个垒码的,直接PS文字图未免有碍观瞻,于是抽空写了个Java自动生成版的。
代码如下:
- package org.test;
-
- import java.awt.AlphaComposite;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Frame;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.RenderingHints;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
-
- import javax.imageio.ImageIO;
-
- /**
- *Copyright2008
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");youmaynot
- *usethisfileexceptincompliancewiththeLicense.Youmayobtainacopyof
- *theLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,WITHOUT
- *WARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.Seethe
- *Licenseforthespecificlanguagegoverningpermissionsandlimitationsunder
- *theLicense.
- *
- *@projectloonframework
- *@authorchenpeng
- *@email:ceponline@yahoo.com.cn
- *@version0.1
- */
- public class MessageImage extends Canvas{
-
- /**
- *
- */
- private static final long serialVersionUID=1L;
-
- private BufferedImagefontImage;
-
- private Graphics2Dg2d;
- private ImagebackImage;
- final static private int WIDTH= 600 ;
-
- final static private int HEIGHT= 480 ;
-
-
- public MessageImage( final Stringmessages){
-
- fontImage= new BufferedImage(WIDTH,HEIGHT, 2 );
- g2d=fontImage.createGraphics();
- try {
- backImage=ImageIO.read( new File( "back.png" ));
- } catch (IOExceptione){
- e.printStackTrace();
- }
- g2d.drawImage(backImage, 0 , 0 , null );
- setAlpha(g2d, 0.7 );
- int size= 25 ;
- int newLine=(WIDTH/size)- 10 ;
- char []messageChars=messages.toCharArray();
- boolean d= true ;
- StringBuildersbr= new StringBuilder();
- int count= 0 ;
- int len=messageChars.length- 1 ;
- StringfontStyle= "幼圆" ;
- Colorcolor=Color.white;
- for ( int i= 0 ,j= 0 ;i<=len;i++,j++){
- sbr.append(messageChars[i]);
- if (j==newLine||(messageChars[i]== '/n' )){
- g2d.drawImage(createImageMessages( 1 , false ,sbr.toString(),
- color,fontStyle, 1 ,size,d),count+= 30 ,
- (HEIGHT-(sbr.length()*size))-(size* 4 ), null );
- d=!d;
- sbr.delete( 0 ,sbr.length());
- j= 0 ;
- } else if (i==len){
- g2d.drawImage(createImageMessages( 1 , false ,sbr.toString(),
- color,fontStyle, 1 ,size,d),count+= 30 ,
- (HEIGHT-(sbr.length()*size))-(size* 4 ), null );
- }
- }
- setAlpha(g2d, 0.6 );
- Stringmes= "Java版颈椎自动矫正图" ;
- fontStyle= "华文新魏" ;
- size= 20 ;
- g2d.drawImage(createImageMessages( 0 , true ,mes,Color.red,fontStyle,
- 1 ,size, false ),WIDTH-(mes.length()*size)-(size* 2 ),
- HEIGHT-(size* 2 ), null );
- }
-
- /**
- *创建一组图片文字
- *
- *@paramaspect
- *@paramisRow
- *@parammessages
- *@paramcolor
- *@paramname
- *@paramstyle
- *@paramsize
- *@paramd
- *@return
- */
- public static BufferedImagecreateImageMessages( final int aspect,
- final boolean isRow, final Stringmessages, final Colorcolor,
- final Stringname, final int style, final int size, final boolean d){
- final int flength=messages.length();
- final int nowSize=size*flength;
- BufferedImagefontImages;
- if (isRow)
- fontImages= new BufferedImage(nowSize,size, 2 );
- else
- fontImages= new BufferedImage(size,nowSize, 2 );
- Graphics2Dgraphics2d=fontImages.createGraphics();
- char []messageChars=messages.toCharArray();
- if (d){
- char []temp= new char [flength];
- for ( int i= 0 ,j=flength- 1 ;i<flength;i++,j--){
- temp[j]=messageChars[i];
- }
- messageChars=temp;
- }
- if (isRow)
- for ( int i= 0 ;i<flength;i++){
- graphics2d.drawImage(createImageMessage(aspect,
- messageChars[i],color,name,style,size,d),
- i*size, 0 , null );
- }
- else
- for ( int i= 0 ;i<flength;i++){
- graphics2d.drawImage(createImageMessage(aspect,
- messageChars[i],color,name,style,size,d), 0 ,i
- *size, null );
- }
- graphics2d.dispose();
- System.gc();
- return fontImages;
- }
-
- /**
- *创建单独图片文字
- *
- *@paramaspect
- *@parammessage
- *@paramcolor
- *@paramname
- *@paramstyle
- *@paramsize
- *@paramd
- *@return
- */
- public static BufferedImagecreateImageMessage( final int aspect,
- final char message, final Colorcolor, final Stringname,
- final int style, final int size, final boolean d){
- final int nowSize=size+ 1 ;
- BufferedImagefontImage= new BufferedImage(nowSize,nowSize, 2 );
- Graphics2Dgraphics2d=fontImage.createGraphics();
- graphics2d.setColor(color);
- graphics2d.setFont( new Font(name,style,size));
- //设定图像显示状态
- RenderingHintshints= new RenderingHints(
- RenderingHints.KEY_TEXT_ANTIALIASING,
- RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
- hints.put(RenderingHints.KEY_DITHERING,
- RenderingHints.VALUE_DITHER_ENABLE);
- hints.put(RenderingHints.KEY_RENDERING,
- RenderingHints.VALUE_RENDER_SPEED);
- hints.put(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
- RenderingHints.VALUE_FRACTIONALMETRICS_ON);
- hints.put(RenderingHints.KEY_COLOR_RENDERING,
- RenderingHints.VALUE_COLOR_RENDER_QUALITY);
- hints.put(RenderingHints.KEY_DITHERING,
- RenderingHints.VALUE_DITHER_DISABLE);
- graphics2d.setRenderingHints(hints);
- graphics2d.drawString(String.valueOf(message), 0 ,size- 3 );
- graphics2d.dispose();
- switch (aspect){
- case 0 :
- break ;
- case 1 :
- fontImage=MessageImage.rotateImage(fontImage, 90 ,d);
- break ;
- case 2 :
- fontImage=MessageImage.rotateImage(fontImage, 180 ,d);
- break ;
- case 3 :
- fontImage=MessageImage.rotateImage(fontImage, 360 ,d);
- break ;
- }
- return fontImage;
- }
-
- public void paint(Graphicsg){
- g.drawImage(fontImage, 0 , 0 , null );
- }
-
- /**
- *水平翻转当前图像
- *
- *@return
- */
- public static BufferedImagerotateImage( final BufferedImageimage){
- int w=image.getWidth();
- int h=image.getHeight();
- BufferedImageimg;
- Graphics2Dgraphics2d;
- (graphics2d=(img= new BufferedImage(w,h,image.getColorModel()
- .getTransparency())).createGraphics()).drawImage(image, 0 , 0 ,
- w,h,w, 0 , 0 ,h, null );
- graphics2d.dispose();
- return img;
- }
-
- /**
- *旋转图像为指定角度
- *
- *@paramdegree
- *@return
- */
- public static BufferedImagerotateImage( final BufferedImageimage,
- final int angdeg, final boolean d){
- int w=image.getWidth();
- int h=image.getHeight();
- int type=image.getColorModel().getTransparency();
- BufferedImageimg;
- Graphics2Dgraphics2d;
- (graphics2d=(img= new BufferedImage(w,h,type)).createGraphics())
- .setRenderingHint(RenderingHints.KEY_INTERPOLATION,
- RenderingHints.VALUE_INTERPOLATION_BILINEAR);
- graphics2d.rotate(d?-Math.toRadians(angdeg):Math.toRadians(angdeg),
- w/ 2 ,h/ 2 );
- graphics2d.drawImage(image, 0 , 0 , null );
- graphics2d.dispose();
- return img;
- }
-
- final static public void setAlpha( final Graphics2Dg, final double d){
- AlphaCompositealphacomposite=AlphaComposite
- .getInstance( 3 ,( float )d);
- g.setComposite(alphacomposite);
- }
-
- public static void main(String[]args){
- java.awt.EventQueue.invokeLater( new Runnable(){
- public void run(){
- Frameframe= new Frame();
- frame.addWindowListener( new WindowAdapter(){
- public void windowClosing(WindowEvente){
- System.exit( 0 );
- }
- });
- frame
- .add( new MessageImage(
- "每天面对计算机/n不是垒码、就是泡论坛、再不然就是和脑残作斗争,爆个哈韩吧,攻击个棒子网什么的,再闲时就写写Blog,转转Google,"
- + "总之24小时离不开电脑,离不开网络(已被纳入我国精神病症状|||),长时间不活动,这颈椎可怎么受的了?没办法,想点办法解决吧。"
- + "最近有人说图片能治疗颈椎病,我却偏不信那个邪,就几句话能费人多大的力气去看?说到底还真能累死活人不成?"
- + "这左转右绕上窜下跳的,除了费点眼睛,怎么可能把脖子一块运动起来?您说是不是这道理?" ));
- frame.setSize(WIDTH+ 5 ,HEIGHT+ 25 );
- frame.setResizable( false );
- frame.setTitle( "Java版颈椎自动矫正图" );
- frame.setLocationRelativeTo( null );
- frame.setVisible( true );
- }
- });
- }
-
- }
-
效果图如下:
仔细说起来,这就和各大小说门户生成图片版小说的原理一般无二,毫无技术难度可言,不过毕竟写了,也就丢出来在Blog凑个数。
那位有时间的话(或者等以后鄙人闲的没事),可以做个自动导出,弄套资治通鉴全文之类的图片做颈椎矫正,试验下连续的看完整本颈椎是轻松了,还是碎掉了……