[转帖]JAVA制作验证图片_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2576 | 回复: 0   主题: [转帖]JAVA制作验证图片        下一篇 
    本主题由 koei 于 2014-5-2 16:22:04 移动
lengyuLee
注册用户
等级:少校
经验:1118
发帖:83
精华:5
注册:2013-3-7
状态:离线
发送短消息息给lengyuLee 加好友    发送短消息息给lengyuLee 发消息
发表于: IP:您无权察看 2013-3-13 17:30:20 | [全部帖] [楼主帖] 楼主

今天上午抽了点时间,看到某些论坛上利用python语言写了一个关于验证码的程序,于是乎,手痒痒,就写了一个利用java编写的代码片段,在这里就给

大家贴出来,好来弥补最近没有在论坛发言的罪过。(以下代码,只适合初学者,没什么记住含量,仅仅是为了实现功能,如有更好意见,欢迎论坛致贴)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class test extends JFrame{
      private static final int LABLE_WIDTH = 500;
      private static final int WORD_NUMBER = 4;
      private ImageIcon image = generateRandomPicture();
      public static Color[] colors = {Color.black,
            Color.blue,
            Color.red,
            Color.cyan,
            Color.darkGray,
            Color.gray,
            Color.magenta,
            Color.orange,
      Color.yellow};
      public test() {
            init();
            addComponent(image);
      }
      private void init(){
            this.setBounds(new Rectangle(200,200,LABLE_WIDTH,LABLE_WIDTH));
            this.setLocationRelativeTo(null);
            this.getContentPane().setLayout(new BorderLayout());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.getContentPane().setBackground(Color.orange);
      }
      private void addComponent(ImageIcon image){
            JLabel img = new JLabel(image);
            this.getContentPane().add(img,BorderLayout.CENTER);
      }
      /**

* 创建一个图片

* @return

*/
      private ImageIcon generateRandomPicture(){
            Random random = new Random();
            BufferedImage imageBuffered = new BufferedImage(500,300, BufferedImage.TYPE_INT_BGR);
            Graphics g = imageBuffered.createGraphics();
            Color colorBak = g.getColor();
            //绘制图片背景
            g.setColor(Color.green);
            g.fillRect(0, 0, LABLE_WIDTH,LABLE_WIDTH);
            //绘制线条
            for (int i = 1; i < 200; i++) {
                  int x1 = random.nextInt(LABLE_WIDTH);
                  int x2 = random.nextInt(LABLE_WIDTH);;
                  int y1 = random.nextInt(LABLE_WIDTH);;
                  int y2 = random.nextInt(LABLE_WIDTH);;
                  g.setColor(colors[i%colors.length]);
                  g.drawLine(x1, y1, x2, y2);
            }
            //绘制验证码
            for (int i = 0; i < WORD_NUMBER; i++) {
                  int index = (int)(random.nextFloat()*colors.length);
                  g.setColor(colors[index]);
                  int size = getPonit(60,100);
                  int height = getPonit(100,300);
                  int width = getPonit(LABLE_WIDTH/WORD_NUMBER*i, LABLE_WIDTH/WORD_NUMBER*i+50);
                  g.setFont(new Font("宋体",Font.BOLD,size));
                  char content = getCharater();
                  g.drawString(content+"", width, height);
            }
            //解放资源
            g.dispose();
            return new ImageIcon(imageBuffered);
      }
      /**

* 获取一个范围内的某一值

* @param x 最小值

* @param y 最大值

* @return

*/
      private int getPonit(int x, int y){
            Random random = new Random();
            int value = (int)(random.nextFloat()*LABLE_WIDTH);
            while(true){
                  if(value > x && value < y){
                        return value;
                  }else{
                  value = (int)(random.nextFloat()*LABLE_WIDTH);
            }
      }
}
/**

* 获取一个中文字符

* @return

*/
private char getCharater(){
      Random random = new Random();
      int value = random.nextInt(0x9fbf >> 1);
      while(true){
            if(value > 0x4e00 && value < 0x9fbf){
                  return (char)value;
            }else{
            if(value < 0 || value > 0x9fbf){
                  value = random.nextInt(0x9fbf >> 1);
            }
            value <<= 1;
      }
}
}
public static void main(String[] args) {
      test t = new test();
      t.setVisible(true);
}
}


该贴由koei转至本版2014-5-2 16:22:04



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