Java根据随机数生成jpg图片

来源:互联网 发布:禹卫书法行书字体 mac 编辑:程序博客网 时间:2024/04/29 21:22

转自:http://blog.csdn.net/linzhanggeorge/article/details/4385253


[java] view plaincopy
  1. import java.awt.Color;  
  2. import java.awt.Font;  
  3. import java.awt.Graphics2D;  
  4. import java.awt.RenderingHints;  
  5. import java.awt.image.BufferedImage;  
  6. import java.io.File;  
  7. import java.io.FileOutputStream;  
  8. import java.util.Random;  
  9.   
  10. import com.sun.image.codec.jpeg.JPEGCodec;  
  11. import com.sun.image.codec.jpeg.JPEGEncodeParam;  
  12. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  13.   
  14.  /** 
  15.      * 根据提供的文字生成jpg图片 
  16.      * @param s String 文字 
  17.      * @param smallWidth int 每个字的宽度和高度是一样的 
  18.       * @param bgcolor Color   背景色 
  19.      * @param fontcolor Color   字色 
  20.      * @param fontPath String 字体文件 
  21.      * @param jpgname String jpg图片名 
  22.       * @return 
  23.      */  
  24.   
  25. public class Test {  
  26.      private String createJpgByFont(String s, int smallWidth,Color bgcolor,Color fontcolor,String fontPath,String jpgname) {  
  27.             try {                                                                                   
  28.  //宽度    高度  
  29. BufferedImage bimage = new BufferedImage(s.length()*smallWidth/2,smallWidth,BufferedImage.TYPE_INT_RGB);  
  30.                 Graphics2D g = bimage.createGraphics();  
  31.                 g.setColor(bgcolor); //背景色  
  32.                 g.fillRect(00,smallWidth*s.length(), smallWidth); //画一个矩形  
  33.                 System.out.println( s.length()+" "+smallWidth);  
  34. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);   
  35. //去除锯齿(当设置的字体过大的时候,会出现锯齿)  
  36.                 g.setColor(fontcolor); //字的颜色  
  37.                 File file = new File(fontPath);   //字体文件  
  38.                Font font = Font.createFont(Font.TRUETYPE_FONT, file);   
  39. //根据字体文件所在位置,创建新的字体对象(此语句在jdk1.5下面才支持)   
  40.                 g.setFont(font.deriveFont((float) smallWidth));     
  41. //font.deriveFont(float f)复制当前 Font 对象并应用新设置字体的大小  
  42.                 g.drawString(s,0, smallWidth);   
  43. //在指定坐标除添加文字  
  44.                 g.dispose();  
  45.                 FileOutputStream out = new FileOutputStream(jpgname); //指定输出文件  
  46.                 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  47.                 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);  
  48.                 param.setQuality(50f, true);  
  49.                 encoder.encode(bimage, param); //存盘  
  50.                 out.flush();  
  51.                 out.close();  
  52.             } catch (Exception e) {  
  53.                 System.out.println( "createJpgByFont Failed!");  
  54.                 e.printStackTrace();  
  55.             }  
  56.             return "Retruning";  
  57.         }  
  58.      public String getRandomNumber(){//取随机数字符串  
  59.          double s=Math.random()*10000;  
  60.          long t=Math.round(s);    
  61.          String st=new Long(t).toString();  
  62.          System.out.println(st);  
  63.          return st;  
  64.      }  
  65.      public static void main(String args[]){  
  66.          Test tt=new Test();  
  67. tt.createJpgByFont(tt.getRandomNumber(),100,Color.gray, Color.red,"C:/times.ttf""C:/name.jpg");  
  68.      }  
  69.   
  70. }  

 

效果:

随机图片1

 

随机图片2




0 0
原创粉丝点击