二維碼掃描現(xiàn)在已經(jīng)有很多的庫可以使用了,常用的有ZXing和ZBar擎值。如果感興趣的同學(xué)可以自行研究麻顶。
libqrencode介紹:是一個用C語言編寫的用來解析二維條形碼(QR
Code)的程序庫,libqrencode通過手機(jī)的CCD攝像頭來掃描二維條形碼。
而二維碼的生成我是借助于libqrencode庫浅悉。創(chuàng)建工程項目,把libqrencode導(dǎo)入到項目中券犁,創(chuàng)建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];