You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

QRCodeCS.cs 35 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. //---------------------------------------------------------------------
  2. // QRCode for C#4.0 Silverlight is translation of QRCode for JavaScript
  3. // https://github.com/jeromeetienne/jquery-qrcode/
  4. //
  5. // Copyright (c) 2009 Kazuhiko Arase
  6. //
  7. // URL: http://www.d-project.com/
  8. //
  9. // Licensed under the MIT license:
  10. // http://www.opensource.org/licenses/mit-license.php
  11. //
  12. // The word "QR Code" is registered trademark of
  13. // DENSO WAVE INCORPORATED
  14. // http://www.denso-wave.com/qrcode/faqpatent-e.html
  15. //
  16. //---------------------------------------------------------------------
  17. namespace QRCode4CS
  18. {
  19. using System;
  20. using System.Collections.Generic;
  21. public class Error : Exception
  22. {
  23. public Error() { }
  24. public Error(string message) : base(message) { }
  25. public Error(string message, Exception inner) : base(message, inner) { }
  26. }
  27. public enum QRMode : int
  28. {
  29. MODE_NUMBER = 1 << 0,
  30. MODE_ALPHA_NUM = 1 << 1,
  31. MODE_8BIT_BYTE = 1 << 2,
  32. MODE_KANJI = 1 << 3
  33. }
  34. public enum QRErrorCorrectLevel : int
  35. {
  36. L = 1,
  37. M = 0,
  38. Q = 3,
  39. H = 2
  40. }
  41. public enum QRMaskPattern : int
  42. {
  43. PATTERN000 = 0,
  44. PATTERN001 = 1,
  45. PATTERN010 = 2,
  46. PATTERN011 = 3,
  47. PATTERN100 = 4,
  48. PATTERN101 = 5,
  49. PATTERN110 = 6,
  50. PATTERN111 = 7
  51. }
  52. public struct Options
  53. {
  54. public int Width { get; set; }
  55. public int Height { get; set; }
  56. public QRErrorCorrectLevel CorrectLevel { get; set; }
  57. public int TypeNumber { get; set; }
  58. public string Text { get; set; }
  59. public Options(string text)
  60. : this()
  61. {
  62. Width = 256;
  63. Height = 256;
  64. TypeNumber = 4;
  65. CorrectLevel = QRErrorCorrectLevel.H;
  66. Text = text;
  67. }
  68. }
  69. internal static class QRUtil
  70. {
  71. internal const int G15 = (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0);
  72. internal const int G18 = (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0);
  73. internal const int G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1);
  74. internal static readonly int[][] PATTERN_POSITION_TABLE = new int[][] {
  75. new int[] {},
  76. new int [] {6, 18},
  77. new int [] {6, 22},
  78. new int [] {6, 26},
  79. new int [] {6, 30},
  80. new int [] {6, 34},
  81. new int [] {6, 22, 38},
  82. new int [] {6, 24, 42},
  83. new int [] {6, 26, 46},
  84. new int [] {6, 28, 50},
  85. new int [] {6, 30, 54},
  86. new int [] {6, 32, 58},
  87. new int [] {6, 34, 62},
  88. new int [] {6, 26, 46, 66},
  89. new int [] {6, 26, 48, 70},
  90. new int [] {6, 26, 50, 74},
  91. new int [] {6, 30, 54, 78},
  92. new int [] {6, 30, 56, 82},
  93. new int [] {6, 30, 58, 86},
  94. new int [] {6, 34, 62, 90},
  95. new int [] {6, 28, 50, 72, 94},
  96. new int [] {6, 26, 50, 74, 98},
  97. new int [] {6, 30, 54, 78, 102},
  98. new int [] {6, 28, 54, 80, 106},
  99. new int [] {6, 32, 58, 84, 110},
  100. new int [] {6, 30, 58, 86, 114},
  101. new int [] {6, 34, 62, 90, 118},
  102. new int [] {6, 26, 50, 74, 98, 122},
  103. new int [] {6, 30, 54, 78, 102, 126},
  104. new int [] {6, 26, 52, 78, 104, 130},
  105. new int [] {6, 30, 56, 82, 108, 134},
  106. new int [] {6, 34, 60, 86, 112, 138},
  107. new int [] {6, 30, 58, 86, 114, 142},
  108. new int [] {6, 34, 62, 90, 118, 146},
  109. new int [] {6, 30, 54, 78, 102, 126, 150},
  110. new int [] {6, 24, 50, 76, 102, 128, 154},
  111. new int [] {6, 28, 54, 80, 106, 132, 158},
  112. new int [] {6, 32, 58, 84, 110, 136, 162},
  113. new int [] {6, 26, 54, 82, 110, 138, 166},
  114. new int [] {6, 30, 58, 86, 114, 142, 170}
  115. };
  116. internal static int GetLengthInBits(QRMode mode, int type)
  117. {
  118. if (1 <= type && type < 10)
  119. {
  120. // 1 - 9
  121. switch (mode)
  122. {
  123. case QRMode.MODE_NUMBER: return 10;
  124. case QRMode.MODE_ALPHA_NUM: return 9;
  125. case QRMode.MODE_8BIT_BYTE: return 8;
  126. case QRMode.MODE_KANJI: return 8;
  127. default:
  128. throw new Error("mode:" + mode);
  129. }
  130. }
  131. else if (type < 27)
  132. {
  133. // 10 - 26
  134. switch (mode)
  135. {
  136. case QRMode.MODE_NUMBER: return 12;
  137. case QRMode.MODE_ALPHA_NUM: return 11;
  138. case QRMode.MODE_8BIT_BYTE: return 16;
  139. case QRMode.MODE_KANJI: return 10;
  140. default:
  141. throw new Error("mode:" + mode);
  142. }
  143. }
  144. else if (type < 41)
  145. {
  146. // 27 - 40
  147. switch (mode)
  148. {
  149. case QRMode.MODE_NUMBER: return 14;
  150. case QRMode.MODE_ALPHA_NUM: return 13;
  151. case QRMode.MODE_8BIT_BYTE: return 16;
  152. case QRMode.MODE_KANJI: return 12;
  153. default:
  154. throw new Error("mode:" + mode);
  155. }
  156. }
  157. else
  158. {
  159. throw new Error("type:" + type);
  160. }
  161. }
  162. internal static double GetLostPoint(QRCode qrCode)
  163. {
  164. int moduleCount = qrCode.GetModuleCount();
  165. double lostPoint = 0;
  166. for (int row = 0; row < moduleCount; row++)
  167. {
  168. for (int col = 0; col < moduleCount; col++)
  169. {
  170. var sameCount = 0;
  171. var dark = qrCode.IsDark(row, col);
  172. for (var r = -1; r <= 1; r++)
  173. {
  174. if (row + r < 0 || moduleCount <= row + r)
  175. {
  176. continue;
  177. }
  178. for (var c = -1; c <= 1; c++)
  179. {
  180. if (col + c < 0 || moduleCount <= col + c)
  181. {
  182. continue;
  183. }
  184. if (r == 0 && c == 0)
  185. {
  186. continue;
  187. }
  188. if (dark == qrCode.IsDark((int)((int)row + r), (int)((int)col + c)))
  189. {
  190. sameCount++;
  191. }
  192. }
  193. }
  194. if (sameCount > 5)
  195. {
  196. lostPoint += (int)(3 + sameCount - 5);
  197. }
  198. }
  199. }
  200. // LEVEL2
  201. for (int row = 0; row < moduleCount - 1; row++)
  202. {
  203. for (int col = 0; col < moduleCount - 1; col++)
  204. {
  205. var count = 0;
  206. if (qrCode.IsDark(row, col)) count++;
  207. if (qrCode.IsDark(row + 1, col)) count++;
  208. if (qrCode.IsDark(row, col + 1)) count++;
  209. if (qrCode.IsDark(row + 1, col + 1)) count++;
  210. if (count == 0 || count == 4)
  211. {
  212. lostPoint += 3;
  213. }
  214. }
  215. }
  216. // LEVEL3
  217. for (int row = 0; row < moduleCount; row++)
  218. {
  219. for (int col = 0; col < moduleCount - 6; col++)
  220. {
  221. if (qrCode.IsDark(row, col)
  222. && !qrCode.IsDark(row, col + 1)
  223. && qrCode.IsDark(row, col + 2)
  224. && qrCode.IsDark(row, col + 3)
  225. && qrCode.IsDark(row, col + 4)
  226. && !qrCode.IsDark(row, col + 5)
  227. && qrCode.IsDark(row, col + 6))
  228. {
  229. lostPoint += 40;
  230. }
  231. }
  232. }
  233. for (int col = 0; col < moduleCount; col++)
  234. {
  235. for (int row = 0; row < moduleCount - 6; row++)
  236. {
  237. if (qrCode.IsDark(row, col)
  238. && !qrCode.IsDark(row + 1, col)
  239. && qrCode.IsDark(row + 2, col)
  240. && qrCode.IsDark(row + 3, col)
  241. && qrCode.IsDark(row + 4, col)
  242. && !qrCode.IsDark(row + 5, col)
  243. && qrCode.IsDark(row + 6, col))
  244. {
  245. lostPoint += 40;
  246. }
  247. }
  248. }
  249. // LEVEL4
  250. int darkCount = 0;
  251. for (int col = 0; col < moduleCount; col++)
  252. {
  253. for (int row = 0; row < moduleCount; row++)
  254. {
  255. if (qrCode.IsDark(row, col))
  256. {
  257. darkCount++;
  258. }
  259. }
  260. }
  261. double ratio = Math.Abs(100.0 * darkCount / moduleCount / moduleCount - 50) / 5;
  262. lostPoint += ratio * 10;
  263. return lostPoint;
  264. }
  265. internal static int GetBCHTypeInfo(int data)
  266. {
  267. int d = (data << 10);
  268. int s = 0;
  269. while ((s = (int)(QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G15))) >= 0)
  270. {
  271. d ^= (Convert.ToInt32(QRUtil.G15) << s);
  272. }
  273. return (int)((data << 10) | d) ^ QRUtil.G15_MASK;
  274. }
  275. internal static int GetBCHTypeNumber(int data)
  276. {
  277. int d = data << 12;
  278. while (QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G18) >= 0)
  279. {
  280. d ^= (QRUtil.G18 << (QRUtil.GetBCHDigit(d) - QRUtil.GetBCHDigit(QRUtil.G18)));
  281. }
  282. return (data << 12) | d;
  283. }
  284. internal static int GetBCHDigit(int dataInt)
  285. {
  286. int digit = 0;
  287. uint data = Convert.ToUInt32(dataInt);
  288. while (data != 0)
  289. {
  290. digit++;
  291. data >>= 1;
  292. }
  293. return digit;
  294. }
  295. internal static int[] GetPatternPosition(int typeNumber)
  296. {
  297. return QRUtil.PATTERN_POSITION_TABLE[typeNumber - 1];
  298. }
  299. internal static bool GetMask(QRMaskPattern maskPattern, int i, int j)
  300. {
  301. switch (maskPattern)
  302. {
  303. case QRMaskPattern.PATTERN000: return (i + j) % 2 == 0;
  304. case QRMaskPattern.PATTERN001: return i % 2 == 0;
  305. case QRMaskPattern.PATTERN010: return j % 3 == 0;
  306. case QRMaskPattern.PATTERN011: return (i + j) % 3 == 0;
  307. case QRMaskPattern.PATTERN100: return (Math.Floor((double) (i / 2)) + Math.Floor((double) (j / 3))) % 2 == 0;
  308. case QRMaskPattern.PATTERN101: return (i * j) % 2 + (i * j) % 3 == 0;
  309. case QRMaskPattern.PATTERN110: return ((i * j) % 2 + (i * j) % 3) % 2 == 0;
  310. case QRMaskPattern.PATTERN111: return ((i * j) % 3 + (i + j) % 2) % 2 == 0;
  311. default:
  312. throw new Error("bad maskPattern:" + maskPattern);
  313. }
  314. }
  315. internal static QRPolynomial GetErrorCorrectPolynomial(int errorCorrectLength)
  316. {
  317. QRPolynomial a = new QRPolynomial(new DataCache() { 1 }, 0);
  318. for (int i = 0; i < errorCorrectLength; i++)
  319. {
  320. a = a.Multiply(new QRPolynomial(new DataCache() { 1, QRMath.GExp(i) }, 0));
  321. }
  322. return a;
  323. }
  324. }
  325. internal struct QRPolynomial
  326. {
  327. private int[] m_num;
  328. public QRPolynomial(DataCache num, int shift)
  329. : this()
  330. {
  331. if (num == null)
  332. {
  333. throw new Error();
  334. }
  335. int offset = 0;
  336. while (offset < num.Count && num[offset] == 0)
  337. {
  338. offset++;
  339. }
  340. this.m_num = new int[num.Count - offset + shift];
  341. for (int i = 0; i < num.Count - offset; i++)
  342. {
  343. this.m_num[i] = num[(int)(i + offset)];
  344. }
  345. }
  346. public int Get(int index)
  347. {
  348. return this.m_num[(int)index];
  349. }
  350. public int GetLength()
  351. {
  352. return (int)this.m_num.Length;
  353. }
  354. public QRPolynomial Multiply(QRPolynomial e)
  355. {
  356. var num = new DataCache(this.GetLength() + e.GetLength() - 1);
  357. for (int i = 0; i < this.GetLength(); i++)
  358. {
  359. for (int j = 0; j < e.GetLength(); j++)
  360. {
  361. num[i + j] ^= QRMath.GExp(QRMath.GLog(this.Get(i)) + QRMath.GLog(e.Get(j)));
  362. }
  363. }
  364. return new QRPolynomial(num, 0);
  365. }
  366. public QRPolynomial Mod(QRPolynomial e)
  367. {
  368. if (Convert.ToInt64(this.GetLength()) - Convert.ToInt64(e.GetLength()) < 0)
  369. {
  370. return this;
  371. }
  372. int ratio = QRMath.GLog(this.Get(0)) - QRMath.GLog(e.Get(0));
  373. var num = new DataCache(this.GetLength());
  374. for (int i = 0; i < this.GetLength(); i++)
  375. {
  376. num[i] = this.Get(i);
  377. }
  378. for (int i = 0; i < e.GetLength(); i++)
  379. {
  380. num[i] ^= QRMath.GExp(QRMath.GLog(e.Get(i)) + ratio);
  381. }
  382. // recursive call
  383. return new QRPolynomial(num, 0).Mod(e);
  384. }
  385. }
  386. internal static class QRMath
  387. {
  388. private static readonly int[] EXP_TABLE;
  389. private static readonly int[] LOG_TABLE;
  390. static QRMath()
  391. {
  392. EXP_TABLE = new int[256];
  393. LOG_TABLE = new int[256];
  394. for (int i = 0; i < 8; i++)
  395. {
  396. QRMath.EXP_TABLE[i] = (int)(1 << (int)i);
  397. }
  398. for (int i = 8; i < 256; i++)
  399. {
  400. QRMath.EXP_TABLE[i] = QRMath.EXP_TABLE[i - 4]
  401. ^ QRMath.EXP_TABLE[i - 5]
  402. ^ QRMath.EXP_TABLE[i - 6]
  403. ^ QRMath.EXP_TABLE[i - 8];
  404. }
  405. for (int i = 0; i < 255; i++)
  406. {
  407. QRMath.LOG_TABLE[QRMath.EXP_TABLE[i]] = i;
  408. }
  409. }
  410. internal static int GLog(int n)
  411. {
  412. if (n < 1)
  413. {
  414. throw new Error("glog(" + n + ")");
  415. }
  416. return QRMath.LOG_TABLE[n];
  417. }
  418. internal static int GExp(int n)
  419. {
  420. while (n < 0)
  421. {
  422. n += 255;
  423. }
  424. while (n >= 256)
  425. {
  426. n -= 255;
  427. }
  428. return QRMath.EXP_TABLE[n];
  429. }
  430. }
  431. public struct QR8bitByte
  432. {
  433. public QRMode Mode { get; private set; }
  434. private string m_data { get; set; }
  435. public QR8bitByte(string data)
  436. : this()
  437. {
  438. m_data = data;
  439. Mode = QRMode.MODE_8BIT_BYTE;
  440. }
  441. public int Length
  442. {
  443. get
  444. {
  445. return m_data.Length;
  446. }
  447. }
  448. public void Write(QRBitBuffer buffer)
  449. {
  450. for (int i = 0; i < m_data.Length; ++i)
  451. {
  452. //item
  453. buffer.Put(m_data[i], 8);
  454. }
  455. ///buffer = Data;
  456. }
  457. }
  458. internal class DataCache : List<int>
  459. {
  460. public DataCache(int capacity)
  461. : base()
  462. {
  463. for (int i = 0; i < capacity; i++)
  464. {
  465. base.Add(0);
  466. }
  467. }
  468. public DataCache()
  469. : base()
  470. {
  471. }
  472. }
  473. internal struct QRRSBlock
  474. {
  475. private static readonly int[][] RS_BLOCK_TABLE = new int[][] {
  476. // L
  477. // M
  478. // Q
  479. // H
  480. // 1
  481. new int [] {1, 26, 19},
  482. new int [] {1, 26, 16},
  483. new int [] {1, 26, 13},
  484. new int [] {1, 26, 9},
  485. // 2
  486. new int [] {1, 44, 34},
  487. new int [] {1, 44, 28},
  488. new int [] {1, 44, 22},
  489. new int [] {1, 44, 16},
  490. // 3
  491. new int [] {1, 70, 55},
  492. new int [] {1, 70, 44},
  493. new int [] {2, 35, 17},
  494. new int [] {2, 35, 13},
  495. // 4
  496. new int [] {1, 100, 80},
  497. new int [] {2, 50, 32},
  498. new int [] {2, 50, 24},
  499. new int [] {4, 25, 9},
  500. // 5
  501. new int [] {1, 134, 108},
  502. new int [] {2, 67, 43},
  503. new int [] {2, 33, 15, 2, 34, 16},
  504. new int [] {2, 33, 11, 2, 34, 12},
  505. // 6
  506. new int [] {2, 86, 68},
  507. new int [] {4, 43, 27},
  508. new int [] {4, 43, 19},
  509. new int [] {4, 43, 15},
  510. // 7
  511. new int [] {2, 98, 78},
  512. new int [] {4, 49, 31},
  513. new int [] {2, 32, 14, 4, 33, 15},
  514. new int [] {4, 39, 13, 1, 40, 14},
  515. // 8
  516. new int [] {2, 121, 97},
  517. new int [] {2, 60, 38, 2, 61, 39},
  518. new int [] {4, 40, 18, 2, 41, 19},
  519. new int [] {4, 40, 14, 2, 41, 15},
  520. // 9
  521. new int [] {2, 146, 116},
  522. new int [] {3, 58, 36, 2, 59, 37},
  523. new int [] {4, 36, 16, 4, 37, 17},
  524. new int [] {4, 36, 12, 4, 37, 13},
  525. // 10
  526. new int [] {2, 86, 68, 2, 87, 69},
  527. new int [] {4, 69, 43, 1, 70, 44},
  528. new int [] {6, 43, 19, 2, 44, 20},
  529. new int [] {6, 43, 15, 2, 44, 16}
  530. };
  531. public int DataCount { get; private set; }
  532. public int TotalCount { get; set; }
  533. public QRRSBlock(int totalCount, int dataCount)
  534. : this()
  535. {
  536. TotalCount = totalCount;
  537. DataCount = dataCount;
  538. }
  539. public static List<QRRSBlock> GetRSBlocks(int typeNumber, QRErrorCorrectLevel errorCorrectLevel)
  540. {
  541. int[] rsBlock = GetRsBlockTable(typeNumber, errorCorrectLevel);
  542. if (rsBlock == null)
  543. {
  544. throw new Error("bad rs block @ typeNumber:" + typeNumber + "/errorCorrectLevel:" + errorCorrectLevel);
  545. }
  546. int length = (int)rsBlock.Length / 3;
  547. var list = new List<QRRSBlock>();
  548. for (int i = 0; i < length; i++)
  549. {
  550. int count = rsBlock[i * 3 + 0];
  551. int totalCount = rsBlock[i * 3 + 1];
  552. int dataCount = rsBlock[i * 3 + 2];
  553. for (int j = 0; j < count; j++)
  554. {
  555. list.Add(new QRRSBlock(totalCount, dataCount));
  556. }
  557. }
  558. return list;
  559. }
  560. private static int[] GetRsBlockTable(int typeNumber, QRErrorCorrectLevel errorCorrectLevel)
  561. {
  562. switch (errorCorrectLevel)
  563. {
  564. case QRErrorCorrectLevel.L:
  565. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 0];
  566. case QRErrorCorrectLevel.M:
  567. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 1];
  568. case QRErrorCorrectLevel.Q:
  569. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 2];
  570. case QRErrorCorrectLevel.H:
  571. return QRRSBlock.RS_BLOCK_TABLE[(typeNumber - 1) * 4 + 3];
  572. default:
  573. return null;
  574. }
  575. }
  576. }
  577. public class QRCode
  578. {
  579. private const int PAD0 = 0xEC;
  580. private const int PAD1 = 0x11;
  581. private List<QR8bitByte> m_dataList = new List<QR8bitByte>();
  582. private int m_typeNumber;
  583. private DataCache m_dataCache;
  584. private int m_moduleCount;
  585. private bool?[][] m_modules;
  586. private QRErrorCorrectLevel m_errorCorrectLevel;
  587. public QRCode(Options options)
  588. : this(options.TypeNumber, options.CorrectLevel)
  589. {
  590. AddData(options.Text);
  591. }
  592. public QRCode(int typeNumber, QRErrorCorrectLevel level)
  593. {
  594. m_typeNumber = typeNumber;
  595. m_errorCorrectLevel = level;
  596. m_dataCache = null;
  597. }
  598. public void AddData(string data)
  599. {
  600. m_dataCache = null;
  601. m_dataList.Add(new QR8bitByte(data));
  602. }
  603. public void Make()
  604. {
  605. MakeImpl(false, GetBestMaskPattern());
  606. }
  607. private QRMaskPattern GetBestMaskPattern()
  608. {
  609. double minLostPoint = 0;
  610. QRMaskPattern pattern = 0;
  611. for (int i = 0; i < 8; i++)
  612. {
  613. this.MakeImpl(true, (QRMaskPattern)i);
  614. double lostPoint = QRUtil.GetLostPoint(this);
  615. if (i == 0 || minLostPoint > lostPoint)
  616. {
  617. minLostPoint = lostPoint;
  618. pattern = (QRMaskPattern)i;
  619. }
  620. }
  621. return pattern;
  622. }
  623. private void MakeImpl(bool test, QRMaskPattern maskPattern)
  624. {
  625. m_moduleCount = this.m_typeNumber * 4 + 17;
  626. m_modules = new bool?[m_moduleCount][];
  627. for (int row = 0; row < m_moduleCount; row++)
  628. {
  629. m_modules[row] = new bool?[(m_moduleCount)];
  630. for (var col = 0; col < m_moduleCount; col++)
  631. {
  632. m_modules[row][col] = null; //(col + row) % 3;
  633. }
  634. }
  635. this.SetupPositionProbePattern(0, 0);
  636. this.SetupPositionProbePattern(m_moduleCount - 7, 0);
  637. this.SetupPositionProbePattern(0, m_moduleCount - 7);
  638. this.SetupPositionAdjustPattern();
  639. this.SetupTimingPattern();
  640. this.setupTypeInfo(test, maskPattern);
  641. if (m_typeNumber >= 7)
  642. {
  643. this.setupTypeNumber(test);
  644. }
  645. if (this.m_dataCache == null)
  646. {
  647. this.m_dataCache = CreateData(this.m_typeNumber, this.m_errorCorrectLevel, this.m_dataList);
  648. }
  649. MapData(this.m_dataCache, maskPattern);
  650. }
  651. public bool IsDark(int row, int col)
  652. {
  653. return m_modules[(int)row][(int)col].Value;
  654. }
  655. private void SetupTimingPattern()
  656. {
  657. for (var r = 8; r < this.m_moduleCount - 8; r++)
  658. {
  659. if (this.m_modules[r][6] != null)
  660. {
  661. continue;
  662. }
  663. this.m_modules[r][6] = (r % 2 == 0);
  664. }
  665. for (var c = 8; c < this.m_moduleCount - 8; c++)
  666. {
  667. if (this.m_modules[6][c] != null)
  668. {
  669. continue;
  670. }
  671. this.m_modules[6][c] = (c % 2 == 0);
  672. }
  673. }
  674. private void setupTypeNumber(bool test)
  675. {
  676. var bits = QRUtil.GetBCHTypeNumber(m_typeNumber);
  677. for (var i = 0; i < 18; i++)
  678. {
  679. var mod = (!test && ((bits >> i) & 1) == 1);
  680. this.m_modules[(int)Math.Floor((double) (i / 3))][i % 3 + this.m_moduleCount - 8 - 3] = mod;
  681. }
  682. for (var i = 0; i < 18; i++)
  683. {
  684. var mod = (!test && ((bits >> i) & 1) == 1);
  685. this.m_modules[i % 3 + this.m_moduleCount - 8 - 3][(int)Math.Floor((double) (i / 3))] = mod;
  686. }
  687. }
  688. private void SetupPositionAdjustPattern()
  689. {
  690. var pos = QRUtil.GetPatternPosition(m_typeNumber);
  691. for (var i = 0; i < pos.Length; i++)
  692. {
  693. for (var j = 0; j < pos.Length; j++)
  694. {
  695. var row = pos[i];
  696. var col = pos[j];
  697. if (this.m_modules[row][col] != null)
  698. {
  699. continue;
  700. }
  701. for (var r = -2; r <= 2; r++)
  702. {
  703. for (var c = -2; c <= 2; c++)
  704. {
  705. if (r == -2 || r == 2 || c == -2 || c == 2
  706. || (r == 0 && c == 0))
  707. {
  708. this.m_modules[row + r][col + c] = true;
  709. }
  710. else
  711. {
  712. this.m_modules[row + r][col + c] = false;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. }
  719. private void setupTypeInfo(bool test, QRMaskPattern maskPattern)
  720. {
  721. var data = ((int)this.m_errorCorrectLevel << 3) | (int)maskPattern;
  722. var bits = QRUtil.GetBCHTypeInfo(data);
  723. // vertical
  724. for (var i = 0; i < 15; i++)
  725. {
  726. var mod = (!test && ((bits >> i) & 1) == 1);
  727. if (i < 6)
  728. {
  729. this.m_modules[i][8] = mod;
  730. }
  731. else if (i < 8)
  732. {
  733. this.m_modules[i + 1][8] = mod;
  734. }
  735. else
  736. {
  737. this.m_modules[this.m_moduleCount - 15 + i][8] = mod;
  738. }
  739. }
  740. // horizontal
  741. for (var i = 0; i < 15; i++)
  742. {
  743. var mod = (!test && ((bits >> i) & 1) == 1);
  744. if (i < 8)
  745. {
  746. this.m_modules[8][this.m_moduleCount - i - 1] = mod;
  747. }
  748. else if (i < 9)
  749. {
  750. this.m_modules[8][15 - i - 1 + 1] = mod;
  751. }
  752. else
  753. {
  754. this.m_modules[8][15 - i - 1] = mod;
  755. }
  756. }
  757. // fixed module
  758. this.m_modules[this.m_moduleCount - 8][8] = (!test);
  759. }
  760. private void MapData(DataCache data, QRMaskPattern maskPattern)
  761. {
  762. int inc = -1;
  763. int row = (int)this.m_moduleCount - 1;
  764. int bitIndex = 7;
  765. int byteIndex = 0;
  766. for (var col = this.m_moduleCount - 1; col > 0; col -= 2)
  767. {
  768. if (col == 6) col--;
  769. while (true)
  770. {
  771. for (int c = 0; c < 2; c++)
  772. {
  773. if (this.m_modules[row][col - c] == null)
  774. {
  775. bool dark = false;
  776. if (byteIndex < data.Count)
  777. {
  778. dark = (((Convert.ToUInt32(data[byteIndex]) >> bitIndex) & 1) == 1);
  779. }
  780. bool mask = QRUtil.GetMask(maskPattern, (int)row, col - c);
  781. if (mask)
  782. {
  783. dark = !dark;
  784. }
  785. this.m_modules[row][col - c] = dark;
  786. bitIndex--;
  787. if (bitIndex == -1)
  788. {
  789. byteIndex++;
  790. bitIndex = 7;
  791. }
  792. }
  793. }
  794. row += inc;
  795. if (row < 0 || this.m_moduleCount <= row)
  796. {
  797. row -= inc;
  798. inc = -inc;
  799. break;
  800. }
  801. }
  802. }
  803. }
  804. private DataCache CreateData(int typeNumber, QRErrorCorrectLevel errorCorrectLevel, List<QR8bitByte> dataList)
  805. {
  806. List<QRRSBlock> rsBlocks = QRRSBlock.GetRSBlocks(typeNumber, errorCorrectLevel);
  807. var buffer = new QRBitBuffer();
  808. for (int i = 0; i < dataList.Count; i++)
  809. {
  810. QR8bitByte data = dataList[i];
  811. buffer.Put((int)data.Mode, 4);
  812. buffer.Put(data.Length, QRUtil.GetLengthInBits(data.Mode, typeNumber));
  813. data.Write(buffer);
  814. }
  815. // calc num max data.
  816. int totalDataCount = 0;
  817. for (var i = 0; i < rsBlocks.Count; i++)
  818. {
  819. totalDataCount += rsBlocks[i].DataCount;
  820. }
  821. if (buffer.GetLengthInBits() > totalDataCount * 8)
  822. {
  823. throw new Error("code length overflow. ("
  824. + buffer.GetLengthInBits()
  825. + ">"
  826. + totalDataCount * 8
  827. + ")");
  828. }
  829. // end code
  830. if (buffer.GetLengthInBits() + 4 <= totalDataCount * 8)
  831. {
  832. buffer.Put(0, 4);
  833. }
  834. // padding
  835. while (buffer.GetLengthInBits() % 8 != 0)
  836. {
  837. buffer.PutBit(false);
  838. }
  839. // padding
  840. while (true)
  841. {
  842. if (buffer.GetLengthInBits() >= totalDataCount * 8)
  843. {
  844. break;
  845. }
  846. buffer.Put(QRCode.PAD0, 8);
  847. if (buffer.GetLengthInBits() >= totalDataCount * 8)
  848. {
  849. break;
  850. }
  851. buffer.Put(QRCode.PAD1, 8);
  852. }
  853. return CreateBytes(buffer, rsBlocks);
  854. }
  855. private DataCache CreateBytes(QRBitBuffer buffer, List<QRRSBlock> rsBlocks)
  856. {
  857. int offset = 0;
  858. int maxDcCount = 0;
  859. int maxEcCount = 0;
  860. var dcdata = new DataCache[(rsBlocks.Count)];
  861. var ecdata = new DataCache[(rsBlocks.Count)];
  862. for (int r = 0; r < rsBlocks.Count; r++)
  863. {
  864. int dcCount = rsBlocks[(int)r].DataCount;
  865. int ecCount = rsBlocks[(int)r].TotalCount - dcCount;
  866. maxDcCount = Math.Max(maxDcCount, dcCount);
  867. maxEcCount = Math.Max(maxEcCount, ecCount);
  868. dcdata[r] = new DataCache(dcCount);
  869. for (int i = 0; i < dcdata[r].Count; i++)
  870. {
  871. dcdata[r][i] = 0xff & buffer.m_buffer[(int)(i + offset)];
  872. }
  873. offset += dcCount;
  874. QRPolynomial rsPoly = QRUtil.GetErrorCorrectPolynomial(ecCount);
  875. QRPolynomial rawPoly = new QRPolynomial(dcdata[r], rsPoly.GetLength() - 1);
  876. var modPoly = rawPoly.Mod(rsPoly);
  877. ecdata[r] = new DataCache(rsPoly.GetLength() - 1);
  878. for (int i = 0; i < ecdata[r].Count; i++)
  879. {
  880. int modIndex = i + modPoly.GetLength() - (int)ecdata[r].Count;
  881. ecdata[r][i] = (modIndex >= 0) ? modPoly.Get(modIndex) : 0;
  882. }
  883. }
  884. int totalCodeCount = 0;
  885. for (int i = 0; i < rsBlocks.Count; i++)
  886. {
  887. totalCodeCount += rsBlocks[(int)i].TotalCount;
  888. }
  889. var data = new DataCache(totalCodeCount);
  890. int index = 0;
  891. for (int i = 0; i < maxDcCount; i++)
  892. {
  893. for (int r = 0; r < rsBlocks.Count; r++)
  894. {
  895. if (i < dcdata[r].Count)
  896. {
  897. data[index++] = dcdata[r][i];
  898. }
  899. }
  900. }
  901. for (int i = 0; i < maxEcCount; i++)
  902. {
  903. for (int r = 0; r < rsBlocks.Count; r++)
  904. {
  905. if (i < ecdata[r].Count)
  906. {
  907. data[index++] = ecdata[r][i];
  908. }
  909. }
  910. }
  911. return data;
  912. }
  913. private void SetupPositionProbePattern(int row, int col)
  914. {
  915. for (int r = -1; r <= 7; r++)
  916. {
  917. if (row + r <= -1 || this.m_moduleCount <= row + r) continue;
  918. for (int c = -1; c <= 7; c++)
  919. {
  920. if (col + c <= -1 || this.m_moduleCount <= col + c) continue;
  921. if ((0 <= r && r <= 6 && (c == 0 || c == 6))
  922. || (0 <= c && c <= 6 && (r == 0 || r == 6))
  923. || (2 <= r && r <= 4 && 2 <= c && c <= 4))
  924. {
  925. this.m_modules[row + r][col + c] = true;
  926. }
  927. else
  928. {
  929. this.m_modules[row + r][col + c] = false;
  930. }
  931. }
  932. }
  933. }
  934. public int GetModuleCount()
  935. {
  936. return this.m_moduleCount;
  937. }
  938. internal int getBestMaskPattern()
  939. {
  940. double minLostPoint = 0;
  941. int pattern = 0;
  942. for (int i = 0; i < 8; i++)
  943. {
  944. this.MakeImpl(true, (QRMaskPattern)i);
  945. double lostPoint = QRUtil.GetLostPoint(this);
  946. if (i == 0 || minLostPoint > lostPoint)
  947. {
  948. minLostPoint = lostPoint;
  949. pattern = i;
  950. }
  951. }
  952. return pattern;
  953. }
  954. }
  955. public class QRBitBuffer
  956. {
  957. internal List<int> m_buffer = new List<int>();
  958. private int m_length = 0;
  959. public bool Get(int index)
  960. {
  961. int bufIndex = Convert.ToInt32(Math.Floor((double) (index / 8)));
  962. return ((Convert.ToUInt32(this.m_buffer[bufIndex]) >> (7 - index % 8)) & 1) == 1;
  963. }
  964. public void Put(int num, int length)
  965. {
  966. for (var i = 0; i < length; i++)
  967. {
  968. this.PutBit(((Convert.ToUInt32(num) >> (length - i - 1)) & 1) == 1);
  969. }
  970. }
  971. public int GetLengthInBits()
  972. {
  973. return m_length;
  974. }
  975. public void PutBit(bool bit)
  976. {
  977. int bufIndex = (int)Math.Floor((double) (this.m_length / 8));
  978. if (this.m_buffer.Count <= bufIndex)
  979. {
  980. this.m_buffer.Add(0);
  981. }
  982. if (bit)
  983. {
  984. this.m_buffer[bufIndex] |= (int)(Convert.ToUInt32(0x80) >> (this.m_length % 8));
  985. }
  986. this.m_length++;
  987. }
  988. }
  989. }