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.

ZXAztecWriter.m 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2013 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 "ZXAztecCode.h"
  17. #import "ZXAztecEncoder.h"
  18. #import "ZXAztecWriter.h"
  19. #import "ZXBitMatrix.h"
  20. #import "ZXByteArray.h"
  21. #import "ZXEncodeHints.h"
  22. const NSStringEncoding ZX_AZTEC_DEFAULT_ENCODING = NSISOLatin1StringEncoding;
  23. @implementation ZXAztecWriter
  24. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height error:(NSError **)error {
  25. return [self encode:contents format:format width:width height:height hints:nil error:error];
  26. }
  27. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  28. NSStringEncoding encoding = hints.encoding;
  29. NSNumber *eccPercent = hints.errorCorrectionPercent;
  30. NSNumber *layers = hints.aztecLayers;
  31. return [self encode:contents
  32. format:format
  33. width:width
  34. height:height
  35. encoding:encoding == 0 ? ZX_AZTEC_DEFAULT_ENCODING : encoding
  36. eccPercent:eccPercent == nil ? ZX_AZTEC_DEFAULT_EC_PERCENT : [eccPercent intValue]
  37. layers:layers == nil ? ZX_AZTEC_DEFAULT_LAYERS : [layers intValue]];
  38. }
  39. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width
  40. height:(int)height encoding:(NSStringEncoding)encoding eccPercent:(int)eccPercent layers:(int)layers {
  41. if (format != kBarcodeFormatAztec) {
  42. @throw [NSException exceptionWithName:NSInvalidArgumentException
  43. reason:[NSString stringWithFormat:@"Can only encode kBarcodeFormatAztec (%d), but got %d", kBarcodeFormatAztec, format]
  44. userInfo:nil];
  45. }
  46. NSData *data = [contents dataUsingEncoding:encoding];
  47. ZXByteArray *bytes = [[ZXByteArray alloc] initWithLength:(unsigned int)[data length]];
  48. memcpy(bytes.array, [data bytes], bytes.length * sizeof(int8_t));
  49. ZXAztecCode *aztec = [ZXAztecEncoder encode:bytes minECCPercent:eccPercent userSpecifiedLayers:layers];
  50. return [self renderResult:aztec width:width height:height];
  51. }
  52. - (ZXBitMatrix *)renderResult:(ZXAztecCode *)aztec width:(int)width height:(int)height {
  53. ZXBitMatrix *input = aztec.matrix;
  54. if (!input) {
  55. return nil;
  56. }
  57. int inputWidth = input.width;
  58. int inputHeight = input.height;
  59. int outputWidth = MAX(width, inputWidth);
  60. int outputHeight = MAX(height, inputHeight);
  61. int multiple = MIN(outputWidth / inputWidth, outputHeight / inputHeight);
  62. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  63. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  64. ZXBitMatrix *output = [[ZXBitMatrix alloc] initWithWidth:outputWidth height:outputHeight];
  65. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  66. // Write the contents of this row of the barcode
  67. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  68. if ([input getX:inputX y:inputY]) {
  69. [output setRegionAtLeft:outputX top:outputY width:multiple height:multiple];
  70. }
  71. }
  72. }
  73. return output;
  74. }
  75. @end