Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ZXMaxiCodeReader.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "ZXBitMatrix.h"
  18. #import "ZXDecodeHints.h"
  19. #import "ZXDecoderResult.h"
  20. #import "ZXErrors.h"
  21. #import "ZXIntArray.h"
  22. #import "ZXMaxiCodeDecoder.h"
  23. #import "ZXMaxiCodeReader.h"
  24. #import "ZXResult.h"
  25. const int ZX_MATRIX_WIDTH = 30;
  26. const int ZX_MATRIX_HEIGHT = 33;
  27. @interface ZXMaxiCodeReader ()
  28. @property (nonatomic, strong, readonly) ZXMaxiCodeDecoder *decoder;
  29. @end
  30. @implementation ZXMaxiCodeReader
  31. - (id)init {
  32. if (self = [super init]) {
  33. _decoder = [[ZXMaxiCodeDecoder alloc] init];
  34. }
  35. return self;
  36. }
  37. /**
  38. * Locates and decodes a MaxiCode in an image.
  39. *
  40. * @return a String representing the content encoded by the MaxiCode
  41. * @return nil if a MaxiCode cannot be found
  42. * @return nil if a MaxiCode cannot be decoded
  43. * @return nil if error correction fails
  44. */
  45. - (ZXResult *)decode:(ZXBinaryBitmap *)image error:(NSError **)error {
  46. return [self decode:image hints:nil error:error];
  47. }
  48. - (ZXResult *)decode:(ZXBinaryBitmap *)image hints:(ZXDecodeHints *)hints error:(NSError **)error {
  49. ZXDecoderResult *decoderResult;
  50. if (hints != nil && hints.pureBarcode) {
  51. ZXBitMatrix *matrix = [image blackMatrixWithError:error];
  52. if (!matrix) {
  53. return nil;
  54. }
  55. ZXBitMatrix *bits = [self extractPureBits:matrix];
  56. if (!bits) {
  57. if (error) *error = ZXNotFoundErrorInstance();
  58. return nil;
  59. }
  60. decoderResult = [self.decoder decode:bits hints:hints error:error];
  61. if (!decoderResult) {
  62. return nil;
  63. }
  64. } else {
  65. if (error) *error = ZXNotFoundErrorInstance();
  66. return nil;
  67. }
  68. NSArray *points = @[];
  69. ZXResult *result = [ZXResult resultWithText:decoderResult.text
  70. rawBytes:decoderResult.rawBytes
  71. resultPoints:points
  72. format:kBarcodeFormatMaxiCode];
  73. NSString *ecLevel = decoderResult.ecLevel;
  74. if (ecLevel != nil) {
  75. [result putMetadata:kResultMetadataTypeErrorCorrectionLevel value:ecLevel];
  76. }
  77. return result;
  78. }
  79. - (void)reset {
  80. // do nothing
  81. }
  82. /**
  83. * This method detects a code in a "pure" image -- that is, pure monochrome image
  84. * which contains only an unrotated, unskewed, image of a code, with some white border
  85. * around it. This is a specialized method that works exceptionally fast in this special
  86. * case.
  87. */
  88. - (ZXBitMatrix *)extractPureBits:(ZXBitMatrix *)image {
  89. ZXIntArray *enclosingRectangle = image.enclosingRectangle;
  90. if (enclosingRectangle == nil) {
  91. return nil;
  92. }
  93. int left = enclosingRectangle.array[0];
  94. int top = enclosingRectangle.array[1];
  95. int width = enclosingRectangle.array[2];
  96. int height = enclosingRectangle.array[3];
  97. // Now just read off the bits
  98. ZXBitMatrix *bits = [[ZXBitMatrix alloc] initWithWidth:ZX_MATRIX_WIDTH height:ZX_MATRIX_HEIGHT];
  99. for (int y = 0; y < ZX_MATRIX_HEIGHT; y++) {
  100. int iy = top + (y * height + height / 2) / ZX_MATRIX_HEIGHT;
  101. for (int x = 0; x < ZX_MATRIX_WIDTH; x++) {
  102. int ix = left + (x * width + width / 2 + (y & 0x01) * width / 2) / ZX_MATRIX_WIDTH;
  103. if ([image getX:ix y:iy]) {
  104. [bits setX:x y:y];
  105. }
  106. }
  107. }
  108. return bits;
  109. }
  110. @end