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.

ZXQRCodeWriter.m 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "ZXBitMatrix.h"
  17. #import "ZXByteMatrix.h"
  18. #import "ZXEncodeHints.h"
  19. #import "ZXQRCode.h"
  20. #import "ZXQRCodeEncoder.h"
  21. #import "ZXQRCodeErrorCorrectionLevel.h"
  22. #import "ZXQRCodeWriter.h"
  23. const int ZX_QUIET_ZONE_SIZE = 4;
  24. @implementation ZXQRCodeWriter
  25. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height error:(NSError **)error {
  26. return [self encode:contents format:format width:width height:height hints:nil error:error];
  27. }
  28. - (ZXBitMatrix *)encode:(NSString *)contents format:(ZXBarcodeFormat)format width:(int)width height:(int)height hints:(ZXEncodeHints *)hints error:(NSError **)error {
  29. if ([contents length] == 0) {
  30. [NSException raise:NSInvalidArgumentException format:@"Found empty contents"];
  31. }
  32. if (format != kBarcodeFormatQRCode) {
  33. [NSException raise:NSInvalidArgumentException format:@"Can only encode QR_CODE"];
  34. }
  35. if (width < 0 || height < 0) {
  36. [NSException raise:NSInvalidArgumentException format:@"Requested dimensions are too small: %dx%d", width, height];
  37. }
  38. ZXQRCodeErrorCorrectionLevel *errorCorrectionLevel = [ZXQRCodeErrorCorrectionLevel errorCorrectionLevelL];
  39. int quietZone = ZX_QUIET_ZONE_SIZE;
  40. if (hints != nil) {
  41. if (hints.errorCorrectionLevel) {
  42. errorCorrectionLevel = hints.errorCorrectionLevel;
  43. }
  44. if (hints.margin) {
  45. quietZone = [hints.margin intValue];
  46. }
  47. }
  48. ZXQRCode *code = [ZXQRCodeEncoder encode:contents ecLevel:errorCorrectionLevel hints:hints error:error];
  49. return [self renderResult:code width:width height:height quietZone:quietZone];
  50. }
  51. - (ZXBitMatrix *)renderResult:(ZXQRCode *)code width:(int)width height:(int)height quietZone:(int)quietZone {
  52. ZXByteMatrix *input = code.matrix;
  53. if (input == nil) {
  54. return nil;
  55. }
  56. int inputWidth = input.width;
  57. int inputHeight = input.height;
  58. int qrWidth = inputWidth + (quietZone * 2);
  59. int qrHeight = inputHeight + (quietZone * 2);
  60. int outputWidth = MAX(width, qrWidth);
  61. int outputHeight = MAX(height, qrHeight);
  62. int multiple = MIN(outputWidth / qrWidth, outputHeight / qrHeight);
  63. // Padding includes both the quiet zone and the extra white pixels to accommodate the requested
  64. // dimensions. For example, if input is 25x25 the QR will be 33x33 including the quiet zone.
  65. // If the requested size is 200x160, the multiple will be 4, for a QR of 132x132. These will
  66. // handle all the padding from 100x100 (the actual QR) up to 200x160.
  67. int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
  68. int topPadding = (outputHeight - (inputHeight * multiple)) / 2;
  69. ZXBitMatrix *output = [[ZXBitMatrix alloc] initWithWidth:outputWidth height:outputHeight];
  70. for (int inputY = 0, outputY = topPadding; inputY < inputHeight; inputY++, outputY += multiple) {
  71. for (int inputX = 0, outputX = leftPadding; inputX < inputWidth; inputX++, outputX += multiple) {
  72. if ([input getX:inputX y:inputY] == 1) {
  73. [output setRegionAtLeft:outputX top:outputY width:multiple height:multiple];
  74. }
  75. }
  76. }
  77. return output;
  78. }
  79. @end