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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "ZXQRCodeMode.h"
  17. #import "ZXQRCodeVersion.h"
  18. @interface ZXQRCodeMode ()
  19. @property (nonatomic, strong, readonly) NSArray *characterCountBitsForVersions;
  20. @end
  21. @implementation ZXQRCodeMode
  22. - (id)initWithCharacterCountBitsForVersions:(NSArray *)characterCountBitsForVersions bits:(int)bits name:(NSString *)name {
  23. if (self = [super init]) {
  24. _characterCountBitsForVersions = characterCountBitsForVersions;
  25. _bits = bits;
  26. _name = name;
  27. }
  28. return self;
  29. }
  30. + (ZXQRCodeMode *)forBits:(int)bits {
  31. switch (bits) {
  32. case 0x0:
  33. return [ZXQRCodeMode terminatorMode];
  34. case 0x1:
  35. return [ZXQRCodeMode numericMode];
  36. case 0x2:
  37. return [ZXQRCodeMode alphanumericMode];
  38. case 0x3:
  39. return [ZXQRCodeMode structuredAppendMode];
  40. case 0x4:
  41. return [ZXQRCodeMode byteMode];
  42. case 0x5:
  43. return [ZXQRCodeMode fnc1FirstPositionMode];
  44. case 0x7:
  45. return [ZXQRCodeMode eciMode];
  46. case 0x8:
  47. return [ZXQRCodeMode kanjiMode];
  48. case 0x9:
  49. return [ZXQRCodeMode fnc1SecondPositionMode];
  50. case 0xD:
  51. return [ZXQRCodeMode hanziMode];
  52. default:
  53. return nil;
  54. }
  55. }
  56. - (int)characterCountBits:(ZXQRCodeVersion *)version {
  57. int number = version.versionNumber;
  58. int offset;
  59. if (number <= 9) {
  60. offset = 0;
  61. } else if (number <= 26) {
  62. offset = 1;
  63. } else {
  64. offset = 2;
  65. }
  66. return [self.characterCountBitsForVersions[offset] intValue];
  67. }
  68. - (NSString *)description {
  69. return self.name;
  70. }
  71. + (ZXQRCodeMode *)terminatorMode {
  72. static ZXQRCodeMode *thisMode = nil;
  73. static dispatch_once_t onceToken;
  74. dispatch_once(&onceToken, ^{
  75. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@0, @0, @0] bits:0x00 name:@"TERMINATOR"];
  76. });
  77. return thisMode;
  78. }
  79. + (ZXQRCodeMode *)numericMode {
  80. static ZXQRCodeMode *thisMode = nil;
  81. static dispatch_once_t onceToken;
  82. dispatch_once(&onceToken, ^{
  83. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@10, @12, @14] bits:0x01 name:@"NUMERIC"];
  84. });
  85. return thisMode;
  86. }
  87. + (ZXQRCodeMode *)alphanumericMode {
  88. static ZXQRCodeMode *thisMode = nil;
  89. static dispatch_once_t onceToken;
  90. dispatch_once(&onceToken, ^{
  91. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@9, @11, @13] bits:0x02 name:@"ALPHANUMERIC"];
  92. });
  93. return thisMode;
  94. }
  95. + (ZXQRCodeMode *)structuredAppendMode {
  96. static ZXQRCodeMode *thisMode = nil;
  97. static dispatch_once_t onceToken;
  98. dispatch_once(&onceToken, ^{
  99. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@0, @0, @0] bits:0x03 name:@"STRUCTURED_APPEND"];
  100. });
  101. return thisMode;
  102. }
  103. + (ZXQRCodeMode *)byteMode {
  104. static ZXQRCodeMode *thisMode = nil;
  105. static dispatch_once_t onceToken;
  106. dispatch_once(&onceToken, ^{
  107. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@8, @16, @16] bits:0x04 name:@"BYTE"];
  108. });
  109. return thisMode;
  110. }
  111. + (ZXQRCodeMode *)eciMode {
  112. static ZXQRCodeMode *thisMode = nil;
  113. static dispatch_once_t onceToken;
  114. dispatch_once(&onceToken, ^{
  115. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@0, @0, @0] bits:0x07 name:@"ECI"];
  116. });
  117. return thisMode;
  118. }
  119. + (ZXQRCodeMode *)kanjiMode {
  120. static ZXQRCodeMode *thisMode = nil;
  121. static dispatch_once_t onceToken;
  122. dispatch_once(&onceToken, ^{
  123. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@8, @10, @12] bits:0x08 name:@"KANJI"];
  124. });
  125. return thisMode;
  126. }
  127. + (ZXQRCodeMode *)fnc1FirstPositionMode {
  128. static ZXQRCodeMode *thisMode = nil;
  129. static dispatch_once_t onceToken;
  130. dispatch_once(&onceToken, ^{
  131. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@0, @0, @0] bits:0x05 name:@"FNC1_FIRST_POSITION"];
  132. });
  133. return thisMode;
  134. }
  135. + (ZXQRCodeMode *)fnc1SecondPositionMode {
  136. static ZXQRCodeMode *thisMode = nil;
  137. static dispatch_once_t onceToken;
  138. dispatch_once(&onceToken, ^{
  139. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@0, @0, @0] bits:0x09 name:@"FNC1_SECOND_POSITION"];
  140. });
  141. return thisMode;
  142. }
  143. + (ZXQRCodeMode *)hanziMode {
  144. static ZXQRCodeMode *thisMode = nil;
  145. static dispatch_once_t onceToken;
  146. dispatch_once(&onceToken, ^{
  147. thisMode = [[ZXQRCodeMode alloc] initWithCharacterCountBitsForVersions:@[@8, @10, @12] bits:0x0D name:@"HANZI"];
  148. });
  149. return thisMode;
  150. }
  151. @end