• 随机图片验证码的制作


    以前写的一个生成彩色验证码的例子。重载了makeCode()函数,即可以生成本地图片,又可生成基于输出流的图片。
    package smart.gui.util;
    。。。
    public class ColoredCodeMaker {
        private final static String letterCollection =
            "ABEFGHNRTabcdefghjkmnprstuvwxyz23456789";
        private String randomStr = "";
        private RenderedImage renderedImg = null;
        public ColoredCodeMaker(int width, int height, int letterCount) {
            init(letterCount);
            makeRenderedImage(width, height);
        }
        public String makeCode(OutputStream os) {
            String formatName = "JPEG";
            try {
                ImageIO.write(this.renderedImg, formatName, os);
            }
            catch (Exception ex) {
                return "";
            }
            return randomStr;
        }
        public String makeCode(File outputFileName) {
            String formatName = "JPEG";
            try {
                ImageIO.write(this.renderedImg, formatName, outputFileName);
            }
            catch (Exception ex) {
                return "";
            }
            return randomStr;
        }
        private void makeRenderedImage(int width, int height) {
            if (width <= 0) {
                width = 60;
            }
            if (height <= 20) {
                height = 20;
            }
            /**
             * TYPE_INT_RGB Represents an image with 8-bit RGB color components packed into integer pixels.
             * The image has a DirectColorModel without alpha.
             */
            BufferedImage bufImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
            /**
             * 获取图形上下文
             */
            Graphics g = bufImage.createGraphics();
            /**
             * 设置背景色
             */
            g.setColor(new Color(0xDCDCDC));
            g.fillRect(0, 0, width, height);
            /**
             * 画边框
             */
             g.setColor(Color.black);
             g.drawRect(0,0,width-1,height-1);
            /**
             * 将认证码显示到图像中
             */
            g.setColor(Color.black);
            g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
            for (int i = 0; i < randomStr.length(); i++) {
                g.drawString(randomStr.charAt(i)+"", width/(randomStr.length()+2)*(i+1), height/2 + 10);
            }
            Random random = new Random();
            for (int i = 0; i < width/2; i++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                g.drawOval(x,y,1,1);
            }

            g.dispose();
            this.renderedImg = bufImage;
        }
        private void init(int letterCount) {
            if (randomStr.length() != letterCount) {
                randomStr = "";
                for (int i = 0; i < letterCount; i++) {
                    randomStr +=
                        letterCollection.charAt( (int) (letterCollection.length() *
                        Math.random()));
                }
            }
        }
    }
    ////////测试代码,图片宽150,高40,图片上共8个随机字符,保存在C盘下
            ColoredCodeMaker ccm = new ColoredCodeMaker(150,40,8);
            ccm.makeCode(new java.io.File("C://aaa.jpeg"));
    ----------------------------------------------------------------------------------------------------------------------------------------------用VB制作验证码生成器
    算法设计

    1.窗体设计

    启动VB6并新建一个标准EXE工程,布置好控件并设置好名称。

    接着将两个图片框的AutoRedraw属性设置成True,txt_inp、cmd_browse的Enabled属性设置为False,txt_inp的Maxlength属性设置成10。然后单击“工程→部件”,勾选“Microsoft CommonDialog Control 6.0”。

    2.算法

    加密过程是程序的重点。在这里我们运用画线和画点函数来处理图片,在图片框上随机选两个点画直线,同时用随机函数确定它的颜色;再随机点一个点,同样用随机函数确定它的颜色。在这里我们还用随机取字体样式和背景色,不过要注意的是,背景和字体颜色的差别要搞大一些,由于每个RBG分量值越大颜色越深,因此我们用下面两条语句就可以增强颜色的对比度:

    pic_las.ForeColor = RGB(Rnd * 255, Rnd * 255, Rnd * 255)
    pic_las.BackColor = RGB(Rnd * 55, Rnd * 55, Rnd * 55)
    绘制图像的具体的代码如下:
    Sub AddPas()
      On Error GoTo Err1
      pic_las.Print Space(10 - Len(Result)); Result
      Dim x, y, x1, x2, i As Integer
      '随机决定输出字体样式
      pic_las.FontItalic = Int(Rnd * 2)
      pic_las.FontUnderline = Int(Rnd * 2)
      pic_las.ForeColor = RGB(Rnd * 155 + 100, Rnd * 155 + 100, Rnd * 155 + 100)
      '为验证码添加随机直线
      For i = 1 To (Int(Rnd(1) * 8) + 1)
        x = Int(Rnd * pic_las.Width): y = Int(Rnd * pic_las.Height)
        x1 = Int(Rnd * pic_las.Width): Y1 = Int(Rnd * pic_las.Height)
        pic_las.Line (x, y)-(x1, Y1), RGB(Rnd * 255, Rnd * 255, Rnd * 255)
      Next i
      '为验证码随机添加圆点
      For i = 1 To (Int(Rnd(1) * 200) + 100)
        pic_las.PSet (pic_las.Width * Rnd, pic_las.Height * Rnd), _
          RGB(Rnd * 255, Rnd * 255, Rnd * 255)
      Next i
      '写入输出文件
      If chk_file.Value = 1 Then
        SavePicture pic_las.Image, txt_outfile.Text
        MsgBox "验证码已经保存在:" & txt_outfile.Text, VBInformation
      End If
      Exit Sub
    Err1:  '产生了错误
      MsgBox Err.Number & ":" & Err.Description, VBExclamation
    End Sub

    内容处理好了,接下来就只要简单响应用户的操作即可。在这里我们还需要解决随机生成验证码的问题,我们可以把需要生成的验证码每一位都放入一个字符串,再用随机函数以及字符串截取函数Mid(String,Start As Long,[Length])读入每一位验证码。另外为了增强程序的功能,我还允许用户保存和拷贝验证码以及允许用户自行定义验证码。由于版面有限,这里就不给出完整的源程序了,你可以从
    http://www.cfan.com.cn/11program/200513/VBpass.zip下载到完整的源代码。

    总结

    运行一下程序,“加密”效果还不错吧。

    这可不是一般的破解机器能识别得了的哦!当然你也可以将这个加密算法运用到你个人的程序和论坛上去,它为程序的安全性提供了很好的保证呢!

  • 相关阅读:
    jquery 表单清空
    CK-Editor content.replace
    CSS DIV HOVER
    返回上一页并刷新与返回上一页不刷新代码
    Google Java编程风格指南中文版
    编程常见英语词汇
    教你如何删除tomcat服务器的stdout.log文件
    @Autowired @Resource @Qualifier的区别
    JSTL标签,EL表达式,OGNL表达式,struts2标签 汇总
    4.11 application未注入报错解决
  • 原文地址:https://www.cnblogs.com/pocter/p/3684539.html
Copyright © 2020-2023  润新知