Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZXUPCEWriter.m 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "ZXUPCEWriter.h"
  17. #import "ZXUPCEANReader.h"
  18. #import "ZXUPCEReader.h"
  19. #import "ZXBoolArray.h"
  20. const int ZX_UPCE_CODE_WIDTH = 3 + (7 * 6) + 6;
  21. @implementation ZXUPCEWriter
  22. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  23. if (format != kBarcodeFormatUPCE) {
  24. [NSException raise:NSInvalidArgumentException format:@"Can only encode UPC_E"];
  25. }
  26. return [super encode:contents format:format width:width height:height hints:hints error:error];
  27. }
  28. - (ZXBoolArray *)encode:(NSString *)contents {
  29. if ([contents length] != 8) {
  30. @throw [NSException exceptionWithName:@"IllegalArgumentException"
  31. reason:[NSString stringWithFormat:@"Requested contents should be 8 digits long, but got %d", (int)[contents length]]
  32. userInfo:nil];
  33. }
  34. int checkDigit = [[contents substringWithRange:NSMakeRange(7, 1)] intValue];
  35. int parities = CHECK_DIGIT_ENCODINGS[checkDigit];
  36. ZXBoolArray *result = [[ZXBoolArray alloc] initWithLength:ZX_UPCE_CODE_WIDTH];
  37. int pos = 0;
  38. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_START_END_PATTERN patternLen:ZX_UPC_EAN_START_END_PATTERN_LEN startColor:YES];
  39. for (int i = 1; i <= 6; i++) {
  40. int digit = [[contents substringWithRange:NSMakeRange(i, 1)] intValue];
  41. if ((parities >> (6 - i) & 1) == 1) {
  42. digit += 10;
  43. }
  44. pos += [self appendPattern:result pos:pos pattern:ZX_UPC_EAN_L_AND_G_PATTERNS[digit] patternLen:ZX_UPC_EAN_L_PATTERNS_SUB_LEN startColor:NO];
  45. }
  46. [self appendPattern:result pos:pos pattern:ZX_UPCE_MIDDLE_END_PATTERN patternLen:ZX_UPCE_MIDDLE_END_PATTERN_LEN startColor:NO];
  47. return result;
  48. }
  49. @end