你的位置:首页 > 操作系统

[操作系统]ios二维码生成


二维码扫描现在已经有很多的库可以使用了,常用的有ZXing和ZBar。如果感兴趣的同学可以自行研究。

libqrencode介绍:是一个用C语言编写的用来解析二维条形码(QR Code)的程序库,libqrencode通过手机的CCD摄像头来扫描二维条形码。

  而二维码的生成我是借助于libqrencode库。创建工程项目,把libqrencode导入到项目中,创建QRCodeBuilder类,

QRCodeBuilder.h

@interfaceQRCodeBuilder :NSObject

 

+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size;

+ (UIImage *) twoDimensionCodeImage:(UIImage *)twoDimensionCode withAvatarImage:(UIImage *)avatarImage;

@end

 

QRCodeBuilder.m

 

#define qrMargin 5

@implementationQRCodeBuilder

 

+ (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size {

unsignedchar *data = 0;

int width;

data = code->data;

width = code->width;

float zoom = (double)size / (code->width + 2.0 * qrMargin);

CGRect rectDraw =CGRectMake(0, 0, zoom, zoom);

 

CGContextSetFillColor(ctx,CGColorGetComponents([UIColorblackColor].CGColor));

for(int i = 0; i < width; ++i) {

for(int j = 0; j < width; ++j) {

if(*data & 1) {

rectDraw.origin =CGPointMake((j + qrMargin) * zoom,(i +qrMargin) * zoom);

CGContextAddRect(ctx, rectDraw);

}

++data;

}

}

CGContextFillPath(ctx);

}

 

+ (UIImage *)qrImageForString:(NSString *)string imageSize:(CGFloat)size {

if (![stringlength]) {

returnnil;

}

 

QRcode *code =QRcode_encodeString([string UTF8String], 0,QR_ECLEVEL_L, QR_MODE_8, 1);

if (!code) {

returnnil;

}

 

// create context

CGColorSpaceRef colorSpace =CGColorSpaceCreateDeviceRGB();

CGContextRef ctx =CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace,kCGImageAlphaPremultipliedLast);

 

CGAffineTransform translateTransform =CGAffineTransformMakeTranslation(0, -size);

CGAffineTransform scaleTransform =CGAffineTransformMakeScale(1, -1);

CGContextConcatCTM(ctx,CGAffineTransformConcat(translateTransform, scaleTransform));

 

// draw QR on this context

[QRCodeGeneratordrawQRCode:code context:ctxsize:size];

 

// get image

CGImageRef qrCGImage =CGBitmapContextCreateImage(ctx);

UIImage * qrImage = [UIImageimageWithCGImage:qrCGImage];

 

// some releases

CGContextRelease(ctx);

CGImageRelease(qrCGImage);

CGColorSpaceRelease(colorSpace);

QRcode_free(code);

 

return qrImage;

}

 

 

+ (UIImage *) twoDimensionCodeImage:(UIImage *)twoDimensionCode withAvatarImage:(UIImage *)avatarImage{

    // two-dimension code 二维码

   CGSize size = twoDimensionCode.size;

   CGSize size2 =CGSizeMake(1.0 / 5.5 * size.width, 1.0 / 5.5 * size.height);

    

    UIGraphicsBeginImageContext(size);

    

   [twoDimensionCode drawInRect:CGRectMake(0, 0, size.width, size.height)];

   [[self avatarImage:avatarImage] drawInRect:CGRectMake((size.width - size2.width) / 2.0, (size.height - size2.height) / 2.0, size2.width, size2.height)];

    

UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

 

+ (UIImage *) avatarImage:(UIImage *)avatarImage{

   UIImage * avatarBackgroudImage = [UIImageimageNamed:@"psb.png"];

   CGSize size = avatarBackgroudImage.size;

    UIGraphicsBeginImageContext(size);

    

    [avatarBackgroudImagedrawInRect:CGRectMake(0, 0, size.width, size.height)];

    [avatarImagedrawInRect:CGRectMake(10, 10, size.width - 20, size.height - 20)];

    

UIImage *resultingImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;

}

@end

 
 

UIImageView * imageView = [[[UIImageViewalloc] initWithFrame:CGRectMake(10, 50, 300, 300)]autorelease];

// imageView.image =

    UIImage * towCode = [QRCodeGeneratorqrImageForString:@"http://blog.csdn.net/tuyaloveios"imageSize:imageView.bounds.size.width];

   UIImage * abelImage = [UIImageimageNamed:@"abel.png"];

    imageView.image = [QRCodeGeneratortwoDimensionCodeImage:towCodewithAvatarImage:abelImage];