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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "ZXBitArray.h"
  17. #import "ZXErrors.h"
  18. #import "ZXIntArray.h"
  19. #import "ZXUPCEReader.h"
  20. // For an UPC-E barcode, the final digit is represented by the parities used
  21. // to encode the middle six digits, according to the table below.
  22. //
  23. // Parity of next 6 digits
  24. // Digit 0 1 2 3 4 5
  25. // 0 Even Even Even Odd Odd Odd
  26. // 1 Even Even Odd Even Odd Odd
  27. // 2 Even Even Odd Odd Even Odd
  28. // 3 Even Even Odd Odd Odd Even
  29. // 4 Even Odd Even Even Odd Odd
  30. // 5 Even Odd Odd Even Even Odd
  31. // 6 Even Odd Odd Odd Even Even
  32. // 7 Even Odd Even Odd Even Odd
  33. // 8 Even Odd Even Odd Odd Even
  34. // 9 Even Odd Odd Even Odd Even
  35. //
  36. // The encoding is represented by the following array, which is a bit pattern
  37. // using Odd = 0 and Even = 1. For example, 5 is represented by:
  38. //
  39. // Odd Even Even Odd Odd Even
  40. // in binary:
  41. // 0 1 1 0 0 1 == 0x19
  42. //
  43. const int CHECK_DIGIT_ENCODINGS[] = {
  44. 0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25
  45. };
  46. const int ZX_UPCE_MIDDLE_END_PATTERN_LEN = 6;
  47. /**
  48. * The pattern that marks the middle, and end, of a UPC-E pattern.
  49. * There is no "second half" to a UPC-E barcode.
  50. */
  51. const int ZX_UPCE_MIDDLE_END_PATTERN[] = {1, 1, 1, 1, 1, 1};
  52. /**
  53. * See ZX_UCPE_L_AND_G_PATTERNS; these values similarly represent patterns of
  54. * even-odd parity encodings of digits that imply both the number system (0 or 1)
  55. * used, and the check digit.
  56. */
  57. const int ZX_UCPE_NUMSYS_AND_CHECK_DIGIT_PATTERNS[][10] = {
  58. {0x38, 0x34, 0x32, 0x31, 0x2C, 0x26, 0x23, 0x2A, 0x29, 0x25},
  59. {0x07, 0x0B, 0x0D, 0x0E, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}
  60. };
  61. @interface ZXUPCEReader ()
  62. @property (nonatomic, strong, readonly) ZXIntArray *decodeMiddleCounters;
  63. @end
  64. @implementation ZXUPCEReader
  65. - (id)init {
  66. if (self = [super init]) {
  67. _decodeMiddleCounters = [[ZXIntArray alloc] initWithLength:4];
  68. }
  69. return self;
  70. }
  71. - (int)decodeMiddle:(ZXBitArray *)row startRange:(NSRange)startRange result:(NSMutableString *)result error:(NSError **)error {
  72. ZXIntArray *counters = self.decodeMiddleCounters;
  73. [counters clear];
  74. int end = [row size];
  75. int rowOffset = (int)NSMaxRange(startRange);
  76. int lgPatternFound = 0;
  77. for (int x = 0; x < 6 && rowOffset < end; x++) {
  78. int bestMatch = [ZXUPCEANReader decodeDigit:row counters:counters rowOffset:rowOffset patternType:ZX_UPC_EAN_PATTERNS_L_AND_G_PATTERNS error:error];
  79. if (bestMatch == -1) {
  80. return -1;
  81. }
  82. [result appendFormat:@"%C", (unichar)('0' + bestMatch % 10)];
  83. rowOffset += [counters sum];
  84. if (bestMatch >= 10) {
  85. lgPatternFound |= 1 << (5 - x);
  86. }
  87. }
  88. if (![self determineNumSysAndCheckDigit:result lgPatternFound:lgPatternFound]) {
  89. if (error) *error = ZXNotFoundErrorInstance();
  90. return -1;
  91. }
  92. return rowOffset;
  93. }
  94. - (NSRange)decodeEnd:(ZXBitArray *)row endStart:(int)endStart error:(NSError **)error {
  95. return [ZXUPCEANReader findGuardPattern:row
  96. rowOffset:endStart
  97. whiteFirst:YES
  98. pattern:ZX_UPCE_MIDDLE_END_PATTERN
  99. patternLen:sizeof(ZX_UPCE_MIDDLE_END_PATTERN) / sizeof(int)
  100. error:error];
  101. }
  102. - (BOOL)checkChecksum:(NSString *)s error:(NSError **)error {
  103. return [super checkChecksum:[ZXUPCEReader convertUPCEtoUPCA:s] error:error];
  104. }
  105. - (BOOL)determineNumSysAndCheckDigit:(NSMutableString *)resultString lgPatternFound:(int)lgPatternFound {
  106. for (int numSys = 0; numSys <= 1; numSys++) {
  107. for (int d = 0; d < 10; d++) {
  108. if (lgPatternFound == ZX_UCPE_NUMSYS_AND_CHECK_DIGIT_PATTERNS[numSys][d]) {
  109. [resultString insertString:[NSString stringWithFormat:@"%C", (unichar)('0' + numSys)] atIndex:0];
  110. [resultString appendFormat:@"%C", (unichar)('0' + d)];
  111. return YES;
  112. }
  113. }
  114. }
  115. return NO;
  116. }
  117. - (ZXBarcodeFormat)barcodeFormat {
  118. return kBarcodeFormatUPCE;
  119. }
  120. /**
  121. * Expands a UPC-E value back into its full, equivalent UPC-A code value.
  122. *
  123. * @param upce UPC-E code as string of digits
  124. * @return equivalent UPC-A code as string of digits
  125. */
  126. + (NSString *)convertUPCEtoUPCA:(NSString *)upce {
  127. NSString *upceChars = [upce substringWithRange:NSMakeRange(1, 6)];
  128. NSMutableString *result = [NSMutableString stringWithCapacity:12];
  129. [result appendFormat:@"%C", [upce characterAtIndex:0]];
  130. unichar lastChar = [upceChars characterAtIndex:5];
  131. switch (lastChar) {
  132. case '0':
  133. case '1':
  134. case '2':
  135. [result appendString:[upceChars substringToIndex:2]];
  136. [result appendFormat:@"%C", lastChar];
  137. [result appendString:@"0000"];
  138. [result appendString:[upceChars substringWithRange:NSMakeRange(2, 3)]];
  139. break;
  140. case '3':
  141. [result appendString:[upceChars substringToIndex:3]];
  142. [result appendString:@"00000"];
  143. [result appendString:[upceChars substringWithRange:NSMakeRange(3, 2)]];
  144. break;
  145. case '4':
  146. [result appendString:[upceChars substringToIndex:4]];
  147. [result appendString:@"00000"];
  148. [result appendString:[upceChars substringWithRange:NSMakeRange(4, 1)]];
  149. break;
  150. default:
  151. [result appendString:[upceChars substringToIndex:5]];
  152. [result appendString:@"0000"];
  153. [result appendFormat:@"%C", lastChar];
  154. break;
  155. }
  156. [result appendFormat:@"%C", [upce characterAtIndex:7]];
  157. return result;
  158. }
  159. @end