[转帖] 验证码代码_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2737 | 回复: 0   主题: [转帖] 验证码代码        下一篇 
只是很无聊
注册用户
等级:中尉
经验:440
发帖:33
精华:0
注册:2013-6-18
状态:离线
发送短消息息给只是很无聊 加好友    发送短消息息给只是很无聊 发消息
发表于: IP:您无权察看 2013-6-18 15:54:56 | [全部帖] [楼主帖] 楼主

  1. import java.awt.image.BufferedImage; 
  2. import java.awt.Graphics; 
  3. import java.awt.Font; 
  4. import java.util.Random; 
  5. import java.awt.Color; 
  6. import javax.imageio.ImageIO; 
  7. import java.io.File; 
  8. import java.io.FileOutputStream; 
  9. /** 
  10.  @Author 007liuchao007 --> chenml--->Michael Sun 
  11. **/ 
  12. public class CheckMa{ 
  13.        public static final String typeB="0123456789ABCDEFGHRJKLMNOPQRSTUVWXYZ"; 
  14.        public static final int Font_SIZE=15; 
  15.        public static final String FONT_NAME="Fixedsys"; 
  16.       
  17.        //设置图片宽和高 
  18.        private int width=100; 
  19.        private int height=30; 
  20.       
  21.        //设置随机数类 
  22.        Random random=new Random(); 
  23.       
  24.        //设置干扰线数量 
  25.        private int lineNum=10; 
  26.       
  27.        //设置字符个数 
  28.        private int charNum=4; 
  29.       
  30.        //测试一下 
  31.        public static void main(String args[])throws Exception{ 
  32.              CheckMa check=new CheckMa(); 
  33.              BufferedImage tmpImage=check.getBufferedImage(); 
  34.              ImageIO.write(tmpImage,"JPEG",new FileOutputStream(new File("d:\\javaweb\\008.jpg"))); 
  35.        } 
  36.       
  37.       
  38.        public BufferedImage getBufferedImage(){ 
  39.              BufferedImage imageBuffer= new BufferedImage(this.width,this.height,BufferedImage.TYPE_INT_BGR); 
  40.             
  41.              //定义一个Graphise类对封闭图像上绘制 
  42.              Graphics g= imageBuffer.getGraphics() ; 
  43.              //设定背景颜色 
  44.              g.setColor(getColor(200,250)); 
  45.             
  46.              //设定填充区域的矩形框 
  47.              g.fillRect(0,0,this.width,this.height); 
  48.             
  49.              //设置干扰色的颜色 
  50.              g.setColor(getColor(100,120)); 
  51.              //绘制干扰线 
  52.              for(int i=0;i<lineNum;i++){ 
  53.                    drawLine(g); 
  54.              } 
  55.             
  56.             
  57.              g.setFont(new Font(FONT_NAME, Font.ROMAN_BASELINE, Font_SIZE)); 
  58.              //绘制字符串 
  59.              for(int i=0;i<charNum;i++){ 
  60.                    g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121))); 
  61.                    String imageString=String.valueOf(typeB.charAt(random.nextInt(typeB.length()))) ; 
  62.                    g.translate(random.nextInt(3), random.nextInt(3)); 
  63.                    g.drawString(imageString, 20*i, 16) ; 
  64.                   
  65.              } 
  66.              g.dispose() ;//释放资源 
  67.             
  68.              return imageBuffer; 
  69.             
  70.        } 
  71.       
  72.       
  73.        public void drawLine(Graphics g){ 
  74.              int x1=random.nextInt(this.width); 
  75.              int y1=random.nextInt(this.height); 
  76.              int x2=random.nextInt(15); 
  77.              int y2=random.nextInt(15); 
  78.              g.drawLine(x1, y1, x1+ x2,y1+y2);//绘制直线 
  79.              // g.translate(random.nextInt(3), random.nextInt(3)); 
  80.             
  81.        } 
  82.       
  83.       
  84.        public Color getColor(int x,int y){ 
  85.              if(x>255) 
  86.              x=255; 
  87.              if(y>255) 
  88.              y=255; 
  89.             
  90.              int r=x+random.nextInt(y-x); 
  91.              int g=x+random.nextInt(y-x); 
  92.              int b=x+random.nextInt(y-x); 
  93.             
  94.              return new Color(r,g,b); 
  95.        } 
  96.       
  97.       
  98.       
  99.       
  100.  } 



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论