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

ZXQRCodeFormatInformation.m 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright 2012 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "ZXQRCodeErrorCorrectionLevel.h"
  17. #import "ZXQRCodeFormatInformation.h"
  18. const int ZX_FORMAT_INFO_MASK_QR = 0x5412;
  19. /**
  20. * See ISO 18004:2006, Annex C, Table C.1
  21. */
  22. const int ZX_FORMAT_INFO_DECODE_LOOKUP_LEN = 32;
  23. const int ZX_FORMAT_INFO_DECODE_LOOKUP[ZX_FORMAT_INFO_DECODE_LOOKUP_LEN][2] = {
  24. {0x5412, 0x00},
  25. {0x5125, 0x01},
  26. {0x5E7C, 0x02},
  27. {0x5B4B, 0x03},
  28. {0x45F9, 0x04},
  29. {0x40CE, 0x05},
  30. {0x4F97, 0x06},
  31. {0x4AA0, 0x07},
  32. {0x77C4, 0x08},
  33. {0x72F3, 0x09},
  34. {0x7DAA, 0x0A},
  35. {0x789D, 0x0B},
  36. {0x662F, 0x0C},
  37. {0x6318, 0x0D},
  38. {0x6C41, 0x0E},
  39. {0x6976, 0x0F},
  40. {0x1689, 0x10},
  41. {0x13BE, 0x11},
  42. {0x1CE7, 0x12},
  43. {0x19D0, 0x13},
  44. {0x0762, 0x14},
  45. {0x0255, 0x15},
  46. {0x0D0C, 0x16},
  47. {0x083B, 0x17},
  48. {0x355F, 0x18},
  49. {0x3068, 0x19},
  50. {0x3F31, 0x1A},
  51. {0x3A06, 0x1B},
  52. {0x24B4, 0x1C},
  53. {0x2183, 0x1D},
  54. {0x2EDA, 0x1E},
  55. {0x2BED, 0x1F},
  56. };
  57. /**
  58. * Offset i holds the number of 1 bits in the binary representation of i
  59. */
  60. const int ZX_BITS_SET_IN_HALF_BYTE[] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
  61. @implementation ZXQRCodeFormatInformation
  62. - (id)initWithFormatInfo:(int)formatInfo {
  63. if (self = [super init]) {
  64. _errorCorrectionLevel = [ZXQRCodeErrorCorrectionLevel forBits:(formatInfo >> 3) & 0x03];
  65. _dataMask = (int8_t)(formatInfo & 0x07);
  66. }
  67. return self;
  68. }
  69. + (int)numBitsDiffering:(int)a b:(int)b {
  70. a ^= b;
  71. return ZX_BITS_SET_IN_HALF_BYTE[a & 0x0F] +
  72. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 4 & 0x0F)] +
  73. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 8 & 0x0F)] +
  74. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 12 & 0x0F)] +
  75. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 16 & 0x0F)] +
  76. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 20 & 0x0F)] +
  77. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 24 & 0x0F)] +
  78. ZX_BITS_SET_IN_HALF_BYTE[((int)((unsigned int)a) >> 28 & 0x0F)];
  79. }
  80. + (ZXQRCodeFormatInformation *)decodeFormatInformation:(int)maskedFormatInfo1 maskedFormatInfo2:(int)maskedFormatInfo2 {
  81. ZXQRCodeFormatInformation *formatInfo = [self doDecodeFormatInformation:maskedFormatInfo1 maskedFormatInfo2:maskedFormatInfo2];
  82. if (formatInfo != nil) {
  83. return formatInfo;
  84. }
  85. return [self doDecodeFormatInformation:maskedFormatInfo1 ^ ZX_FORMAT_INFO_MASK_QR maskedFormatInfo2:maskedFormatInfo2 ^ ZX_FORMAT_INFO_MASK_QR];
  86. }
  87. + (ZXQRCodeFormatInformation *)doDecodeFormatInformation:(int)maskedFormatInfo1 maskedFormatInfo2:(int)maskedFormatInfo2 {
  88. int bestDifference = INT_MAX;
  89. int bestFormatInfo = 0;
  90. for (int i = 0; i < ZX_FORMAT_INFO_DECODE_LOOKUP_LEN; i++) {
  91. int targetInfo = ZX_FORMAT_INFO_DECODE_LOOKUP[i][0];
  92. if (targetInfo == maskedFormatInfo1 || targetInfo == maskedFormatInfo2) {
  93. return [[ZXQRCodeFormatInformation alloc] initWithFormatInfo:ZX_FORMAT_INFO_DECODE_LOOKUP[i][1]];
  94. }
  95. int bitsDifference = [self numBitsDiffering:maskedFormatInfo1 b:targetInfo];
  96. if (bitsDifference < bestDifference) {
  97. bestFormatInfo = ZX_FORMAT_INFO_DECODE_LOOKUP[i][1];
  98. bestDifference = bitsDifference;
  99. }
  100. if (maskedFormatInfo1 != maskedFormatInfo2) {
  101. bitsDifference = [self numBitsDiffering:maskedFormatInfo2 b:targetInfo];
  102. if (bitsDifference < bestDifference) {
  103. bestFormatInfo = ZX_FORMAT_INFO_DECODE_LOOKUP[i][1];
  104. bestDifference = bitsDifference;
  105. }
  106. }
  107. }
  108. if (bestDifference <= 3) {
  109. return [[ZXQRCodeFormatInformation alloc] initWithFormatInfo:bestFormatInfo];
  110. }
  111. return nil;
  112. }
  113. - (NSUInteger)hash {
  114. return (self.errorCorrectionLevel.ordinal << 3) | (int)self.dataMask;
  115. }
  116. - (BOOL)isEqual:(id)o {
  117. if (![o isKindOfClass:[ZXQRCodeFormatInformation class]]) {
  118. return NO;
  119. }
  120. ZXQRCodeFormatInformation *other = (ZXQRCodeFormatInformation *)o;
  121. return self.errorCorrectionLevel == other.errorCorrectionLevel && self.dataMask == other.dataMask;
  122. }
  123. @end