星空网 > 软件开发 > 操作系统

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];




原标题:ios二维码生成

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

外媒:继拉美之后,Shopee或进军印度市场:https://www.kjdsnews.com/a/537621.html
看千亿市场下的男颜经济,东南亚男性如何“为颜”剁手:https://www.kjdsnews.com/a/537622.html
8月盘点 | 深圳、郑州、佛山、嘉兴等地陆续发布跨境电商扶持政策:https://www.kjdsnews.com/a/537623.html
Shopify可以跟卖吗?Shopify跟卖注意事项:https://www.kjdsnews.com/a/537624.html
Shopee可能开设印度站点?印尼二季度经济增速超韩国、越南:https://www.kjdsnews.com/a/537625.html
亚马逊保险新规9月1日生效!一文解决8大疑难问题:https://www.kjdsnews.com/a/537626.html
天坛最佳攻略 天坛必玩景点:https://www.vstour.cn/a/408240.html
央视新址为什么会找回:https://www.vstour.cn/a/408241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流