Nadesłany przez Tomasz Lubiński, 19 lipca 2011 15:00
Kod przedstawiony poniżej przedstawia główną część rozwiązania problemu.Pobierz pełne rozwiązanie.
Jeżeli nie odpowiada Ci sposób formatowania kodu przez autora skorzystaj z pretty printer'a i dostosuj go automatycznie do siebie.
ean13.js:
//EAN-13 //(c) 2010 by Tomasz Lubinski //www.algorytm.org var parityTable = [ [0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 1, 1], [0, 0, 1, 1, 0, 1], [0, 0, 1, 1, 1, 0], [0, 1, 0, 0, 1, 1], [0, 1, 1, 0, 0, 1], [0, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 1], [0, 1, 0, 1, 1, 0], [0, 1, 1, 0, 1, 0] ]; var right = [ [1, 1, 1, 0, 0, 1, 0], [1, 1, 0, 0, 1, 1, 0], [1, 1, 0, 1, 1, 0, 0], [1, 0, 0, 0, 0, 1, 0], [1, 0, 1, 1, 1, 0, 0], [1, 0, 0, 1, 1, 1, 0], [1, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 1, 0, 0], [1, 0, 0, 1, 0, 0, 0], [1, 1, 1, 0, 1, 0, 0] ]; var left = [ [ [0, 0, 0, 1, 1, 0, 1], [0, 0, 1, 1, 0, 0, 1], [0, 0, 1, 0, 0, 1, 1], [0, 1, 1, 1, 1, 0, 1], [0, 1, 0, 0, 0, 1, 1], [0, 1, 1, 0, 0, 0, 1], [0, 1, 0, 1, 1, 1, 1], [0, 1, 1, 1, 0, 1, 1], [0, 1, 1, 0, 1, 1, 1], [0, 0, 0, 1, 0, 1, 1] ], [ [0, 1, 0, 0, 1, 1, 1], [0, 1, 1, 0, 0, 1, 1], [0, 0, 1, 1, 0, 1, 1], [0, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 1, 0, 1], [0, 1, 1, 1, 0, 0 ,1], [0, 0, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 0 ,1], [0, 0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 1, 1, 1] ] ]; function convert(EAN13) { /* inccorect length or check sum, return null */ if ((EAN13 == null) || (EAN13.length != 13) || (checkSum(EAN13) == false)) { return null; } var bars = new Array(95); for (i=0; i<95; i++) { bars[i] = 0; } bars[0] = 1; bars[1] = 0; bars[2] = 1; for (i=1; i<7; i++) { for (j=0; j<7; j++) { bars[(i-1)*7 + 3 + j] = left[parityTable[EAN13[0]][i-1]][EAN13[i]][j]; } } bars[45] = 0; bars[46] = 1; bars[47] = 0; bars[48] = 1; bars[49] = 0; for (i=7; i<13; i++) { for (j=0; j<7; j++) { bars[(i-7)*7 + 50 + j] = right[EAN13[i]][j]; } } bars[92] = 1; bars[93] = 0; bars[94] = 1; return bars; } /** * calculates checksum of EAN13 */ function checkSum(EAN13) { var sum = 1 * EAN13[0] + 3 * EAN13[1] + 1 * EAN13[2] + 3 * EAN13[3] + 1 * EAN13[4] + 3 * EAN13[5] + 1 * EAN13[6] + 3 * EAN13[7] + 1 * EAN13[8] + 3 * EAN13[9] + 1 * EAN13[10] + 3 * EAN13[11]; sum %= 10; sum = 10 - sum; sum %= 10; if (sum == EAN13[12]) { return true; } else { return false; } } //initialize data function generate(EAN13) { var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); /* clear canvas */ canvas.width = canvas.width; /* prepare style */ ctx.fillStyle = "rgb(0,0,0)"; ctx.font = "10pt Arial"; ctx.textBaseline = "top"; ctx.textAlign = "left"; var bars = convert(EAN13); if (bars != null) { for (i=0; i<bars.length; i++) { if ((i >= 0 && i<= 2) || (i >= 45 && i<= 49) || (i >= 92 && i<= 94)) { length = 100; } else { length = 90; } if (bars[i] == 1) { ctx.fillRect(i*2+20, 10, 2, length); } } ctx.fillText(EAN13[0], 12, 100); for (i=1; i<7; i++) { ctx.fillText(EAN13[i], i*14+15, 105); } for (i=7; i<13; i++) { ctx.fillText(EAN13[i], i*14+25, 105); } } else { ctx.fillText("Nieprawidłowy numer", 50, 80); ctx.fillText("Incorrect number", 50, 100); } }