You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ZXAI013x0x1xDecoder.m 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "ZXAI013x0x1xDecoder.h"
  17. #import "ZXBitArray.h"
  18. #import "ZXErrors.h"
  19. #import "ZXRSSExpandedGeneralAppIdDecoder.h"
  20. const int ZX_AI013x0x1x_HEADER_SIZE = 7 + 1;
  21. const int ZX_AI013x0x1x_WEIGHT_SIZE = 20;
  22. const int ZX_AI013x0x1x_DATE_SIZE = 16;
  23. @interface ZXAI013x0x1xDecoder ()
  24. @property (nonatomic, copy, readonly) NSString *dateCode;
  25. @property (nonatomic, copy, readonly) NSString *firstAIdigits;
  26. @end
  27. @implementation ZXAI013x0x1xDecoder
  28. - (id)initWithInformation:(ZXBitArray *)information firstAIdigits:(NSString *)firstAIdigits dateCode:(NSString *)dateCode {
  29. if (self = [super initWithInformation:information]) {
  30. _dateCode = dateCode;
  31. _firstAIdigits = firstAIdigits;
  32. }
  33. return self;
  34. }
  35. - (NSString *)parseInformationWithError:(NSError **)error {
  36. if (self.information.size != ZX_AI013x0x1x_HEADER_SIZE + ZX_AI01_GTIN_SIZE + ZX_AI013x0x1x_WEIGHT_SIZE + ZX_AI013x0x1x_DATE_SIZE) {
  37. if (error) *error = ZXNotFoundErrorInstance();
  38. return nil;
  39. }
  40. NSMutableString *buf = [NSMutableString string];
  41. [self encodeCompressedGtin:buf currentPos:ZX_AI013x0x1x_HEADER_SIZE];
  42. [self encodeCompressedWeight:buf currentPos:ZX_AI013x0x1x_HEADER_SIZE + ZX_AI01_GTIN_SIZE weightSize:ZX_AI013x0x1x_WEIGHT_SIZE];
  43. [self encodeCompressedDate:buf currentPos:ZX_AI013x0x1x_HEADER_SIZE + ZX_AI01_GTIN_SIZE + ZX_AI013x0x1x_WEIGHT_SIZE];
  44. return buf;
  45. }
  46. - (void)encodeCompressedDate:(NSMutableString *)buf currentPos:(int)currentPos {
  47. int numericDate = [self.generalDecoder extractNumericValueFromBitArray:currentPos bits:ZX_AI013x0x1x_DATE_SIZE];
  48. if (numericDate == 38400) {
  49. return;
  50. }
  51. [buf appendFormat:@"(%@)", self.dateCode];
  52. int day = numericDate % 32;
  53. numericDate /= 32;
  54. int month = numericDate % 12 + 1;
  55. numericDate /= 12;
  56. int year = numericDate;
  57. if (year / 10 == 0) {
  58. [buf appendString:@"0"];
  59. }
  60. [buf appendFormat:@"%d", year];
  61. if (month / 10 == 0) {
  62. [buf appendString:@"0"];
  63. }
  64. [buf appendFormat:@"%d", month];
  65. if (day / 10 == 0) {
  66. [buf appendString:@"0"];
  67. }
  68. [buf appendFormat:@"%d", day];
  69. }
  70. - (void)addWeightCode:(NSMutableString *)buf weight:(int)weight {
  71. int lastAI = weight / 100000;
  72. [buf appendFormat:@"(%@%d)", self.firstAIdigits, lastAI];
  73. }
  74. - (int)checkWeight:(int)weight {
  75. return weight % 100000;
  76. }
  77. @end