Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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 "ZXBarcodeFormat.h"
  17. #import "ZXBitArray.h"
  18. #import "ZXErrors.h"
  19. #import "ZXIntArray.h"
  20. #import "ZXResult.h"
  21. #import "ZXResultMetadataType.h"
  22. #import "ZXResultPoint.h"
  23. #import "ZXUPCEANExtension5Support.h"
  24. #import "ZXUPCEANReader.h"
  25. const int ZX_UPCEAN_CHECK_DIGIT_ENCODINGS[] = {
  26. 0x18, 0x14, 0x12, 0x11, 0x0C, 0x06, 0x03, 0x0A, 0x09, 0x05
  27. };
  28. @interface ZXUPCEANExtension5Support ()
  29. @property (nonatomic, strong, readonly) ZXIntArray *decodeMiddleCounters;
  30. @end
  31. @implementation ZXUPCEANExtension5Support
  32. - (id)init {
  33. if (self = [super init]) {
  34. _decodeMiddleCounters = [[ZXIntArray alloc] initWithLength:4];
  35. }
  36. return self;
  37. }
  38. - (ZXResult *)decodeRow:(int)rowNumber row:(ZXBitArray *)row extensionStartRange:(NSRange)extensionStartRange error:(NSError **)error {
  39. NSMutableString *resultString = [NSMutableString string];
  40. int end = [self decodeMiddle:row startRange:extensionStartRange result:resultString error:error];
  41. if (end == -1) {
  42. return nil;
  43. }
  44. NSMutableDictionary *extensionData = [self parseExtensionString:resultString];
  45. ZXResult *extensionResult = [[ZXResult alloc] initWithText:resultString
  46. rawBytes:nil
  47. resultPoints:@[[[ZXResultPoint alloc] initWithX:(extensionStartRange.location + NSMaxRange(extensionStartRange)) / 2.0f y:rowNumber],
  48. [[ZXResultPoint alloc] initWithX:end y:rowNumber]]
  49. format:kBarcodeFormatUPCEANExtension];
  50. if (extensionData != nil) {
  51. [extensionResult putAllMetadata:extensionData];
  52. }
  53. return extensionResult;
  54. }
  55. - (int)decodeMiddle:(ZXBitArray *)row startRange:(NSRange)startRange result:(NSMutableString *)result error:(NSError **)error {
  56. ZXIntArray *counters = self.decodeMiddleCounters;
  57. [counters clear];
  58. int end = [row size];
  59. int rowOffset = (int)NSMaxRange(startRange);
  60. int lgPatternFound = 0;
  61. for (int x = 0; x < 5 && rowOffset < end; x++) {
  62. int bestMatch = [ZXUPCEANReader decodeDigit:row counters:counters rowOffset:rowOffset patternType:ZX_UPC_EAN_PATTERNS_L_AND_G_PATTERNS error:error];
  63. if (bestMatch == -1) {
  64. return -1;
  65. }
  66. [result appendFormat:@"%C", (unichar)('0' + bestMatch % 10)];
  67. rowOffset += [counters sum];
  68. if (bestMatch >= 10) {
  69. lgPatternFound |= 1 << (4 - x);
  70. }
  71. if (x != 4) {
  72. // Read off separator if not last
  73. rowOffset = [row nextSet:rowOffset];
  74. rowOffset = [row nextUnset:rowOffset];
  75. }
  76. }
  77. if (result.length != 5) {
  78. if (error) *error = ZXNotFoundErrorInstance();
  79. return -1;
  80. }
  81. int checkDigit = [self determineCheckDigit:lgPatternFound];
  82. if (checkDigit == -1) {
  83. if (error) *error = ZXNotFoundErrorInstance();
  84. return -1;
  85. } else if ([self extensionChecksum:result] != checkDigit) {
  86. if (error) *error = ZXNotFoundErrorInstance();
  87. return -1;
  88. }
  89. return rowOffset;
  90. }
  91. - (int)extensionChecksum:(NSString *)s {
  92. int length = (int)[s length];
  93. int sum = 0;
  94. for (int i = length - 2; i >= 0; i -= 2) {
  95. sum += (int)[s characterAtIndex:i] - (int)'0';
  96. }
  97. sum *= 3;
  98. for (int i = length - 1; i >= 0; i -= 2) {
  99. sum += (int)[s characterAtIndex:i] - (int)'0';
  100. }
  101. sum *= 3;
  102. return sum % 10;
  103. }
  104. - (int)determineCheckDigit:(int)lgPatternFound {
  105. for (int d = 0; d < 10; d++) {
  106. if (lgPatternFound == ZX_UPCEAN_CHECK_DIGIT_ENCODINGS[d]) {
  107. return d;
  108. }
  109. }
  110. return -1;
  111. }
  112. /**
  113. * @param raw raw content of extension
  114. * @return formatted interpretation of raw content as a NSDictionary mapping
  115. * one ZXResultMetadataType to appropriate value, or nil if not known
  116. */
  117. - (NSMutableDictionary *)parseExtensionString:(NSString *)raw {
  118. if (raw.length != 5) {
  119. return nil;
  120. }
  121. id value = [self parseExtension5String:raw];
  122. if (value) {
  123. return [NSMutableDictionary dictionaryWithObject:value forKey:@(kResultMetadataTypeSuggestedPrice)];
  124. } else {
  125. return nil;
  126. }
  127. }
  128. - (NSString *)parseExtension5String:(NSString *)raw {
  129. NSString *currency;
  130. switch ([raw characterAtIndex:0]) {
  131. case '0':
  132. currency = @"£";
  133. break;
  134. case '5':
  135. currency = @"$";
  136. break;
  137. case '9':
  138. if ([@"90000" isEqualToString:raw]) {
  139. return nil;
  140. }
  141. if ([@"99991" isEqualToString:raw]) {
  142. return @"0.00";
  143. }
  144. if ([@"99990" isEqualToString:raw]) {
  145. return @"Used";
  146. }
  147. currency = @"";
  148. break;
  149. default:
  150. currency = @"";
  151. break;
  152. }
  153. int rawAmount = [[raw substringFromIndex:1] intValue];
  154. NSString *unitsString = [@(rawAmount / 100) stringValue];
  155. int hundredths = rawAmount % 100;
  156. NSString *hundredthsString = hundredths < 10 ?
  157. [NSString stringWithFormat:@"0%d", hundredths] : [@(hundredths) stringValue];
  158. return [NSString stringWithFormat:@"%@%@.%@", currency, unitsString, hundredthsString];
  159. }
  160. @end