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.

ZXAI01decoder.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "ZXAI01decoder.h"
  17. #import "ZXBitArray.h"
  18. #import "ZXRSSExpandedGeneralAppIdDecoder.h"
  19. const int ZX_AI01_GTIN_SIZE = 40;
  20. @implementation ZXAI01decoder
  21. - (void)encodeCompressedGtin:(NSMutableString *)buf currentPos:(int)currentPos {
  22. [buf appendString:@"(01)"];
  23. int initialPosition = (int)[buf length];
  24. [buf appendString:@"9"];
  25. [self encodeCompressedGtinWithoutAI:buf currentPos:currentPos initialBufferPosition:initialPosition];
  26. }
  27. - (void)encodeCompressedGtinWithoutAI:(NSMutableString *)buf currentPos:(int)currentPos initialBufferPosition:(int)initialBufferPosition {
  28. for (int i = 0; i < 4; ++i) {
  29. int currentBlock = [self.generalDecoder extractNumericValueFromBitArray:currentPos + 10 * i bits:10];
  30. if (currentBlock / 100 == 0) {
  31. [buf appendString:@"0"];
  32. }
  33. if (currentBlock / 10 == 0) {
  34. [buf appendString:@"0"];
  35. }
  36. [buf appendFormat:@"%d", currentBlock];
  37. }
  38. [self appendCheckDigit:buf currentPos:initialBufferPosition];
  39. }
  40. - (void)appendCheckDigit:(NSMutableString *)buf currentPos:(int)currentPos {
  41. int checkDigit = 0;
  42. for (int i = 0; i < 13; i++) {
  43. int digit = [buf characterAtIndex:i + currentPos] - '0';
  44. checkDigit += (i & 0x01) == 0 ? 3 * digit : digit;
  45. }
  46. checkDigit = 10 - (checkDigit % 10);
  47. if (checkDigit == 10) {
  48. checkDigit = 0;
  49. }
  50. [buf appendFormat:@"%d", checkDigit];
  51. }
  52. @end