Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ZXMaxiCodeDecoder.m 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "ZXBitMatrix.h"
  17. #import "ZXByteArray.h"
  18. #import "ZXDecodeHints.h"
  19. #import "ZXDecoderResult.h"
  20. #import "ZXErrors.h"
  21. #import "ZXGenericGF.h"
  22. #import "ZXIntArray.h"
  23. #import "ZXMaxiCodeBitMatrixParser.h"
  24. #import "ZXMaxiCodeDecodedBitStreamParser.h"
  25. #import "ZXMaxiCodeDecoder.h"
  26. #import "ZXReedSolomonDecoder.h"
  27. const int ZX_MAXI_CODE_ALL = 0;
  28. const int ZX_MAXI_CODE_EVEN = 1;
  29. const int ZX_MAXI_CODE_ODD = 2;
  30. @interface ZXMaxiCodeDecoder ()
  31. @property (nonatomic, strong, readonly) ZXReedSolomonDecoder *rsDecoder;
  32. @end
  33. @implementation ZXMaxiCodeDecoder
  34. - (id)init {
  35. if (self = [super init]) {
  36. _rsDecoder = [[ZXReedSolomonDecoder alloc] initWithField:[ZXGenericGF MaxiCodeField64]];
  37. }
  38. return self;
  39. }
  40. - (ZXDecoderResult *)decode:(ZXBitMatrix *)bits error:(NSError **)error {
  41. return [self decode:bits hints:nil error:error];
  42. }
  43. - (ZXDecoderResult *)decode:(ZXBitMatrix *)bits hints:(ZXDecodeHints *)hints error:(NSError **)error {
  44. ZXMaxiCodeBitMatrixParser *parser = [[ZXMaxiCodeBitMatrixParser alloc] initWithBitMatrix:bits error:error];
  45. if (!parser) {
  46. return nil;
  47. }
  48. ZXByteArray *codewords = [parser readCodewords];
  49. if (![self correctErrors:codewords start:0 dataCodewords:10 ecCodewords:10 mode:ZX_MAXI_CODE_ALL error:error]) {
  50. return nil;
  51. }
  52. int mode = codewords.array[0] & 0x0F;
  53. ZXByteArray *datawords;
  54. switch (mode) {
  55. case 2:
  56. case 3:
  57. case 4:
  58. if (![self correctErrors:codewords start:20 dataCodewords:84 ecCodewords:40 mode:ZX_MAXI_CODE_EVEN error:error]) {
  59. return nil;
  60. }
  61. if (![self correctErrors:codewords start:20 dataCodewords:84 ecCodewords:40 mode:ZX_MAXI_CODE_ODD error:error]) {
  62. return nil;
  63. }
  64. datawords = [[ZXByteArray alloc] initWithLength:94];
  65. break;
  66. case 5:
  67. if (![self correctErrors:codewords start:20 dataCodewords:68 ecCodewords:56 mode:ZX_MAXI_CODE_EVEN error:error]) {
  68. return nil;
  69. }
  70. if (![self correctErrors:codewords start:20 dataCodewords:68 ecCodewords:56 mode:ZX_MAXI_CODE_ODD error:error]) {
  71. return nil;
  72. }
  73. datawords = [[ZXByteArray alloc] initWithLength:78];
  74. break;
  75. default:
  76. if (error) *error = ZXNotFoundErrorInstance();
  77. return nil;
  78. }
  79. for (int i = 0; i < 10; i++) {
  80. datawords.array[i] = codewords.array[i];
  81. }
  82. for (int i = 20; i < datawords.length + 10; i++) {
  83. datawords.array[i - 10] = codewords.array[i];
  84. }
  85. return [ZXMaxiCodeDecodedBitStreamParser decode:datawords mode:mode];
  86. }
  87. - (BOOL)correctErrors:(ZXByteArray *)codewordBytes start:(int)start dataCodewords:(int)dataCodewords
  88. ecCodewords:(int)ecCodewords mode:(int)mode error:(NSError **)error {
  89. int codewords = dataCodewords + ecCodewords;
  90. // in EVEN or ODD mode only half the codewords
  91. int divisor = mode == ZX_MAXI_CODE_ALL ? 1 : 2;
  92. // First read into an array of ints
  93. ZXIntArray *codewordsInts = [[ZXIntArray alloc] initWithLength:codewords / divisor];
  94. for (int i = 0; i < codewords; i++) {
  95. if ((mode == ZX_MAXI_CODE_ALL) || (i % 2 == (mode - 1))) {
  96. codewordsInts.array[i / divisor] = codewordBytes.array[i + start] & 0xFF;
  97. }
  98. }
  99. NSError *decodeError = nil;
  100. if (![self.rsDecoder decode:codewordsInts twoS:ecCodewords / divisor error:&decodeError]) {
  101. if (decodeError.code == ZXReedSolomonError && error) {
  102. *error = ZXChecksumErrorInstance();
  103. }
  104. return NO;
  105. }
  106. // Copy back into array of bytes -- only need to worry about the bytes that were data
  107. // We don't care about errors in the error-correction codewords
  108. for (int i = 0; i < dataCodewords; i++) {
  109. if ((mode == ZX_MAXI_CODE_ALL) || (i % 2 == (mode - 1))) {
  110. codewordBytes.array[i + start] = (int8_t) codewordsInts.array[i / divisor];
  111. }
  112. }
  113. return YES;
  114. }
  115. @end