Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ZXEAN13Writer.m 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "ZXBarcodeFormat.h"
  17. #import "ZXBoolArray.h"
  18. #import "ZXEAN13Reader.h"
  19. #import "ZXEAN13Writer.h"
  20. #import "ZXUPCEANReader.h"
  21. const int ZX_EAN13_CODE_WIDTH = 3 + // start guard
  22. (7 * 6) + // left bars
  23. 5 + // middle guard
  24. (7 * 6) + // right bars
  25. 3; // end guard
  26. @implementation ZXEAN13Writer
  27. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  28. if (format != kBarcodeFormatEan13) {
  29. @throw [NSException exceptionWithName:NSInvalidArgumentException
  30. reason:[NSString stringWithFormat:@"Can only encode EAN_13, but got %d", format]
  31. userInfo:nil];
  32. }
  33. return [super encode:contents format:format width:width height:height hints:hints error:error];
  34. }
  35. - (ZXBoolArray *)encode:(NSString *)contents {
  36. if ([contents length] != 13) {
  37. [NSException raise:NSInvalidArgumentException
  38. format:@"Requested contents should be 13 digits long, but got %d", (int)[contents length]];
  39. }
  40. if (![ZXUPCEANReader checkStandardUPCEANChecksum:contents]) {
  41. [NSException raise:NSInvalidArgumentException
  42. format:@"Contents do not pass checksum"];
  43. }
  44. int firstDigit = [[contents substringToIndex:1] intValue];
  45. int parities = ZX_EAN13_FIRST_DIGIT_ENCODINGS[firstDigit];
  46. ZXBoolArray *result = [[ZXBoolArray alloc] initWithLength:ZX_EAN13_CODE_WIDTH];
  47. int pos = 0;
  48. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_START_END_PATTERN patternLen:ZX_UPC_EAN_START_END_PATTERN_LEN startColor:YES];
  49. for (int i = 1; i <= 6; i++) {
  50. int digit = [[contents substringWithRange:NSMakeRange(i, 1)] intValue];
  51. if ((parities >> (6 - i) & 1) == 1) {
  52. digit += 10;
  53. }
  54. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_L_AND_G_PATTERNS[digit] patternLen:ZX_UPC_EAN_L_PATTERNS_SUB_LEN startColor:FALSE];
  55. }
  56. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_MIDDLE_PATTERN patternLen:ZX_UPC_EAN_MIDDLE_PATTERN_LEN startColor:FALSE];
  57. for (int i = 7; i <= 12; i++) {
  58. int digit = [[contents substringWithRange:NSMakeRange(i, 1)] intValue];
  59. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_L_PATTERNS[digit] patternLen:ZX_UPC_EAN_L_PATTERNS_SUB_LEN startColor:YES];
  60. }
  61. [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_START_END_PATTERN patternLen:ZX_UPC_EAN_START_END_PATTERN_LEN startColor:YES];
  62. return result;
  63. }
  64. @end