Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/web_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ var ccap = require('../')({
height:50,
offset:30,
quality:100,
fontsize:40
fontsize:40,
text_len: 4, // 长度
str_ary: ['1','2','3','4','5','6','7','9','A','C','D','E','F','G','H',
'I','J','K','L','M','N','R','S','T','U','W','X','Y','Z'], // 自定义字符组,去掉一些容易搞错的字符如0 O 8 B等
});

http.createServer(function (request, response) {
Expand Down
12 changes: 5 additions & 7 deletions lib/gen.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
var str_ary = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

//定义随机数组

var gen_fuc = function(){
var str_num = 6,
r_num = str_ary.length,
var gen_fuc = function(options){
var r_num = options.str_ary.length,
text = '';
for(var i=0;i<str_num;i++){
for(var i=0;i<options.str_num;i++){
var pos = Math.floor(Math.random()*r_num)
text += str_ary[pos];//生成随机数
text += options.str_ary[pos];//生成随机数
}
return text;
}
Expand Down
13 changes: 8 additions & 5 deletions lib/hcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ var pathCluster = cluster.isWorker ? String(cluster.worker.id) : '0';

var img_path = path.join(rootPath, pathCluster);
var pid = process.pid;



var isjpeg = (os.platform() == 'linux')? 1 : 0;//判断是否启用jpeg,如果是为win32则只能使用bmp

Expand All @@ -61,6 +59,9 @@ var CAP = function(args){
this.buf = [];//缓存数组
this.text_buf = [];//定义字符串内容数组

this.str_ary = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

this._init(args);//构造函数

this._create = this.generate ? this._custom_create : this._default_create;
Expand All @@ -82,9 +83,11 @@ CAP.prototype._init = function(args){
this.width = obj.width || this.width;
this.height = obj.height || this.height;
this.offset = obj.offset || this.offset;
this.quality = obj.quality || this.quality;
this.quality = obj.quality || this.quality;
this.generate = obj.generate || null;
this.fontsize = obj.fontsize || this.fontsize;
this.str_ary = obj.str_ary || this.str_ary;
this.text_len = obj.text_len || this._text_len;
return;
}
//如果只传递了宽,高,间隔则替换默认值
Expand Down Expand Up @@ -125,9 +128,9 @@ CAP.prototype._call_create = function(text,len,j){//调用C++的CIMG库的create
}

CAP.prototype._default_create = function(){//默认方式创建

var str_num = this.text_len || this._text_len;
for(var i=0;i<this._cache_num;i++){
this._call_create(this._default_generate(), this._text_len, i)
this._call_create(this._default_generate({str_ary: this.str_ary, str_num: str_num}), this._text_len, i)
}

return this;
Expand Down
18 changes: 15 additions & 3 deletions test/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,23 @@ assert.equal(captcha8.timerIsRunning(),0)
captcha8.setTimeout()
assert.equal(captcha8.timerIsRunning(),1)

console.log('ccap all test done!')


var captcha9 = ccap({
width:200,
height:50,
offset:30,
quality:100,
fontsize:40,
text_len: 4, // 长度
str_ary: ['1','2','3','4','5','6','7','9','A','C','D','E','F','G','H',
'I','J','K','L','M','N','R','S','T','U','W','X','Y','Z'], // 自定义字符组,去掉一些容易搞错的字符如0 O 8 B等
});
var ary = captcha9.get();

assert.equal(/\w/.test(ary[0]),true)
assert.equal(ary[0].length,4)
assert.equal(Buffer.isBuffer(ary[1]), true)

console.log('ccap all test done!')

process.exit(0)

Expand Down