📄 qrcode.js
字号:
//!/usr/local/bin/ruby//// QRcode class library for ruby version 0.50beta6 (c)2002-2004 Y.Swetake/////* for JavaScript by ma.la / (2005/10) http://la.ma.la/misc/qrcode/*/nil = null;function Qrcode(){ this.initialize.apply(this,arguments)}Qrcode.prototype.extend({initialize : function(){ this.path="./qrcode_data" this.qrcode_error_correct="M" this.qrcode_version=0 this.qrcode_structureappend_n=0 this.qrcode_structureappend_m=0 this.qrcode_structureappend_parity="" this.qrcode_structureappend_originaldata=""},string_bit_cal : function(s1,s2,ind){ if (s1.length>s2.length){ s3=s1 s1=s2 s2=s3 } i=0 res="" left_length=s2.length-s1.length switch(ind){ case "xor" : s1.each_byte(function(b){ res += (b ^ s2.charCodeAt(i)).chr() i += 1 }) res += s2[s1.length,left_length] break case "or" : s1.each_byte(function(b){ res += (b | s2.charCodeAt(i)).chr() i += 1 }) res += s2[s1.length,left_length] break case "and" : s1.each_byte(function(b){ res += (b & s2.charCodeAt(i)).chr() i += 1 }) res += chr(0).x(left_length) break }// alert("res" + res.unpack("C*")); return(res)},string_bit_not : function(s1){ res="" s1.each_byte(function(b){res += (256 + ~b).chr()}) return(res)},set_qrcode_version : function(z){ if (z>=0 && z<=40) this.qrcode_version=z},set_qrcode_error_correct : function(z){ this.qrcode_error_correct=z},get_qrcode_version : function(){ return this.qrcode_version},set_structureappend : function(m,n,p){ if (n>1 && n<=16 && m>0 && m<=16 && p>=0 && p<=255){ this.qrcode_structureappend_m=m this.qrcode_structureappend_n=n this.qrcode_structureappend_parity=p }},cal_structureappend_parity : function(originaldata){ if (originaldata.length>1) { structureappend_parity=0 originaldata.each_byte(function(b){structureappend_parity^=b}) return structureappend_parity }},make_qrcode : function(qrcode_data_string){ var data_length, data_counter, data_value, data_bits, codeword_num_plus, codeword_num_counter_value, i, alphanumeric_character_hash, total_data_bits, ecc_character_hash, ec, max_data_bits_array, j, max_data_bits, max_codewords_array, max_codewords, max_modules_1side, matrix_remain_bit, byte_num, filename, fp, matx, maty, masks, fi_x, fi_y, rs_ecc_codewords, rso, matrix_x_array, matrix_y_array, mask_array, rs_block_order, format_information_x2, format_information_y2, format_information_x1, format_information_y1, max_data_codewords, rs_cal_table_array, frame_data, codewords_counter, codewords, remaining_bits, buffer, buffer_bits, flag, rs_block_number, rs_temp, rs_block_order_num, rs_codewords, rs_data_codewords, rstemp, first, left_chr, cal, matrix_content, codeword_i, codeword_bits_number, matrix_remain, remain_bit_temp, min_demerit_score, hor_master, ver_master, k, l, all_matrix, demerit_n1, ptn_temp, bit, bit_r, bit_mask, hor, ver, ver_and, ver_or, n1_search, n2_search1, n2_search2, n3_search, n4_search, hor_temp, demerit_n3, demerit_n4, demerit_n2, demerit_score, mask_number, mask_content, format_information_value, format_information_array, content, out, mxe;
data_length = qrcode_data_string.length;
if (data_length <= 0) {
throw "Data do not exist";
return 0;
}
data_counter = 0;
data_value = [];
data_bits = [];
if (this.qrcode_structureappend_n > 1) {
data_value[0] = 3;
data_bits[0] = 4;
data_value[1] = this.qrcode_structureappend_m - 1;
data_bits[1] = 4;
data_value[2] = this.qrcode_structureappend_n - 1;
data_bits[2] = 4;
data_value[3] = this.qrcode_structureappend_parity;
data_bits[3] = 8;
data_counter = 4;
}
data_bits[data_counter] = 4;
if (/[^0-9]/.test(qrcode_data_string)) {
if (/[^0-9A-Z \$\*\%\+\-\.\/\:]/.test(qrcode_data_string)) {
codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8];
data_value[data_counter] = 4;
data_counter += 1;
data_value[data_counter] = data_length;
data_bits[data_counter] = 8;
codeword_num_counter_value = data_counter;
data_counter += 1;
i = 0;
while (i < data_length) {
data_value[data_counter] = qrcode_data_string.charCodeAt(i);
data_bits[data_counter] = 8;
data_counter += 1;
i += 1;
}
} else {
codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4];
data_value[data_counter] = 2;
data_counter += 1;
data_value[data_counter] = data_length;
data_bits[data_counter] = 9;
codeword_num_counter_value = data_counter;
alphanumeric_character_hash = {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, A:10, B:11, C:12, D:13, E:14, F:15, G:16, H:17, I:18, J:19, K:20, L:21, M:22, N:23, O:24, P:25, Q:26, R:27, S:28, T:29, U:30, V:31, W:32, X:33, Y:34, Z:35, ' ':36, $:37, '%':38, '*':39, '+':40, '-':41, '.':42, '/':43, ':':44};
i = 0;
data_counter += 1;
while (i < data_length) {
if ((i % 2) == 0) {
data_value[data_counter] = alphanumeric_character_hash[qrcode_data_string.ruby_slice(i, 1)];
data_bits[data_counter] = 6;
} else {
data_value[data_counter] = data_value[data_counter] * 45 + alphanumeric_character_hash[qrcode_data_string.ruby_slice(i, 1)];
data_bits[data_counter] = 11;
data_counter += 1;
}
i += 1;
}
}
} else {
codeword_num_plus = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4];
data_value[data_counter] = 1;
data_counter += 1;
data_value[data_counter] = data_length;
data_bits[data_counter] = 10;
codeword_num_counter_value = data_counter;
i = 0;
data_counter += 1;
while (i < data_length) {
if ((i % 3) == 0) {
data_value[data_counter] = qrcode_data_string.ruby_slice(i, 1).to_i();
data_bits[data_counter] = 4;
} else {
data_value[data_counter] = data_value[data_counter] * 10 + qrcode_data_string.ruby_slice(i, 1).to_i();
if ((i % 3) == 1) {
data_bits[data_counter] = 7;
} else {
data_bits[data_counter] = 10;
data_counter += 1;
}
}
i += 1;
}
}
if (typeof data_bits[data_counter] == "undefined") {
} else {
if (data_bits[data_counter] > 0) {
data_counter += 1;
}
}
i = 0;
total_data_bits = 0;
while (i < data_counter) {
total_data_bits += data_bits[i];
i += 1;
}
ecc_character_hash = {L:1, l:1, M:0, m:0, Q:3, q:3, H:2, h:2};
ec = ecc_character_hash[this.qrcode_error_correct];
if (!ec) {
ec = 0;
}
max_data_bits_array = [0, 128, 224, 352, 512, 688, 864, 992, 1232, 1456, 1728, 2032, 2320, 2672, 2920, 3320, 3624, 4056, 4504, 5016, 5352, 5712, 6256, 6880, 7312, 8000, 8496, 9024, 9544, 10136, 10984, 11640, 12328, 13048, 13800, 14496, 15312, 15936, 16816, 17728, 18672, 152, 272, 440, 640, 864, 1088, 1248, 1552, 1856, 2192, 2592, 2960, 3424, 3688, 4184, 4712, 5176, 5768, 6360, 6888, 7456, 8048, 8752, 9392, 10208, 10960, 11744, 12248, 13048, 13880, 14744, 15640, 16568, 17528, 18448, 19472, 20528, 21616, 22496, 23648, 72, 128, 208, 288, 368, 480, 528, 688, 800, 976, 1120, 1264, 1440, 1576, 1784, 2024, 2264, 2504, 2728, 3080, 3248, 3536, 3712, 4112, 4304, 4768, 5024, 5288, 5608, 5960, 6344, 6760, 7208, 7688, 7888, 8432, 8768, 9136, 9776, 10208, 104, 176, 272, 384, 496, 608, 704, 880, 1056, 1232, 1440, 1648, 1952, 2088, 2360, 2600, 2936, 3176, 3560, 3880, 4096, 4544, 4912, 5312, 5744, 6032, 6464, 6968, 7288, 7880, 8264, 8920, 9368, 9848, 10288, 10832, 11408, 12016, 12656, 13328];
if (this.qrcode_version == 0) {
i = 1 + 40 * ec;
j = i + 39;
this.qrcode_version = 1;
while (i <= j) {
if ((max_data_bits_array[i]) >= total_data_bits + codeword_num_plus[this.qrcode_version]) {
max_data_bits = max_data_bits_array[i];
break;
}
i += 1;
this.qrcode_version += 1;
}
} else {
max_data_bits = max_data_bits_array[this.qrcode_version + 40 * ec];
}
total_data_bits += codeword_num_plus[this.qrcode_version];
data_bits[codeword_num_counter_value] += codeword_num_plus[this.qrcode_version];
max_codewords_array = [0, 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706];
max_codewords = max_codewords_array[this.qrcode_version];
max_modules_1side = 17 + (this.qrcode_version << 2);
matrix_remain_bit = [0, 0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0];
try {
byte_num = matrix_remain_bit[this.qrcode_version] + (max_codewords << 3); filename = this.path + "/qrv" + this.qrcode_version.to_s() + "_" + ec.to_s() + ".dat";
fp = File.open(filename, "rb_bin");
matx = fp.read(byte_num);
maty = fp.read(byte_num);
masks = fp.read(byte_num);
fi_x = fp.read(15);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -