使用C生成验证码

    最近项目中需要用到验证码屏蔽机器人对网页的访问,所以也就学习了下如何使用C来生成验证码。

该方法优点是:简单,缺点:性能比较差。

    生成验证码的方法是调用libcaptcha.c提供的一些接口函数。该文件可以在网上下载。http://brokestream.com/captcha.html

    demo程序如下:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define gifsize 17646
int main() {
    char l[6] ;
    unsigned char im[70*200];
    unsigned char gif[gifsize];
    FILE *fp = NULL; 
    clock_t t1;
    clock_t t2;
    long sec; 
    int i = 0;

    t1 = clock();
    for (i = 0; i < 10000;i++)
    {
        captcha(im,l);
        makegif(im,gif);
    }
    t2 = clock();

    printf("t2 -t1 %d\n", t2 - t1); 
    printf("t2 -t1 %d\n", CLOCKS_PER_SEC);

    fp = fopen("/home/waf/code/Capt/test.gif", "a+");
    if (fp != NULL)
    {
        printf("the captcha is %s\n", l); 
        fwrite(gif, 1, gifsize, fp);
        fclose(fp);
    }
    else
    {
        printf("fp is null \n"); 
    }

    return 0;
}

生成验证码的核心代码,即如下两行

captcha(im,l);
makegif(im,gif);

gif用于保存生成的gif图片,l用于保存生成的字母

clock用于计算生成10000次的时间,通过测试大概生成10000次需要2.3秒,性能不是很高。

下面将继续调研是否存在性能较好的方法。

转载于:https://my.oschina.net/xDreamYY/blog/225159

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值