iOS--二维码(libqrencode、ZBarSDK)

二维码就是保存一些字符串的信息。 

  第三方库下载地址:http://download.csdn.net/detail/u010165653/8294135

创建二维码图片:

使用第三方库,libqrencode,

01:导入第三方库,这个库不需要额外的系统类库。

02 :导入头文件  #import"QRCodeGenerator.h"

03:使用方法:

UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,50,220, 220)];

    [self.view addSubview:imageView];

    [imageView release];

    

    imageView.image = [QRCodeGenerator qrImageForString:@"要转成二维码的文本" imageSize:imageView.bounds.size.width];


示例:
#import <UIKit/UIKit.h>
#import "QRCodeGenerator.h"

@interface ViewController : UIViewController

@end<span style="color:#b4261a;">
</span>
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 220, 220)];
    [self.view addSubview:imageView];
    [imageView release];
    
    imageView.image = [QRCodeGenerator qrImageForString:@"要转成二维码的文本" imageSize:imageView.bounds.size.width];
}


@end

 扫描二维码图片:
使用第三方库, ZBarSDK

           01:导入第三方库,这个库依赖系统类库,所以还需要导入系统类库有:

  AVFoundation.framework

  CoreMedia.framework

  CoreVideo.framework

  QuartzCore.framework

 libiconv.dylib

02:导入头文件: #import "ZBarSDK.h"
   03:创建ZBarReaderController对象,设置代理,遵从 
UINavigationControllerDelegate,UIImagePickerControllerDelegate

示例代码:

#import <UIKit/UIKit.h>
#import "ZBarSDK.h"
/*
 导入ZBarSDK文件并引入一下框架
 AVFoundation.framework
 CoreMedia.framework
 CoreVideo.framework
 QuartzCore.framework
 libiconv.dylib
 引入头文件#import “ZBarSDK.h” 即可使用
 */

@interface ViewController : UIViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 10, 100, 40);
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    

}

- (void)buttonClick{
    ZBarReaderController* zbarController = [[ZBarReaderController alloc] init];
    zbarController.delegate = self;
    [self presentModalViewController:zbarController animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    //最后得到的结果
    NSLog(@"%@",symbol.data);
    
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
}


@end



你可能感兴趣的:(iOS--二维码(libqrencode、ZBarSDK))