QRCodeGenerator.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // QR Code Generator - generates UIImage from NSString
  3. //
  4. // Copyright (C) 2012 http://moqod.com Andrew Kopanev <andrew@moqod.com>
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10. // of the Software, and to permit persons to whom the Software is furnished to do so,
  11. // subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  17. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  18. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  19. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  20. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. // DEALINGS IN THE SOFTWARE.
  22. //
  23. #import "QRCodeGenerator.h"
  24. #import "qrencode.h"
  25. enum {
  26. qr_margin = 3
  27. };
  28. @implementation QRCodeGenerator
  29. + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {
  30. unsigned char *data = 0;
  31. int width;
  32. data = code->data;
  33. width = code->width;
  34. float zoom = (double)size / (code->width + 2.0 * qr_margin);
  35. CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);
  36. // draw
  37. CGContextSetFillColor(ctx, CGColorGetComponents([UIColor blackColor].CGColor));
  38. for(int i = 0; i < width; ++i) {
  39. for(int j = 0; j < width; ++j) {
  40. if(*data & 1) {
  41. rectDraw.origin = CGPointMake( (int) ((j + qr_margin) * zoom+0.5) ,(int)((i + qr_margin) * zoom+0.5));
  42. CGContextAddRect(ctx, rectDraw);
  43. //CGContextFillRect(ctx, rectDraw);
  44. }
  45. ++data;
  46. }
  47. }
  48. CGContextFillPath(ctx);
  49. }
  50. + (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {
  51. if (![string length]) {
  52. return nil;
  53. }
  54. QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1);
  55. if (!code) {
  56. return nil;
  57. }
  58. // create context
  59. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  60. CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
  61. CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
  62. CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
  63. CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
  64. // draw QR on this context
  65. [QRCodeGenerator drawQRCode:code context:ctx size:size];
  66. // get image
  67. CGImageRef qrCGImage = CGBitmapContextCreateImage(ctx);
  68. UIImage * qrImage = [UIImage imageWithCGImage:qrCGImage];
  69. // some releases
  70. CGContextRelease(ctx);
  71. CGImageRelease(qrCGImage);
  72. CGColorSpaceRelease(colorSpace);
  73. QRcode_free(code);
  74. return qrImage;
  75. }
  76. @end