Skip to content

Instantly share code, notes, and snippets.

@StoneMoe
Created March 2, 2017 16:28
Show Gist options
  • Save StoneMoe/1caea48aff45cc541f88ec6f93d0c9e7 to your computer and use it in GitHub Desktop.
Save StoneMoe/1caea48aff45cc541f88ec6f93d0c9e7 to your computer and use it in GitHub Desktop.
function ocr() {
var image = document.querySelector("#myimg6"); // 取验证码
var canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
var numbers = [ //0-9和a-z的明暗值
"0",
"0",
"1100001111100000011110011000110011110011111111001111111001111111001111111001111111001111111001111111000000001100000000011111111111111111111111111111111111111111",
"1100001111000000011100111100111111110011111110001111000001111100000111111110001111111100111111110011000000011110000011111111111111111111111111111111111111111111",
"1111000111111100011111100001111111100111110010011110011001110011100111000000001100000000111111100111111000001111100000011111111111111111111111111111111111111111",
"0000000011100000111110011111111001111111100000111110000001111001100011111111001111111100110011100011000000011110000011111111111111111111111111111111111111111111",
"1111000011110000001110001111111001111111000000111100000001110001100011001111001100111100111001100011100000011111000011111111111111111111111111111111111111111111",
"0000000011000000001100111000111111100011111110011111111001111111000111111100111111110011111110001111111001111111100111111111111111111111111111111111111111111111",
"0100001111100110011100111100110011110011000110001110000001111000000111001111001100111100110011110011100000011111000011111111111111111111111111111111111111111111",
"1100001111110000011100011000110011110011001111001100011000111000000011110000001111111001111111000111000000111100001111111111111111111111111111111111111111111111",
"1111111111111111111111111111111100000111100000001111111100111100000011000000001100011100110011100011000000000110000100011111111111111111111111111111111111111111",
"0011111111001111111100111111110010000111000000001100011100010011111001001111100100111110010001110001000000001100100001111111111111111111111111111111111111111111",
"1111111111111111111111111111111100000011100000000100011110010011111001001111111100111111110001111001100000000111100000111111111111111111111111111111111111111111",
"1111110001111111000111111110011100001001100000000100011100010011111001001111100100111110010001110000100000000011000010001111111111111111111111111111111111111111",
"1111111111111111111111111111111100000111000000001100111110010000000000000000000100111111110001111001100000000111000000111111111111111111111111111111111111111111",
"1110000001100000000111001111110000000011000000001111001111111100111111110011111111001111111000111111000000001100000000111111111111111111111111111111111111111111",
"1111111111111111111111111111111100001000100000000100011100010011111001001111100100111110010001110001100000000111000010011111111001111111100111000000111100000111",
"0001111111000111111110011111111001000011100000000110001110011001111001100111100110011110011001111001000011000000001100001111111111111111111111111111111111111111",
"1111001111111100111111111111111000001111100000111111110011111111001111111100111111110011111111001111100000000110000000011111111111111111111111111111111111111111",
"1111001111111100111111111111110000000111000000011111111001111111100111111110011111111001111111100111111110011111111001111111100111111110011100000011110000001111",
"0001111111000111111100011111111001100001100110000110010011111000011111100001111110010011111001100111000111000000011100001111111111111111111111111111111111111111",
"0",
"1111111111111111111111111111110000110001000000000000110010000011001100001100110000110011000011001100000100010000010001001111111111111111111111111111111111111111",
"1111111111111111111111111111110001000011000000000110001110011000111001100111100110001110010001111001000011000000000100001111111111111111111111111111111111111111",
"0",
"1111111111111111111111111111110010000111000000000100011100010011111001001111100100111110010001110001000000001100100001110011111111001111111100001111110000111111",
"1111111111111111111111111111111100001000100000000000011100010011111001001111100100111110010001111001100000000111000010011111111001111111100111111000001111100000",
"1111111111111111111111111111110000100011000000000111000110011100111111110011111111001111111100111111000000001100000000111111111111111111111111111111111111111111",
"1111111111111111111111111111111110000001100000000110011110011000011111110000001111111000010001111001100000000110000001111111111111111111111111111111111111111111",
"1100111111110011111111001111110000000011000000001111001111111100111111110011111111001111111100110001110000000111100001111111111111111111111111111111111111111111",
"1111111111111111111111111111110001110001000111000110011110011001111001100111100110011110011001110001100000000011001110001111111111111111111111111111111111111111",
"1111111111111111111111111111110000110000000111000000111110011001110011100111001111001001111100100111111000111111100011111111111111111111111111111111111111111111",
"1111111111111111111111111111110001110000000111000000111110010011111001001000100110001000111000100011100111001110011100111111111111111111111111111111111111111111",
"1111111111111111111111111111110000110000000011000011001100111110001111111100111111100001111100110011000011000000001100001111111111111111111111111111111111111111",
"1111111111111111111111111111110001110000000111000000111110011001111001100111001111001001111100100011110000011111100011111110001111111001111100000001110000000111",
"1111111111111111111111111111111000000001100000000110001100111111100111111100011111100111111000111001100000000110000000011111111111111111111111111111111111111111"
];
var captcha = ""; //存放识别后的验证码
canvas.width = image.width;
canvas.height = image.height;
document.body.appendChild(canvas);
ctx.drawImage(image, 0, 0);
for (var i = 0; i < 4; i++) {
var pixels = ctx.getImageData(14 * i + 4, 5, 10, 16).data;
var ldString = "";
for (var j = 0,length = pixels.length; j < length; j += 4) {
ldString = ldString + (+(pixels[j] * 0.3 + pixels[j + 1] * 0.59 + pixels[j + 2] * 0.11 >= 140));
}
var comms = numbers.map(function (value) { //为了100%识别率,这里不能直接判断是否和模板字符串相等,因为可能有个别0被计算成1,或者相反
return ldString.split("").filter(function (v, index) {
return value[index] === v
}).length
});
captcha += comms.indexOf(Math.max.apply(null, comms))<=9?comms.indexOf(Math.max.apply(null, comms)):String.fromCharCode(comms.indexOf(Math.max.apply(null, comms))+87); //添加到识别好的验证码中
}
alert(captcha);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment