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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "ZXBinaryBitmap.h"
  17. #import "ZXDecodeHints.h"
  18. #import "ZXErrors.h"
  19. #import "ZXMultiFormatReader.h"
  20. #import "ZXResult.h"
  21. #if defined(ZXINGOBJC_AZTEC) || !defined(ZXINGOBJC_USE_SUBSPECS)
  22. #import "ZXAztecReader.h"
  23. #endif
  24. #if defined(ZXINGOBJC_DATAMATRIX) || !defined(ZXINGOBJC_USE_SUBSPECS)
  25. #import "ZXDataMatrixReader.h"
  26. #endif
  27. #if defined(ZXINGOBJC_MAXICODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  28. #import "ZXMaxiCodeReader.h"
  29. #endif
  30. #if defined(ZXINGOBJC_ONED) || !defined(ZXINGOBJC_USE_SUBSPECS)
  31. #import "ZXMultiFormatOneDReader.h"
  32. #endif
  33. #if defined(ZXINGOBJC_PDF417) || !defined(ZXINGOBJC_USE_SUBSPECS)
  34. #import "ZXPDF417Reader.h"
  35. #endif
  36. #if defined(ZXINGOBJC_QRCODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  37. #import "ZXQRCodeReader.h"
  38. #endif
  39. @interface ZXMultiFormatReader ()
  40. @property (nonatomic, strong, readonly) NSMutableArray *readers;
  41. @end
  42. @implementation ZXMultiFormatReader
  43. - (id)init {
  44. if (self = [super init]) {
  45. _readers = [NSMutableArray array];
  46. }
  47. return self;
  48. }
  49. + (id)reader {
  50. return [[ZXMultiFormatReader alloc] init];
  51. }
  52. /**
  53. * This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
  54. * passes null as a hint to the decoders. However, that makes it inefficient to call repeatedly.
  55. * Use setHints() followed by decodeWithState() for continuous scan applications.
  56. *
  57. * @param image The pixel data to decode
  58. * @return The contents of the image or nil if any errors occurred
  59. */
  60. - (ZXResult *)decode:(ZXBinaryBitmap *)image error:(NSError **)error {
  61. self.hints = nil;
  62. return [self decodeInternal:image error:error];
  63. }
  64. /**
  65. * Decode an image using the hints provided. Does not honor existing state.
  66. *
  67. * @param image The pixel data to decode
  68. * @param hints The hints to use, clearing the previous state.
  69. * @return The contents of the image or nil if any errors occurred
  70. */
  71. - (ZXResult *)decode:(ZXBinaryBitmap *)image hints:(ZXDecodeHints *)hints error:(NSError **)error {
  72. self.hints = hints;
  73. return [self decodeInternal:image error:error];
  74. }
  75. - (ZXResult *)decodeWithState:(ZXBinaryBitmap *)image error:(NSError **)error {
  76. if (self.readers == nil) {
  77. self.hints = nil;
  78. }
  79. return [self decodeInternal:image error:error];
  80. }
  81. /**
  82. * This method adds state to the MultiFormatReader. By setting the hints once, subsequent calls
  83. * to decodeWithState(image) can reuse the same set of readers without reallocating memory. This
  84. * is important for performance in continuous scan clients.
  85. *
  86. * @param hints The set of hints to use for subsequent calls to decode(image)
  87. */
  88. - (void)setHints:(ZXDecodeHints *)hints {
  89. _hints = hints;
  90. BOOL tryHarder = hints != nil && hints.tryHarder;
  91. [self.readers removeAllObjects];
  92. if (hints != nil) {
  93. BOOL addZXOneDReader = [hints containsFormat:kBarcodeFormatUPCA] ||
  94. [hints containsFormat:kBarcodeFormatUPCE] ||
  95. [hints containsFormat:kBarcodeFormatEan13] ||
  96. [hints containsFormat:kBarcodeFormatEan8] ||
  97. [hints containsFormat:kBarcodeFormatCodabar] ||
  98. [hints containsFormat:kBarcodeFormatCode39] ||
  99. [hints containsFormat:kBarcodeFormatCode93] ||
  100. [hints containsFormat:kBarcodeFormatCode128] ||
  101. [hints containsFormat:kBarcodeFormatITF] ||
  102. [hints containsFormat:kBarcodeFormatRSS14] ||
  103. [hints containsFormat:kBarcodeFormatRSSExpanded];
  104. if (addZXOneDReader && !tryHarder) {
  105. #if defined(ZXINGOBJC_ONED) || !defined(ZXINGOBJC_USE_SUBSPECS)
  106. [self.readers addObject:[[ZXMultiFormatOneDReader alloc] initWithHints:hints]];
  107. #endif
  108. }
  109. #if defined(ZXINGOBJC_QRCODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  110. if ([hints containsFormat:kBarcodeFormatQRCode]) {
  111. [self.readers addObject:[[ZXQRCodeReader alloc] init]];
  112. }
  113. #endif
  114. #if defined(ZXINGOBJC_DATAMATRIX) || !defined(ZXINGOBJC_USE_SUBSPECS)
  115. if ([hints containsFormat:kBarcodeFormatDataMatrix]) {
  116. [self.readers addObject:[[ZXDataMatrixReader alloc] init]];
  117. }
  118. #endif
  119. #if defined(ZXINGOBJC_AZTEC) || !defined(ZXINGOBJC_USE_SUBSPECS)
  120. if ([hints containsFormat:kBarcodeFormatAztec]) {
  121. [self.readers addObject:[[ZXAztecReader alloc] init]];
  122. }
  123. #endif
  124. #if defined(ZXINGOBJC_PDF417) || !defined(ZXINGOBJC_USE_SUBSPECS)
  125. if ([hints containsFormat:kBarcodeFormatPDF417]) {
  126. [self.readers addObject:[[ZXPDF417Reader alloc] init]];
  127. }
  128. #endif
  129. #if defined(ZXINGOBJC_MAXICODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  130. if ([hints containsFormat:kBarcodeFormatMaxiCode]) {
  131. [self.readers addObject:[[ZXMaxiCodeReader alloc] init]];
  132. }
  133. #endif
  134. #if defined(ZXINGOBJC_ONED) || !defined(ZXINGOBJC_USE_SUBSPECS)
  135. if (addZXOneDReader && tryHarder) {
  136. [self.readers addObject:[[ZXMultiFormatOneDReader alloc] initWithHints:hints]];
  137. }
  138. #endif
  139. }
  140. if ([self.readers count] == 0) {
  141. if (!tryHarder) {
  142. #if defined(ZXINGOBJC_ONED) || !defined(ZXINGOBJC_USE_SUBSPECS)
  143. [self.readers addObject:[[ZXMultiFormatOneDReader alloc] initWithHints:hints]];
  144. #endif
  145. }
  146. #if defined(ZXINGOBJC_QRCODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  147. [self.readers addObject:[[ZXQRCodeReader alloc] init]];
  148. #endif
  149. #if defined(ZXINGOBJC_DATAMATRIX) || !defined(ZXINGOBJC_USE_SUBSPECS)
  150. [self.readers addObject:[[ZXDataMatrixReader alloc] init]];
  151. #endif
  152. #if defined(ZXINGOBJC_AZTEC) || !defined(ZXINGOBJC_USE_SUBSPECS)
  153. [self.readers addObject:[[ZXAztecReader alloc] init]];
  154. #endif
  155. #if defined(ZXINGOBJC_PDF417) || !defined(ZXINGOBJC_USE_SUBSPECS)
  156. [self.readers addObject:[[ZXPDF417Reader alloc] init]];
  157. #endif
  158. #if defined(ZXINGOBJC_MAXICODE) || !defined(ZXINGOBJC_USE_SUBSPECS)
  159. [self.readers addObject:[[ZXMaxiCodeReader alloc] init]];
  160. #endif
  161. if (tryHarder) {
  162. #if defined(ZXINGOBJC_ONED) || !defined(ZXINGOBJC_USE_SUBSPECS)
  163. [self.readers addObject:[[ZXMultiFormatOneDReader alloc] initWithHints:hints]];
  164. #endif
  165. }
  166. }
  167. }
  168. - (void)reset {
  169. if (self.readers != nil) {
  170. for (id<ZXReader> reader in self.readers) {
  171. [reader reset];
  172. }
  173. }
  174. }
  175. - (ZXResult *)decodeInternal:(ZXBinaryBitmap *)image error:(NSError **)error {
  176. if (self.readers != nil) {
  177. for (id<ZXReader> reader in self.readers) {
  178. ZXResult *result = [reader decode:image hints:self.hints error:nil];
  179. if (result) {
  180. return result;
  181. }
  182. }
  183. }
  184. if (error) *error = ZXNotFoundErrorInstance();
  185. return nil;
  186. }
  187. @end