Last active
December 16, 2015 19:39
-
-
Save GZShi/5487022 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 左边的规则 | |
function leftCode(number, o) { | |
if(o == '1') | |
return [ | |
'0001101', '0011001', '0010011', '0111101', '0100011', | |
'0110001', '0101111', '0111011', '0110111', '0001011'][parseInt(number)%10]; | |
else { | |
return [ | |
'0100111', '0110011', '0011011', '0100001', '0011101', | |
'0111001', '0000101', '0010001', '0001001', '0010111'][parseInt(number)%10]; | |
} | |
} | |
// 右边的规则 | |
function rightCode(number) { | |
return [ | |
'1110010', '1100110', '1101100', '1000010', '1011100', | |
'1001110', '1010000', '1000100', '1001000', '1110100'][parseInt(number)%10]; | |
} | |
// 隐含数字的规则 | |
function ISBNLeftRule(number) { | |
return [ | |
'111111', '110100', '110010', '110001', '101100', | |
'100110', '100011', '101010', '101001', '100101'][parseInt(number)%10]; | |
} | |
function createEAN13(series) { | |
var result = '101'; | |
var rule = ISBNLeftRule(series[0]); | |
for(var i = 1; i < 7; ++i) { | |
result += leftCode(series[i], rule[i-1]); | |
} | |
result += '01010'; | |
for(var i = 7; i < 13; ++i) { | |
result += rightCode(series[i]); | |
} | |
result += '101'; | |
return result; | |
} | |
// 校验规则 | |
function checkEAN13(series, mode) { | |
var sum = 0; | |
for(var i = 0; i < 6; ++i) { | |
sum += parseInt(series[i*2]); | |
sum += parseInt(series[i*2 + 1]) * 3; | |
} | |
sum = 10 - (sum % 10); | |
if(mode == true) | |
return sum + ''; | |
if(parseInt(series[12]) == sum) | |
return true; | |
return false; | |
} | |
function draw(ctx, series) { | |
ctx.clearRect(0, 0, 1000, 1000); | |
if(series.length == 12) { | |
series = series + checkEAN13(series, true); | |
} else if(checkEAN13(series) == false) { | |
ctx.fillStyle = 'black'; | |
ctx.fillText('check failed!', 10, 30); | |
return ; | |
} | |
var WIDTH = 4; // 条码宽度 | |
var HEIGHT = 200; // 条码高度 | |
var bc = createEAN13(series); | |
ctx.fillStyle = 'black'; | |
for(i = 0; i < 3; ++i) { | |
if(bc[i] == '1') { | |
ctx.fillRect(i*WIDTH, 20, WIDTH, HEIGHT+25); | |
} | |
} | |
for(; i < 45; ++i) { | |
if(bc[i] == '1') { | |
ctx.fillRect(i*WIDTH, 20, WIDTH, HEIGHT); | |
} | |
} | |
for(; i < 50; ++i) { | |
if(bc[i] == '1') { | |
ctx.fillRect(i*WIDTH, 20, WIDTH, HEIGHT+25); | |
} | |
} | |
for(; i < 92; ++i) { | |
if(bc[i] == '1') { | |
ctx.fillRect(i*WIDTH, 20, WIDTH, HEIGHT); | |
} | |
} | |
for(; i < 95; ++i) { | |
if(bc[i] == '1') { | |
ctx.fillRect(i*WIDTH, 20, WIDTH, HEIGHT+25); | |
} | |
} | |
} | |
function create() { | |
var ctx = document.getElementById('c').getContext('2d'); | |
var text = document.getElementById('in').value; | |
draw(ctx, text); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment