原文地址:iOS內建掃描 和 產生二維碼
1.二維碼產生
libqrencode介紹:是一個用C語言編寫的用來解析二維條碼(QRCode)的程式庫,libqrencode通過手機的CCD網路攝影機來掃描二維條碼。http://pan.baidu.com/s/1eQs1Epk
匯入libqrencode庫,下面直接代碼:
[objc] view plain copy #import <Foundation/Foundation.h> @interface QRCodeGenerator : NSObject + (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size; + (UIImage *) twoDimensionCodeImage:(UIImage *)twoDimensionCode withAvatarImage:(UIImage *)avatarImage; @end
[objc] view plain copy #import "QRCodeGenerator.h" #import "qrencode.h" enum { qr_margin = 3 }; @implementation QRCodeGenerator + (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { unsigned charchar *data = 0; int width; data = code->data; width = code->width; float zoom = (double)size / (code->width + 2.0 * qr_margin); CGRect rectDraw = CGRectMake(0, 0, zoom, zoom); // draw CGContextSetFillColor(ctx, CGColorGetComponents([UIColor blackColor].CGColor)); for(int i = 0; i < width; ++i) { for(int j = 0; j < width; ++j) { if(*data & 1) { rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom); CGContextAddRect(ctx, rectDraw); } ++data; } } CGContextFillPath(ctx); } + (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size { if (![string length]) { return nil; } QRcode *code = QRcode_encodeString([string UTF8String], 0, QR_ECLEVEL_L, QR_MODE_8, 1