Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 4 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. let QRCode = {};
  2. (function() {
  3. /**
  4. * 获取单个字符的utf8编码
  5. * unicode BMP平面约65535个字符
  6. * @param {num} code
  7. * return {array}
  8. */
  9. function unicodeFormat8(code) {
  10. // 1 byte
  11. var c0, c1, c2;
  12. if (code < 128) {
  13. return [code];
  14. // 2 bytes
  15. } else if (code < 2048) {
  16. c0 = 192 + (code >> 6);
  17. c1 = 128 + (code & 63);
  18. return [c0, c1];
  19. // 3 bytes
  20. } else {
  21. c0 = 224 + (code >> 12);
  22. c1 = 128 + (code >> 6 & 63);
  23. c2 = 128 + (code & 63);
  24. return [c0, c1, c2];
  25. }
  26. }
  27. /**
  28. * 获取字符串的utf8编码字节串
  29. * @param {string} string
  30. * @return {array}
  31. */
  32. function getUTF8Bytes(string) {
  33. var utf8codes = [];
  34. for (var i = 0; i < string.length; i++) {
  35. var code = string.charCodeAt(i);
  36. var utf8 = unicodeFormat8(code);
  37. for (var j = 0; j < utf8.length; j++) {
  38. utf8codes.push(utf8[j]);
  39. }
  40. }
  41. return utf8codes;
  42. }
  43. /**
  44. * 二维码算法实现
  45. * @param {string} data 要编码的信息字符串
  46. * @param {num} errorCorrectLevel 纠错等级
  47. */
  48. function QRCodeAlg(data, errorCorrectLevel) {
  49. this.typeNumber = -1; //版本
  50. this.errorCorrectLevel = errorCorrectLevel;
  51. this.modules = null; //二维矩阵,存放最终结果
  52. this.moduleCount = 0; //矩阵大小
  53. this.dataCache = null; //数据缓存
  54. this.rsBlocks = null; //版本数据信息
  55. this.totalDataCount = -1; //可使用的数据量
  56. this.data = data;
  57. this.utf8bytes = getUTF8Bytes(data);
  58. this.make();
  59. }
  60. QRCodeAlg.prototype = {
  61. constructor: QRCodeAlg,
  62. /**
  63. * 获取二维码矩阵大小
  64. * @return {num} 矩阵大小
  65. */
  66. getModuleCount: function() {
  67. return this.moduleCount;
  68. },
  69. /**
  70. * 编码
  71. */
  72. make: function() {
  73. this.getRightType();
  74. this.dataCache = this.createData();
  75. this.createQrcode();
  76. },
  77. /**
  78. * 设置二位矩阵功能图形
  79. * @param {bool} test 表示是否在寻找最好掩膜阶段
  80. * @param {num} maskPattern 掩膜的版本
  81. */
  82. makeImpl: function(maskPattern) {
  83. this.moduleCount = this.typeNumber * 4 + 17;
  84. this.modules = new Array(this.moduleCount);
  85. for (var row = 0; row < this.moduleCount; row++) {
  86. this.modules[row] = new Array(this.moduleCount);
  87. }
  88. this.setupPositionProbePattern(0, 0);
  89. this.setupPositionProbePattern(this.moduleCount - 7, 0);
  90. this.setupPositionProbePattern(0, this.moduleCount - 7);
  91. this.setupPositionAdjustPattern();
  92. this.setupTimingPattern();
  93. this.setupTypeInfo(true, maskPattern);
  94. if (this.typeNumber >= 7) {
  95. this.setupTypeNumber(true);
  96. }
  97. this.mapData(this.dataCache, maskPattern);
  98. },
  99. /**
  100. * 设置二维码的位置探测图形
  101. * @param {num} row 探测图形的中心横坐标
  102. * @param {num} col 探测图形的中心纵坐标
  103. */
  104. setupPositionProbePattern: function(row, col) {
  105. for (var r = -1; r <= 7; r++) {
  106. if (row + r <= -1 || this.moduleCount <= row + r) continue;
  107. for (var c = -1; c <= 7; c++) {
  108. if (col + c <= -1 || this.moduleCount <= col + c) continue;
  109. if ((0 <= r && r <= 6 && (c == 0 || c == 6)) || (0 <= c && c <= 6 && (r == 0 || r == 6)) || (2 <= r && r <= 4 && 2 <= c &&
  110. c <= 4)) {
  111. this.modules[row + r][col + c] = true;
  112. } else {
  113. this.modules[row + r][col + c] = false;
  114. }
  115. }
  116. }
  117. },
  118. /**
  119. * 创建二维码
  120. * @return {[type]} [description]
  121. */
  122. createQrcode: function() {
  123. var minLostPoint = 0;
  124. var pattern = 0;
  125. var bestModules = null;
  126. for (var i = 0; i < 8; i++) {
  127. this.makeImpl(i);
  128. var lostPoint = QRUtil.getLostPoint(this);
  129. if (i == 0 || minLostPoint > lostPoint) {
  130. minLostPoint = lostPoint;
  131. pattern = i;
  132. bestModules = this.modules;
  133. }
  134. }
  135. this.modules = bestModules;
  136. this.setupTypeInfo(false, pattern);
  137. if (this.typeNumber >= 7) {
  138. this.setupTypeNumber(false);
  139. }
  140. },
  141. /**
  142. * 设置定位图形
  143. * @return {[type]} [description]
  144. */
  145. setupTimingPattern: function() {
  146. for (var r = 8; r < this.moduleCount - 8; r++) {
  147. if (this.modules[r][6] != null) {
  148. continue;
  149. }
  150. this.modules[r][6] = (r % 2 == 0);
  151. if (this.modules[6][r] != null) {
  152. continue;
  153. }
  154. this.modules[6][r] = (r % 2 == 0);
  155. }
  156. },
  157. /**
  158. * 设置矫正图形
  159. * @return {[type]} [description]
  160. */
  161. setupPositionAdjustPattern: function() {
  162. var pos = QRUtil.getPatternPosition(this.typeNumber);
  163. for (var i = 0; i < pos.length; i++) {
  164. for (var j = 0; j < pos.length; j++) {
  165. var row = pos[i];
  166. var col = pos[j];
  167. if (this.modules[row][col] != null) {
  168. continue;
  169. }
  170. for (var r = -2; r <= 2; r++) {
  171. for (var c = -2; c <= 2; c++) {
  172. if (r == -2 || r == 2 || c == -2 || c == 2 || (r == 0 && c == 0)) {
  173. this.modules[row + r][col + c] = true;
  174. } else {
  175. this.modules[row + r][col + c] = false;
  176. }
  177. }
  178. }
  179. }
  180. }
  181. },
  182. /**
  183. * 设置版本信息(7以上版本才有)
  184. * @param {bool} test 是否处于判断最佳掩膜阶段
  185. * @return {[type]} [description]
  186. */
  187. setupTypeNumber: function(test) {
  188. var bits = QRUtil.getBCHTypeNumber(this.typeNumber);
  189. for (var i = 0; i < 18; i++) {
  190. var mod = (!test && ((bits >> i) & 1) == 1);
  191. this.modules[Math.floor(i / 3)][i % 3 + this.moduleCount - 8 - 3] = mod;
  192. this.modules[i % 3 + this.moduleCount - 8 - 3][Math.floor(i / 3)] = mod;
  193. }
  194. },
  195. /**
  196. * 设置格式信息(纠错等级和掩膜版本)
  197. * @param {bool} test
  198. * @param {num} maskPattern 掩膜版本
  199. * @return {}
  200. */
  201. setupTypeInfo: function(test, maskPattern) {
  202. var data = (QRErrorCorrectLevel[this.errorCorrectLevel] << 3) | maskPattern;
  203. var bits = QRUtil.getBCHTypeInfo(data);
  204. // vertical
  205. for (var i = 0; i < 15; i++) {
  206. var mod = (!test && ((bits >> i) & 1) == 1);
  207. if (i < 6) {
  208. this.modules[i][8] = mod;
  209. } else if (i < 8) {
  210. this.modules[i + 1][8] = mod;
  211. } else {
  212. this.modules[this.moduleCount - 15 + i][8] = mod;
  213. }
  214. // horizontal
  215. var mod = (!test && ((bits >> i) & 1) == 1);
  216. if (i < 8) {
  217. this.modules[8][this.moduleCount - i - 1] = mod;
  218. } else if (i < 9) {
  219. this.modules[8][15 - i - 1 + 1] = mod;
  220. } else {
  221. this.modules[8][15 - i - 1] = mod;
  222. }
  223. }
  224. // fixed module
  225. this.modules[this.moduleCount - 8][8] = (!test);
  226. },
  227. /**
  228. * 数据编码
  229. * @return {[type]} [description]
  230. */
  231. createData: function() {
  232. var buffer = new QRBitBuffer();
  233. var lengthBits = this.typeNumber > 9 ? 16 : 8;
  234. buffer.put(4, 4); //添加模式
  235. buffer.put(this.utf8bytes.length, lengthBits);
  236. for (var i = 0, l = this.utf8bytes.length; i < l; i++) {
  237. buffer.put(this.utf8bytes[i], 8);
  238. }
  239. if (buffer.length + 4 <= this.totalDataCount * 8) {
  240. buffer.put(0, 4);
  241. }
  242. // padding
  243. while (buffer.length % 8 != 0) {
  244. buffer.putBit(false);
  245. }
  246. // padding
  247. while (true) {
  248. if (buffer.length >= this.totalDataCount * 8) {
  249. break;
  250. }
  251. buffer.put(QRCodeAlg.PAD0, 8);
  252. if (buffer.length >= this.totalDataCount * 8) {
  253. break;
  254. }
  255. buffer.put(QRCodeAlg.PAD1, 8);
  256. }
  257. return this.createBytes(buffer);
  258. },
  259. /**
  260. * 纠错码编码
  261. * @param {buffer} buffer 数据编码
  262. * @return {[type]}
  263. */
  264. createBytes: function(buffer) {
  265. var offset = 0;
  266. var maxDcCount = 0;
  267. var maxEcCount = 0;
  268. var length = this.rsBlock.length / 3;
  269. var rsBlocks = new Array();
  270. for (var i = 0; i < length; i++) {
  271. var count = this.rsBlock[i * 3 + 0];
  272. var totalCount = this.rsBlock[i * 3 + 1];
  273. var dataCount = this.rsBlock[i * 3 + 2];
  274. for (var j = 0; j < count; j++) {
  275. rsBlocks.push([dataCount, totalCount]);
  276. }
  277. }
  278. var dcdata = new Array(rsBlocks.length);
  279. var ecdata = new Array(rsBlocks.length);
  280. for (var r = 0; r < rsBlocks.length; r++) {
  281. var dcCount = rsBlocks[r][0];
  282. var ecCount = rsBlocks[r][1] - dcCount;
  283. maxDcCount = Math.max(maxDcCount, dcCount);
  284. maxEcCount = Math.max(maxEcCount, ecCount);
  285. dcdata[r] = new Array(dcCount);
  286. for (var i = 0; i < dcdata[r].length; i++) {
  287. dcdata[r][i] = 0xff & buffer.buffer[i + offset];
  288. }
  289. offset += dcCount;
  290. var rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount);
  291. var rawPoly = new QRPolynomial(dcdata[r], rsPoly.getLength() - 1);
  292. var modPoly = rawPoly.mod(rsPoly);
  293. ecdata[r] = new Array(rsPoly.getLength() - 1);
  294. for (var i = 0; i < ecdata[r].length; i++) {
  295. var modIndex = i + modPoly.getLength() - ecdata[r].length;
  296. ecdata[r][i] = (modIndex >= 0) ? modPoly.get(modIndex) : 0;
  297. }
  298. }
  299. var data = new Array(this.totalDataCount);
  300. var index = 0;
  301. for (var i = 0; i < maxDcCount; i++) {
  302. for (var r = 0; r < rsBlocks.length; r++) {
  303. if (i < dcdata[r].length) {
  304. data[index++] = dcdata[r][i];
  305. }
  306. }
  307. }
  308. for (var i = 0; i < maxEcCount; i++) {
  309. for (var r = 0; r < rsBlocks.length; r++) {
  310. if (i < ecdata[r].length) {
  311. data[index++] = ecdata[r][i];
  312. }
  313. }
  314. }
  315. return data;
  316. },
  317. /**
  318. * 布置模块,构建最终信息
  319. * @param {} data
  320. * @param {} maskPattern
  321. * @return {}
  322. */
  323. mapData: function(data, maskPattern) {
  324. var inc = -1;
  325. var row = this.moduleCount - 1;
  326. var bitIndex = 7;
  327. var byteIndex = 0;
  328. for (var col = this.moduleCount - 1; col > 0; col -= 2) {
  329. if (col == 6) col--;
  330. while (true) {
  331. for (var c = 0; c < 2; c++) {
  332. if (this.modules[row][col - c] == null) {
  333. var dark = false;
  334. if (byteIndex < data.length) {
  335. dark = (((data[byteIndex] >>> bitIndex) & 1) == 1);
  336. }
  337. var mask = QRUtil.getMask(maskPattern, row, col - c);
  338. if (mask) {
  339. dark = !dark;
  340. }
  341. this.modules[row][col - c] = dark;
  342. bitIndex--;
  343. if (bitIndex == -1) {
  344. byteIndex++;
  345. bitIndex = 7;
  346. }
  347. }
  348. }
  349. row += inc;
  350. if (row < 0 || this.moduleCount <= row) {
  351. row -= inc;
  352. inc = -inc;
  353. break;
  354. }
  355. }
  356. }
  357. }
  358. };
  359. /**
  360. * 填充字段
  361. */
  362. QRCodeAlg.PAD0 = 0xEC;
  363. QRCodeAlg.PAD1 = 0x11;
  364. //---------------------------------------------------------------------
  365. // 纠错等级对应的编码
  366. //---------------------------------------------------------------------
  367. var QRErrorCorrectLevel = [1, 0, 3, 2];
  368. //---------------------------------------------------------------------
  369. // 掩膜版本
  370. //---------------------------------------------------------------------
  371. var QRMaskPattern = {
  372. PATTERN000: 0,
  373. PATTERN001: 1,
  374. PATTERN010: 2,
  375. PATTERN011: 3,
  376. PATTERN100: 4,
  377. PATTERN101: 5,
  378. PATTERN110: 6,
  379. PATTERN111: 7
  380. };
  381. //---------------------------------------------------------------------
  382. // 工具类
  383. //---------------------------------------------------------------------
  384. var QRUtil = {
  385. /*
  386. 每个版本矫正图形的位置
  387. */
  388. PATTERN_POSITION_TABLE: [
  389. [],
  390. [6, 18],
  391. [6, 22],
  392. [6, 26],
  393. [6, 30],
  394. [6, 34],
  395. [6, 22, 38],
  396. [6, 24, 42],
  397. [6, 26, 46],
  398. [6, 28, 50],
  399. [6, 30, 54],
  400. [6, 32, 58],
  401. [6, 34, 62],
  402. [6, 26, 46, 66],
  403. [6, 26, 48, 70],
  404. [6, 26, 50, 74],
  405. [6, 30, 54, 78],
  406. [6, 30, 56, 82],
  407. [6, 30, 58, 86],
  408. [6, 34, 62, 90],
  409. [6, 28, 50, 72, 94],
  410. [6, 26, 50, 74, 98],
  411. [6, 30, 54, 78, 102],
  412. [6, 28, 54, 80, 106],
  413. [6, 32, 58, 84, 110],
  414. [6, 30, 58, 86, 114],
  415. [6, 34, 62, 90, 118],
  416. [6, 26, 50, 74, 98, 122],
  417. [6, 30, 54, 78, 102, 126],
  418. [6, 26, 52, 78, 104, 130],
  419. [6, 30, 56, 82, 108, 134],
  420. [6, 34, 60, 86, 112, 138],
  421. [6, 30, 58, 86, 114, 142],
  422. [6, 34, 62, 90, 118, 146],
  423. [6, 30, 54, 78, 102, 126, 150],
  424. [6, 24, 50, 76, 102, 128, 154],
  425. [6, 28, 54, 80, 106, 132, 158],
  426. [6, 32, 58, 84, 110, 136, 162],
  427. [6, 26, 54, 82, 110, 138, 166],
  428. [6, 30, 58, 86, 114, 142, 170]
  429. ],
  430. G15: (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0),
  431. G18: (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0),
  432. G15_MASK: (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1),
  433. /*
  434. BCH编码格式信息
  435. */
  436. getBCHTypeInfo: function(data) {
  437. var d = data << 10;
  438. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15) >= 0) {
  439. d ^= (QRUtil.G15 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G15)));
  440. }
  441. return ((data << 10) | d) ^ QRUtil.G15_MASK;
  442. },
  443. /*
  444. BCH编码版本信息
  445. */
  446. getBCHTypeNumber: function(data) {
  447. var d = data << 12;
  448. while (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18) >= 0) {
  449. d ^= (QRUtil.G18 << (QRUtil.getBCHDigit(d) - QRUtil.getBCHDigit(QRUtil.G18)));
  450. }
  451. return (data << 12) | d;
  452. },
  453. /*
  454. 获取BCH位信息
  455. */
  456. getBCHDigit: function(data) {
  457. var digit = 0;
  458. while (data != 0) {
  459. digit++;
  460. data >>>= 1;
  461. }
  462. return digit;
  463. },
  464. /*
  465. 获取版本对应的矫正图形位置
  466. */
  467. getPatternPosition: function(typeNumber) {
  468. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  469. },
  470. /*
  471. 掩膜算法
  472. */
  473. getMask: function(maskPattern, i, j) {
  474. switch (maskPattern) {
  475. case QRMaskPattern.PATTERN000:
  476. return (i + j) % 2 == 0;
  477. case QRMaskPattern.PATTERN001:
  478. return i % 2 == 0;
  479. case QRMaskPattern.PATTERN010:
  480. return j % 3 == 0;
  481. case QRMaskPattern.PATTERN011:
  482. return (i + j) % 3 == 0;
  483. case QRMaskPattern.PATTERN100:
  484. return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 == 0;
  485. case QRMaskPattern.PATTERN101:
  486. return (i * j) % 2 + (i * j) % 3 == 0;
  487. case QRMaskPattern.PATTERN110:
  488. return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  489. case QRMaskPattern.PATTERN111:
  490. return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  491. default:
  492. throw new Error("bad maskPattern:" + maskPattern);
  493. }
  494. },
  495. /*
  496. 获取RS的纠错多项式
  497. */
  498. getErrorCorrectPolynomial: function(errorCorrectLength) {
  499. var a = new QRPolynomial([1], 0);
  500. for (var i = 0; i < errorCorrectLength; i++) {
  501. a = a.multiply(new QRPolynomial([1, QRMath.gexp(i)], 0));
  502. }
  503. return a;
  504. },
  505. /*
  506. 获取评价
  507. */
  508. getLostPoint: function(qrCode) {
  509. var moduleCount = qrCode.getModuleCount(),
  510. lostPoint = 0,
  511. darkCount = 0;
  512. for (var row = 0; row < moduleCount; row++) {
  513. var sameCount = 0;
  514. var head = qrCode.modules[row][0];
  515. for (var col = 0; col < moduleCount; col++) {
  516. var current = qrCode.modules[row][col];
  517. //level 3 评价
  518. if (col < moduleCount - 6) {
  519. if (current && !qrCode.modules[row][col + 1] && qrCode.modules[row][col + 2] && qrCode.modules[row][col + 3] && qrCode.modules[
  520. row][col + 4] && !qrCode.modules[row][col + 5] && qrCode.modules[row][col + 6]) {
  521. if (col < moduleCount - 10) {
  522. if (qrCode.modules[row][col + 7] && qrCode.modules[row][col + 8] && qrCode.modules[row][col + 9] && qrCode.modules[
  523. row][col + 10]) {
  524. lostPoint += 40;
  525. }
  526. } else if (col > 3) {
  527. if (qrCode.modules[row][col - 1] && qrCode.modules[row][col - 2] && qrCode.modules[row][col - 3] && qrCode.modules[
  528. row][col - 4]) {
  529. lostPoint += 40;
  530. }
  531. }
  532. }
  533. }
  534. //level 2 评价
  535. if ((row < moduleCount - 1) && (col < moduleCount - 1)) {
  536. var count = 0;
  537. if (current) count++;
  538. if (qrCode.modules[row + 1][col]) count++;
  539. if (qrCode.modules[row][col + 1]) count++;
  540. if (qrCode.modules[row + 1][col + 1]) count++;
  541. if (count == 0 || count == 4) {
  542. lostPoint += 3;
  543. }
  544. }
  545. //level 1 评价
  546. if (head ^ current) {
  547. sameCount++;
  548. } else {
  549. head = current;
  550. if (sameCount >= 5) {
  551. lostPoint += (3 + sameCount - 5);
  552. }
  553. sameCount = 1;
  554. }
  555. //level 4 评价
  556. if (current) {
  557. darkCount++;
  558. }
  559. }
  560. }
  561. for (var col = 0; col < moduleCount; col++) {
  562. var sameCount = 0;
  563. var head = qrCode.modules[0][col];
  564. for (var row = 0; row < moduleCount; row++) {
  565. var current = qrCode.modules[row][col];
  566. //level 3 评价
  567. if (row < moduleCount - 6) {
  568. if (current && !qrCode.modules[row + 1][col] && qrCode.modules[row + 2][col] && qrCode.modules[row + 3][col] && qrCode.modules[
  569. row + 4][col] && !qrCode.modules[row + 5][col] && qrCode.modules[row + 6][col]) {
  570. if (row < moduleCount - 10) {
  571. if (qrCode.modules[row + 7][col] && qrCode.modules[row + 8][col] && qrCode.modules[row + 9][col] && qrCode.modules[
  572. row + 10][col]) {
  573. lostPoint += 40;
  574. }
  575. } else if (row > 3) {
  576. if (qrCode.modules[row - 1][col] && qrCode.modules[row - 2][col] && qrCode.modules[row - 3][col] && qrCode.modules[
  577. row - 4][col]) {
  578. lostPoint += 40;
  579. }
  580. }
  581. }
  582. }
  583. //level 1 评价
  584. if (head ^ current) {
  585. sameCount++;
  586. } else {
  587. head = current;
  588. if (sameCount >= 5) {
  589. lostPoint += (3 + sameCount - 5);
  590. }
  591. sameCount = 1;
  592. }
  593. }
  594. }
  595. // LEVEL4
  596. var ratio = Math.abs(100 * darkCount / moduleCount / moduleCount - 50) / 5;
  597. lostPoint += ratio * 10;
  598. return lostPoint;
  599. }
  600. };
  601. //---------------------------------------------------------------------
  602. // QRMath使用的数学工具
  603. //---------------------------------------------------------------------
  604. var QRMath = {
  605. /*
  606. 将n转化为a^m
  607. */
  608. glog: function(n) {
  609. if (n < 1) {
  610. throw new Error("glog(" + n + ")");
  611. }
  612. return QRMath.LOG_TABLE[n];
  613. },
  614. /*
  615. 将a^m转化为n
  616. */
  617. gexp: function(n) {
  618. while (n < 0) {
  619. n += 255;
  620. }
  621. while (n >= 256) {
  622. n -= 255;
  623. }
  624. return QRMath.EXP_TABLE[n];
  625. },
  626. EXP_TABLE: new Array(256),
  627. LOG_TABLE: new Array(256)
  628. };
  629. for (var i = 0; i < 8; i++) {
  630. QRMath.EXP_TABLE[i] = 1 << i;
  631. }
  632. for (var i = 8; i < 256; i++) {
  633. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4] ^ QRMath.EXP_TABLE[i - 5] ^ QRMath.EXP_TABLE[i - 6] ^ QRMath.EXP_TABLE[i - 8];
  634. }
  635. for (var i = 0; i < 255; i++) {
  636. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  637. }
  638. //---------------------------------------------------------------------
  639. // QRPolynomial 多项式
  640. //---------------------------------------------------------------------
  641. /**
  642. * 多项式类
  643. * @param {Array} num 系数
  644. * @param {num} shift a^shift
  645. */
  646. function QRPolynomial(num, shift) {
  647. if (num.length == undefined) {
  648. throw new Error(num.length + "/" + shift);
  649. }
  650. var offset = 0;
  651. while (offset < num.length && num[offset] == 0) {
  652. offset++;
  653. }
  654. this.num = new Array(num.length - offset + shift);
  655. for (var i = 0; i < num.length - offset; i++) {
  656. this.num[i] = num[i + offset];
  657. }
  658. }
  659. QRPolynomial.prototype = {
  660. get: function(index) {
  661. return this.num[index];
  662. },
  663. getLength: function() {
  664. return this.num.length;
  665. },
  666. /**
  667. * 多项式乘法
  668. * @param {QRPolynomial} e 被乘多项式
  669. * @return {[type]} [description]
  670. */
  671. multiply: function(e) {
  672. var num = new Array(this.getLength() + e.getLength() - 1);
  673. for (var i = 0; i < this.getLength(); i++) {
  674. for (var j = 0; j < e.getLength(); j++) {
  675. num[i + j] ^= QRMath.gexp(QRMath.glog(this.get(i)) + QRMath.glog(e.get(j)));
  676. }
  677. }
  678. return new QRPolynomial(num, 0);
  679. },
  680. /**
  681. * 多项式模运算
  682. * @param {QRPolynomial} e 模多项式
  683. * @return {}
  684. */
  685. mod: function(e) {
  686. var tl = this.getLength(),
  687. el = e.getLength();
  688. if (tl - el < 0) {
  689. return this;
  690. }
  691. var num = new Array(tl);
  692. for (var i = 0; i < tl; i++) {
  693. num[i] = this.get(i);
  694. }
  695. while (num.length >= el) {
  696. var ratio = QRMath.glog(num[0]) - QRMath.glog(e.get(0));
  697. for (var i = 0; i < e.getLength(); i++) {
  698. num[i] ^= QRMath.gexp(QRMath.glog(e.get(i)) + ratio);
  699. }
  700. while (num[0] == 0) {
  701. num.shift();
  702. }
  703. }
  704. return new QRPolynomial(num, 0);
  705. }
  706. };
  707. //---------------------------------------------------------------------
  708. // RS_BLOCK_TABLE
  709. //---------------------------------------------------------------------
  710. /*
  711. 二维码各个版本信息[块数, 每块中的数据块数, 每块中的信息块数]
  712. */
  713. var RS_BLOCK_TABLE = [
  714. // L
  715. // M
  716. // Q
  717. // H
  718. // 1
  719. [1, 26, 19],
  720. [1, 26, 16],
  721. [1, 26, 13],
  722. [1, 26, 9],
  723. // 2
  724. [1, 44, 34],
  725. [1, 44, 28],
  726. [1, 44, 22],
  727. [1, 44, 16],
  728. // 3
  729. [1, 70, 55],
  730. [1, 70, 44],
  731. [2, 35, 17],
  732. [2, 35, 13],
  733. // 4
  734. [1, 100, 80],
  735. [2, 50, 32],
  736. [2, 50, 24],
  737. [4, 25, 9],
  738. // 5
  739. [1, 134, 108],
  740. [2, 67, 43],
  741. [2, 33, 15, 2, 34, 16],
  742. [2, 33, 11, 2, 34, 12],
  743. // 6
  744. [2, 86, 68],
  745. [4, 43, 27],
  746. [4, 43, 19],
  747. [4, 43, 15],
  748. // 7
  749. [2, 98, 78],
  750. [4, 49, 31],
  751. [2, 32, 14, 4, 33, 15],
  752. [4, 39, 13, 1, 40, 14],
  753. // 8
  754. [2, 121, 97],
  755. [2, 60, 38, 2, 61, 39],
  756. [4, 40, 18, 2, 41, 19],
  757. [4, 40, 14, 2, 41, 15],
  758. // 9
  759. [2, 146, 116],
  760. [3, 58, 36, 2, 59, 37],
  761. [4, 36, 16, 4, 37, 17],
  762. [4, 36, 12, 4, 37, 13],
  763. // 10
  764. [2, 86, 68, 2, 87, 69],
  765. [4, 69, 43, 1, 70, 44],
  766. [6, 43, 19, 2, 44, 20],
  767. [6, 43, 15, 2, 44, 16],
  768. // 11
  769. [4, 101, 81],
  770. [1, 80, 50, 4, 81, 51],
  771. [4, 50, 22, 4, 51, 23],
  772. [3, 36, 12, 8, 37, 13],
  773. // 12
  774. [2, 116, 92, 2, 117, 93],
  775. [6, 58, 36, 2, 59, 37],
  776. [4, 46, 20, 6, 47, 21],
  777. [7, 42, 14, 4, 43, 15],
  778. // 13
  779. [4, 133, 107],
  780. [8, 59, 37, 1, 60, 38],
  781. [8, 44, 20, 4, 45, 21],
  782. [12, 33, 11, 4, 34, 12],
  783. // 14
  784. [3, 145, 115, 1, 146, 116],
  785. [4, 64, 40, 5, 65, 41],
  786. [11, 36, 16, 5, 37, 17],
  787. [11, 36, 12, 5, 37, 13],
  788. // 15
  789. [5, 109, 87, 1, 110, 88],
  790. [5, 65, 41, 5, 66, 42],
  791. [5, 54, 24, 7, 55, 25],
  792. [11, 36, 12],
  793. // 16
  794. [5, 122, 98, 1, 123, 99],
  795. [7, 73, 45, 3, 74, 46],
  796. [15, 43, 19, 2, 44, 20],
  797. [3, 45, 15, 13, 46, 16],
  798. // 17
  799. [1, 135, 107, 5, 136, 108],
  800. [10, 74, 46, 1, 75, 47],
  801. [1, 50, 22, 15, 51, 23],
  802. [2, 42, 14, 17, 43, 15],
  803. // 18
  804. [5, 150, 120, 1, 151, 121],
  805. [9, 69, 43, 4, 70, 44],
  806. [17, 50, 22, 1, 51, 23],
  807. [2, 42, 14, 19, 43, 15],
  808. // 19
  809. [3, 141, 113, 4, 142, 114],
  810. [3, 70, 44, 11, 71, 45],
  811. [17, 47, 21, 4, 48, 22],
  812. [9, 39, 13, 16, 40, 14],
  813. // 20
  814. [3, 135, 107, 5, 136, 108],
  815. [3, 67, 41, 13, 68, 42],
  816. [15, 54, 24, 5, 55, 25],
  817. [15, 43, 15, 10, 44, 16],
  818. // 21
  819. [4, 144, 116, 4, 145, 117],
  820. [17, 68, 42],
  821. [17, 50, 22, 6, 51, 23],
  822. [19, 46, 16, 6, 47, 17],
  823. // 22
  824. [2, 139, 111, 7, 140, 112],
  825. [17, 74, 46],
  826. [7, 54, 24, 16, 55, 25],
  827. [34, 37, 13],
  828. // 23
  829. [4, 151, 121, 5, 152, 122],
  830. [4, 75, 47, 14, 76, 48],
  831. [11, 54, 24, 14, 55, 25],
  832. [16, 45, 15, 14, 46, 16],
  833. // 24
  834. [6, 147, 117, 4, 148, 118],
  835. [6, 73, 45, 14, 74, 46],
  836. [11, 54, 24, 16, 55, 25],
  837. [30, 46, 16, 2, 47, 17],
  838. // 25
  839. [8, 132, 106, 4, 133, 107],
  840. [8, 75, 47, 13, 76, 48],
  841. [7, 54, 24, 22, 55, 25],
  842. [22, 45, 15, 13, 46, 16],
  843. // 26
  844. [10, 142, 114, 2, 143, 115],
  845. [19, 74, 46, 4, 75, 47],
  846. [28, 50, 22, 6, 51, 23],
  847. [33, 46, 16, 4, 47, 17],
  848. // 27
  849. [8, 152, 122, 4, 153, 123],
  850. [22, 73, 45, 3, 74, 46],
  851. [8, 53, 23, 26, 54, 24],
  852. [12, 45, 15, 28, 46, 16],
  853. // 28
  854. [3, 147, 117, 10, 148, 118],
  855. [3, 73, 45, 23, 74, 46],
  856. [4, 54, 24, 31, 55, 25],
  857. [11, 45, 15, 31, 46, 16],
  858. // 29
  859. [7, 146, 116, 7, 147, 117],
  860. [21, 73, 45, 7, 74, 46],
  861. [1, 53, 23, 37, 54, 24],
  862. [19, 45, 15, 26, 46, 16],
  863. // 30
  864. [5, 145, 115, 10, 146, 116],
  865. [19, 75, 47, 10, 76, 48],
  866. [15, 54, 24, 25, 55, 25],
  867. [23, 45, 15, 25, 46, 16],
  868. // 31
  869. [13, 145, 115, 3, 146, 116],
  870. [2, 74, 46, 29, 75, 47],
  871. [42, 54, 24, 1, 55, 25],
  872. [23, 45, 15, 28, 46, 16],
  873. // 32
  874. [17, 145, 115],
  875. [10, 74, 46, 23, 75, 47],
  876. [10, 54, 24, 35, 55, 25],
  877. [19, 45, 15, 35, 46, 16],
  878. // 33
  879. [17, 145, 115, 1, 146, 116],
  880. [14, 74, 46, 21, 75, 47],
  881. [29, 54, 24, 19, 55, 25],
  882. [11, 45, 15, 46, 46, 16],
  883. // 34
  884. [13, 145, 115, 6, 146, 116],
  885. [14, 74, 46, 23, 75, 47],
  886. [44, 54, 24, 7, 55, 25],
  887. [59, 46, 16, 1, 47, 17],
  888. // 35
  889. [12, 151, 121, 7, 152, 122],
  890. [12, 75, 47, 26, 76, 48],
  891. [39, 54, 24, 14, 55, 25],
  892. [22, 45, 15, 41, 46, 16],
  893. // 36
  894. [6, 151, 121, 14, 152, 122],
  895. [6, 75, 47, 34, 76, 48],
  896. [46, 54, 24, 10, 55, 25],
  897. [2, 45, 15, 64, 46, 16],
  898. // 37
  899. [17, 152, 122, 4, 153, 123],
  900. [29, 74, 46, 14, 75, 47],
  901. [49, 54, 24, 10, 55, 25],
  902. [24, 45, 15, 46, 46, 16],
  903. // 38
  904. [4, 152, 122, 18, 153, 123],
  905. [13, 74, 46, 32, 75, 47],
  906. [48, 54, 24, 14, 55, 25],
  907. [42, 45, 15, 32, 46, 16],
  908. // 39
  909. [20, 147, 117, 4, 148, 118],
  910. [40, 75, 47, 7, 76, 48],
  911. [43, 54, 24, 22, 55, 25],
  912. [10, 45, 15, 67, 46, 16],
  913. // 40
  914. [19, 148, 118, 6, 149, 119],
  915. [18, 75, 47, 31, 76, 48],
  916. [34, 54, 24, 34, 55, 25],
  917. [20, 45, 15, 61, 46, 16]
  918. ];
  919. /**
  920. * 根据数据获取对应版本
  921. * @return {[type]} [description]
  922. */
  923. QRCodeAlg.prototype.getRightType = function() {
  924. for (var typeNumber = 1; typeNumber < 41; typeNumber++) {
  925. var rsBlock = RS_BLOCK_TABLE[(typeNumber - 1) * 4 + this.errorCorrectLevel];
  926. if (rsBlock == undefined) {
  927. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + this.errorCorrectLevel);
  928. }
  929. var length = rsBlock.length / 3;
  930. var totalDataCount = 0;
  931. for (var i = 0; i < length; i++) {
  932. var count = rsBlock[i * 3 + 0];
  933. var dataCount = rsBlock[i * 3 + 2];
  934. totalDataCount += dataCount * count;
  935. }
  936. var lengthBytes = typeNumber > 9 ? 2 : 1;
  937. if (this.utf8bytes.length + lengthBytes < totalDataCount || typeNumber == 40) {
  938. this.typeNumber = typeNumber;
  939. this.rsBlock = rsBlock;
  940. this.totalDataCount = totalDataCount;
  941. break;
  942. }
  943. }
  944. };
  945. //---------------------------------------------------------------------
  946. // QRBitBuffer
  947. //---------------------------------------------------------------------
  948. function QRBitBuffer() {
  949. this.buffer = new Array();
  950. this.length = 0;
  951. }
  952. QRBitBuffer.prototype = {
  953. get: function(index) {
  954. var bufIndex = Math.floor(index / 8);
  955. return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1);
  956. },
  957. put: function(num, length) {
  958. for (var i = 0; i < length; i++) {
  959. this.putBit(((num >>> (length - i - 1)) & 1));
  960. }
  961. },
  962. putBit: function(bit) {
  963. var bufIndex = Math.floor(this.length / 8);
  964. if (this.buffer.length <= bufIndex) {
  965. this.buffer.push(0);
  966. }
  967. if (bit) {
  968. this.buffer[bufIndex] |= (0x80 >>> (this.length % 8));
  969. }
  970. this.length++;
  971. }
  972. };
  973. // xzedit
  974. let qrcodeAlgObjCache = [];
  975. /**
  976. * 二维码构造函数,主要用于绘制
  977. * @param {参数列表} opt 传递参数
  978. * @return {}
  979. */
  980. QRCode = function(opt) {
  981. //设置默认参数
  982. this.options = {
  983. text: '',
  984. size: 256,
  985. correctLevel: 3,
  986. background: '#ffffff',
  987. foreground: '#000000',
  988. pdground: '#000000',
  989. image: '',
  990. imageSize: 30,
  991. canvasId: opt.canvasId,
  992. context: opt.context,
  993. usingComponents: opt.usingComponents,
  994. showLoading: opt.showLoading,
  995. loadingText: opt.loadingText,
  996. };
  997. if (typeof opt === 'string') { // 只编码ASCII字符串
  998. opt = {
  999. text: opt
  1000. };
  1001. }
  1002. if (opt) {
  1003. for (var i in opt) {
  1004. this.options[i] = opt[i];
  1005. }
  1006. }
  1007. //使用QRCodeAlg创建二维码结构
  1008. var qrCodeAlg = null;
  1009. for (var i = 0, l = qrcodeAlgObjCache.length; i < l; i++) {
  1010. if (qrcodeAlgObjCache[i].text == this.options.text && qrcodeAlgObjCache[i].text.correctLevel == this.options.correctLevel) {
  1011. qrCodeAlg = qrcodeAlgObjCache[i].obj;
  1012. break;
  1013. }
  1014. }
  1015. if (i == l) {
  1016. qrCodeAlg = new QRCodeAlg(this.options.text, this.options.correctLevel);
  1017. qrcodeAlgObjCache.push({
  1018. text: this.options.text,
  1019. correctLevel: this.options.correctLevel,
  1020. obj: qrCodeAlg
  1021. });
  1022. }
  1023. /**
  1024. * 计算矩阵点的前景色
  1025. * @param {Obj} config
  1026. * @param {Number} config.row 点x坐标
  1027. * @param {Number} config.col 点y坐标
  1028. * @param {Number} config.count 矩阵大小
  1029. * @param {Number} config.options 组件的options
  1030. * @return {String}
  1031. */
  1032. let getForeGround = function(config) {
  1033. var options = config.options;
  1034. if (options.pdground && (
  1035. (config.row > 1 && config.row < 5 && config.col > 1 && config.col < 5) ||
  1036. (config.row > (config.count - 6) && config.row < (config.count - 2) && config.col > 1 && config.col < 5) ||
  1037. (config.row > 1 && config.row < 5 && config.col > (config.count - 6) && config.col < (config.count - 2))
  1038. )) {
  1039. return options.pdground;
  1040. }
  1041. return options.foreground;
  1042. }
  1043. // 创建canvas
  1044. let createCanvas = function(options) {
  1045. if (options.showLoading) {
  1046. uni.showLoading({
  1047. title: options.loadingText,
  1048. mask: true
  1049. });
  1050. }
  1051. var ctx = uni.createCanvasContext(options.canvasId, options.context);
  1052. var count = qrCodeAlg.getModuleCount();
  1053. var ratioSize = options.size;
  1054. var ratioImgSize = options.imageSize;
  1055. //计算每个点的长宽
  1056. var tileW = (ratioSize / count).toPrecision(4);
  1057. var tileH = (ratioSize / count).toPrecision(4);
  1058. //绘制
  1059. for (var row = 0; row < count; row++) {
  1060. for (var col = 0; col < count; col++) {
  1061. var w = (Math.ceil((col + 1) * tileW) - Math.floor(col * tileW));
  1062. var h = (Math.ceil((row + 1) * tileW) - Math.floor(row * tileW));
  1063. var foreground = getForeGround({
  1064. row: row,
  1065. col: col,
  1066. count: count,
  1067. options: options
  1068. });
  1069. ctx.setFillStyle(qrCodeAlg.modules[row][col] ? foreground : options.background);
  1070. ctx.fillRect(Math.round(col * tileW), Math.round(row * tileH), w, h);
  1071. }
  1072. }
  1073. if (options.image) {
  1074. var x = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1075. var y = Number(((ratioSize - ratioImgSize) / 2).toFixed(2));
  1076. drawRoundedRect(ctx, x, y, ratioImgSize, ratioImgSize, 2, 6, true, true)
  1077. ctx.drawImage(options.image, x, y, ratioImgSize, ratioImgSize);
  1078. // 画圆角矩形
  1079. function drawRoundedRect(ctxi, x, y, width, height, r, lineWidth, fill, stroke) {
  1080. ctxi.setLineWidth(lineWidth);
  1081. ctxi.setFillStyle(options.background);
  1082. ctxi.setStrokeStyle(options.background);
  1083. ctxi.beginPath(); // draw top and top right corner
  1084. ctxi.moveTo(x + r, y);
  1085. ctxi.arcTo(x + width, y, x + width, y + r, r); // draw right side and bottom right corner
  1086. ctxi.arcTo(x + width, y + height, x + width - r, y + height, r); // draw bottom and bottom left corner
  1087. ctxi.arcTo(x, y + height, x, y + height - r, r); // draw left and top left corner
  1088. ctxi.arcTo(x, y, x + r, y, r);
  1089. ctxi.closePath();
  1090. if (fill) {
  1091. ctxi.fill();
  1092. }
  1093. if (stroke) {
  1094. ctxi.stroke();
  1095. }
  1096. }
  1097. }
  1098. setTimeout(() => {
  1099. // #ifndef MP-DINGTALK
  1100. ctx.draw(true, () => {
  1101. // 保存到临时区域
  1102. setTimeout(() => {
  1103. uni.canvasToTempFilePath({
  1104. width: options.width,
  1105. height: options.height,
  1106. destWidth: options.width,
  1107. destHeight: options.height,
  1108. canvasId: options.canvasId,
  1109. quality: Number(1),
  1110. success: function(res) {
  1111. if (options.cbResult) {
  1112. // 由于官方还没有统一此接口的输出字段,所以先判定下 支付宝为 res.apFilePath
  1113. if (!empty(res.tempFilePath)) {
  1114. options.cbResult(res.tempFilePath)
  1115. } else if (!empty(res.apFilePath)) {
  1116. options.cbResult(res.apFilePath)
  1117. } else {
  1118. options.cbResult(res.tempFilePath)
  1119. }
  1120. }
  1121. },
  1122. fail: function(res) {
  1123. if (options.cbResult) {
  1124. options.cbResult(res)
  1125. }
  1126. },
  1127. complete: function() {
  1128. uni.hideLoading();
  1129. },
  1130. }, options.context);
  1131. }, options.text.length + 100);
  1132. });
  1133. // #endif
  1134. // #ifdef MP-DINGTALK
  1135. ctx.draw(true);
  1136. // 保存到临时区域
  1137. setTimeout(() => {
  1138. ctx.toTempFilePath({
  1139. width: options.width,
  1140. height: options.height,
  1141. destWidth: options.width,
  1142. destHeight: options.height,
  1143. success: function(res) {
  1144. if (options.cbResult) {
  1145. options.cbResult(res.filePath)
  1146. }
  1147. },
  1148. fail: function(res) {
  1149. if (options.cbResult) {
  1150. options.cbResult(res)
  1151. }
  1152. },
  1153. complete: function() {
  1154. uni.hideLoading();
  1155. }
  1156. })
  1157. }, options.text.length + 100);
  1158. // #endif
  1159. }, options.usingComponents ? 0 : 150);
  1160. }
  1161. createCanvas(this.options);
  1162. // 空判定
  1163. let empty = function(v) {
  1164. let tp = typeof v,
  1165. rt = false;
  1166. if (tp == "number" && String(v) == "") {
  1167. rt = true
  1168. } else if (tp == "undefined") {
  1169. rt = true
  1170. } else if (tp == "object") {
  1171. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  1172. } else if (tp == "string") {
  1173. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  1174. } else if (tp == "function") {
  1175. rt = false
  1176. }
  1177. return rt
  1178. }
  1179. };
  1180. QRCode.prototype.clear = function(fn) {
  1181. var ctx = uni.createCanvasContext(this.options.canvasId, this.options.context)
  1182. ctx.clearRect(0, 0, this.options.size, this.options.size)
  1183. ctx.draw(false, () => {
  1184. if (fn) {
  1185. fn()
  1186. }
  1187. })
  1188. };
  1189. })()
  1190. export default QRCode