formatinf.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Ported to JavaScript by Lazar Laszlo 2011
  3. lazarsoft@gmail.com, www.lazarsoft.info
  4. */
  5. /*
  6. *
  7. * Copyright 2007 ZXing authors
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. import {URShift} from './qrcode';
  22. import ErrorCorrectionLevel from './errorlevel';
  23. var FORMAT_INFO_MASK_QR = 0x5412;
  24. var FORMAT_INFO_DECODE_LOOKUP = [
  25. [0x5412, 0x00],
  26. [0x5125, 0x01],
  27. [0x5E7C, 0x02],
  28. [0x5B4B, 0x03],
  29. [0x45F9, 0x04],
  30. [0x40CE, 0x05],
  31. [0x4F97, 0x06],
  32. [0x4AA0, 0x07],
  33. [0x77C4, 0x08],
  34. [0x72F3, 0x09],
  35. [0x7DAA, 0x0A],
  36. [0x789D, 0x0B],
  37. [0x662F, 0x0C],
  38. [0x6318, 0x0D],
  39. [0x6C41, 0x0E],
  40. [0x6976, 0x0F],
  41. [0x1689, 0x10],
  42. [0x13BE, 0x11],
  43. [0x1CE7, 0x12],
  44. [0x19D0, 0x13],
  45. [0x0762, 0x14],
  46. [0x0255, 0x15],
  47. [0x0D0C, 0x16],
  48. [0x083B, 0x17],
  49. [0x355F, 0x18],
  50. [0x3068, 0x19],
  51. [0x3F31, 0x1A],
  52. [0x3A06, 0x1B],
  53. [0x24B4, 0x1C],
  54. [0x2183, 0x1D],
  55. [0x2EDA, 0x1E],
  56. [0x2BED, 0x1F],
  57. ];
  58. var BITS_SET_IN_HALF_BYTE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];
  59. export default function FormatInformation(formatInfo) {
  60. this.errorCorrectionLevel = ErrorCorrectionLevel.forBits((formatInfo >> 3) & 0x03);
  61. this.dataMask = (formatInfo & 0x07);
  62. }
  63. FormatInformation.prototype.GetHashCode = function() {
  64. return (this.errorCorrectionLevel.ordinal() << 3) | this.dataMask;
  65. };
  66. FormatInformation.prototype.Equals = function(o) {
  67. var other = o;
  68. return this.errorCorrectionLevel == other.errorCorrectionLevel && this.dataMask == other.dataMask;
  69. };
  70. FormatInformation.numBitsDiffering = function(a, b) {
  71. a ^= b; // a now has a 1 bit exactly where its bit differs with b's
  72. // Count bits set quickly with a series of lookups:
  73. return BITS_SET_IN_HALF_BYTE[a & 0x0F] + BITS_SET_IN_HALF_BYTE[(URShift(a, 4) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 8) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 12) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 16) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 20) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 24) & 0x0F)] + BITS_SET_IN_HALF_BYTE[(URShift(a, 28) & 0x0F)];
  74. };
  75. FormatInformation.decodeFormatInformation = function(maskedFormatInfo) {
  76. var formatInfo = FormatInformation.doDecodeFormatInformation(maskedFormatInfo);
  77. if (formatInfo != null) {
  78. return formatInfo;
  79. }
  80. // Should return null, but, some QR codes apparently
  81. // do not mask this info. Try again by actually masking the pattern
  82. // first
  83. return FormatInformation.doDecodeFormatInformation(maskedFormatInfo ^ FORMAT_INFO_MASK_QR);
  84. };
  85. FormatInformation.doDecodeFormatInformation = function(maskedFormatInfo) {
  86. // Find the int in FORMAT_INFO_DECODE_LOOKUP with fewest bits differing
  87. var bestDifference = 0xffffffff;
  88. var bestFormatInfo = 0;
  89. for (var i = 0; i < FORMAT_INFO_DECODE_LOOKUP.length; i++) {
  90. var decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
  91. var targetInfo = decodeInfo[0];
  92. if (targetInfo == maskedFormatInfo) {
  93. // Found an exact match
  94. return new FormatInformation(decodeInfo[1]);
  95. }
  96. var bitsDifference = this.numBitsDiffering(maskedFormatInfo, targetInfo);
  97. if (bitsDifference < bestDifference) {
  98. bestFormatInfo = decodeInfo[1];
  99. bestDifference = bitsDifference;
  100. }
  101. }
  102. // Hamming distance of the 32 masked codes is 7, by construction, so <= 3 bits
  103. // differing means we found a match
  104. if (bestDifference <= 3) {
  105. return new FormatInformation(bestFormatInfo);
  106. }
  107. return null;
  108. };