Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXMultiFormatUPCEANReader.m 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "ZXDecodeHints.h"
  17. #import "ZXEAN8Reader.h"
  18. #import "ZXEAN13Reader.h"
  19. #import "ZXErrors.h"
  20. #import "ZXMultiFormatUPCEANReader.h"
  21. #import "ZXReader.h"
  22. #import "ZXResult.h"
  23. #import "ZXUPCAReader.h"
  24. #import "ZXUPCEReader.h"
  25. @interface ZXMultiFormatUPCEANReader ()
  26. @property (nonatomic, strong, readonly) NSMutableArray *readers;
  27. @end
  28. @implementation ZXMultiFormatUPCEANReader
  29. - (id)initWithHints:(ZXDecodeHints *)hints {
  30. if (self = [super init]) {
  31. _readers = [NSMutableArray array];
  32. if (hints != nil) {
  33. if ([hints containsFormat:kBarcodeFormatEan13]) {
  34. [_readers addObject:[[ZXEAN13Reader alloc] init]];
  35. } else if ([hints containsFormat:kBarcodeFormatUPCA]) {
  36. [_readers addObject:[[ZXUPCAReader alloc] init]];
  37. }
  38. if ([hints containsFormat:kBarcodeFormatEan8]) {
  39. [_readers addObject:[[ZXEAN8Reader alloc] init]];
  40. }
  41. if ([hints containsFormat:kBarcodeFormatUPCE]) {
  42. [_readers addObject:[[ZXUPCEReader alloc] init]];
  43. }
  44. }
  45. if ([_readers count] == 0) {
  46. [_readers addObject:[[ZXEAN13Reader alloc] init]];
  47. [_readers addObject:[[ZXEAN8Reader alloc] init]];
  48. [_readers addObject:[[ZXUPCEReader alloc] init]];
  49. }
  50. }
  51. return self;
  52. }
  53. - (ZXResult *)decodeRow:(int)rowNumber row:(ZXBitArray *)row hints:(ZXDecodeHints *)hints error:(NSError **)error {
  54. NSRange startGuardPattern = [ZXUPCEANReader findStartGuardPattern:row error:error];
  55. if (startGuardPattern.location == NSNotFound) {
  56. return nil;
  57. }
  58. for (ZXUPCEANReader *reader in self.readers) {
  59. ZXResult *result = [reader decodeRow:rowNumber row:row startGuardRange:startGuardPattern hints:hints error:error];
  60. if (!result) {
  61. continue;
  62. }
  63. // Special case: a 12-digit code encoded in UPC-A is identical to a "0"
  64. // followed by those 12 digits encoded as EAN-13. Each will recognize such a code,
  65. // UPC-A as a 12-digit string and EAN-13 as a 13-digit string starting with "0".
  66. // Individually these are correct and their readers will both read such a code
  67. // and correctly call it EAN-13, or UPC-A, respectively.
  68. //
  69. // In this case, if we've been looking for both types, we'd like to call it
  70. // a UPC-A code. But for efficiency we only run the EAN-13 decoder to also read
  71. // UPC-A. So we special case it here, and convert an EAN-13 result to a UPC-A
  72. // result if appropriate.
  73. //
  74. // But, don't return UPC-A if UPC-A was not a requested format!
  75. BOOL ean13MayBeUPCA = kBarcodeFormatEan13 == result.barcodeFormat && [result.text characterAtIndex:0] == '0';
  76. BOOL canReturnUPCA = hints == nil || [hints numberOfPossibleFormats] == 0 || [hints containsFormat:kBarcodeFormatUPCA];
  77. if (ean13MayBeUPCA && canReturnUPCA) {
  78. // Transfer the metdata across
  79. ZXResult *resultUPCA = [ZXResult resultWithText:[result.text substringFromIndex:1]
  80. rawBytes:result.rawBytes
  81. resultPoints:result.resultPoints
  82. format:kBarcodeFormatUPCA];
  83. [resultUPCA putAllMetadata:result.resultMetadata];
  84. return resultUPCA;
  85. }
  86. return result;
  87. }
  88. if (error) *error = ZXNotFoundErrorInstance();
  89. return nil;
  90. }
  91. - (void)reset {
  92. for (id<ZXReader> reader in self.readers) {
  93. [reader reset];
  94. }
  95. }
  96. @end