Skip to content

Commit

Permalink
add strict mode to captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
mateogianolio committed May 26, 2016
1 parent 321b1d3 commit 95e75bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MLP character recognition
# OCR

Trains a multi-layer perceptron (MLP) neural network to perform optical character recognition (OCR).

Expand Down
18 changes: 10 additions & 8 deletions captcha.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(function() {
(function () {
'use strict';

var canvas = require('canvas');

function generate(config, callback) {
config.size = config.size || 4;
config.height = config.height || 24;
Expand All @@ -10,7 +12,7 @@
config.text = config.text || ('' + Math.random()).substr(2, config.size);
config.distortion = config.distortion === undefined ? true : config.distortion;

var size = Math.round(config.height * .7),
var size = Math.round(config.height * 0.7),
c = new canvas(config.width, config.height),
context = c.getContext('2d'),
fonts = config.fonts || ['sans-serif', 'serif'],
Expand All @@ -19,10 +21,10 @@
context.fillStyle = config.background;
context.fillRect(0, 0, config.width, config.height);
context.fillStyle = config.color;

for(i = 0; i < config.text.length; i++) {
context.font = size + 'px ' + fonts[Math.floor(Math.random() * fonts.length)];

if(config.distortion) {
context.setTransform(
Math.random() * 0.25 + 1, // scale horizontally
Expand All @@ -42,17 +44,17 @@
config.height - size / 2 // move vertically
);
}

context.fillText(config.text.charAt(i), 0, 0);
}

c.toBuffer(function(error, buffer) {
if(error)
throw error;

callback(config.text, buffer);
});
}

module.exports.generate = generate;
})();
})();

0 comments on commit 95e75bf

Please sign in to comment.