最近项目中需要用到验证码屏蔽机器人对网页的访问,所以也就学习了下如何使用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秒,性能不是很高。
下面将继续调研是否存在性能较好的方法。