Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ZXUPCAWriter.m 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "ZXEAN13Writer.h"
  17. #import "ZXUPCAWriter.h"
  18. @implementation ZXUPCAWriter
  19. - (ZXEAN13Writer *)subWriter {
  20. static ZXEAN13Writer *subWriter = nil;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. subWriter = [[ZXEAN13Writer alloc] init];
  24. });
  25. return subWriter;
  26. }
  27. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height error:(NSError **)error {
  28. return [self encode:contents format:format width:width height:height hints:nil error:error];
  29. }
  30. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  31. if (format != kBarcodeFormatUPCA) {
  32. @throw [NSException exceptionWithName:NSInvalidArgumentException
  33. reason:[NSString stringWithFormat:@"Can only encode UPC-A, but got %d", format]
  34. userInfo:nil];
  35. }
  36. return [self.subWriter encode:[self preencode:contents] format:kBarcodeFormatEan13 width:width height:height hints:hints error:error];
  37. }
  38. /**
  39. * Transform a UPC-A code into the equivalent EAN-13 code, and add a check digit if it is not
  40. * already present.
  41. */
  42. - (NSString *)preencode:(NSString *)contents {
  43. NSUInteger length = [contents length];
  44. if (length == 11) {
  45. int sum = 0;
  46. for (int i = 0; i < 11; ++i) {
  47. sum += ([contents characterAtIndex:i] - '0') * (i % 2 == 0 ? 3 : 1);
  48. }
  49. contents = [contents stringByAppendingFormat:@"%d", (1000 - sum) % 10];
  50. } else if (length != 12) {
  51. @throw [NSException exceptionWithName:NSInvalidArgumentException
  52. reason:[NSString stringWithFormat:@"Requested contents should be 11 or 12 digits long, but got %ld", (unsigned long)[contents length]]
  53. userInfo:nil];
  54. }
  55. return [NSString stringWithFormat:@"0%@", contents];
  56. }
  57. @end