技术文章分类(180)

技术随笔(11)

ios二维码的生成和扫描

注意:生成和扫描是完全独立的功能,没有任何联系,没有公共库

一,二维码的扫描:

整个demo下载:https://github.com/MartinLi841538513/TestZBarSDK/archive/master.zip

 

准备工作:

1.下载ZBarSDK https://github.com/MartinLi841538513/ZBarSDK/archive/master.zip ,加入到项目中

2.导入如下框架(其中libzbar.a是ZBarSDK中的)

 

3.导入#import "ZBarSDK.h"

4.继承协议@interface ViewController :UIViewController<ZBarReaderViewDelegate>

 

扫描步骤:

 

 1.通过摄像头获取图片

 2.通过Zbar的内部处理方法来识别图片

 3.处理信息并显示


代码实现:

//
//  MLViewController.m
//  TestZBar
//
//  Created by dongway on 14-7-31.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

#import "MLViewController.h"
#import "ZBarSDK.h"

@interface MLViewController ()
{
    ZBarReaderView *zReaderView;
}

@end

@implementation MLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)scanQRCode:(id)sender {
    zReaderView = [[ZBarReaderView alloc]init];
    zReaderView.frame = CGRectMake(10, 44, 300, 300);
    zReaderView.readerDelegate = self;
    [self.view addSubview:zReaderView];
    [zReaderView start];
}


- (void) readerView: (ZBarReaderView*) readerView didReadSymbols: (ZBarSymbolSet*) symbols fromImage: (UIImage*) image{
    ZBarSymbol *s = nil;
    for (s in symbols){
        self.url.text = s.data;
        self.imageView.image = image;
        [readerView removeFromSuperview];
    }
}
@end

 

我再第二种扫一扫的方式,我认为在实际项目中效果更好。还是用的ZBarSDK,用 pod 'ZBarSDK', '~> 1.3.1'比上面那种导入库的方法更方便。

#import "ZBarSDKDemoViewController.h"
#import <ZBarSDK.h>

@interface ZBarSDKDemoViewController ()<ZBarReaderDelegate>

@end

@implementation ZBarSDKDemoViewController

- (IBAction)scanAction:(id)sender {
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    reader.readerDelegate = self;
    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    [self presentViewController:reader animated:YES completion:nil];
}

#pragma ZBarReaderDelegate
- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    NSString *textd = symbol.data;
    //解决中文乱码问题
    if ([textd canBeConvertedToEncoding:NSShiftJISStringEncoding])
    {
        textd = [NSString stringWithCString:[textd cStringUsingEncoding:
                                             NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
    }
    self.info.text = textd;
    [reader dismissViewControllerAnimated:YES completion:nil];
}
@end

 

 

 二,二维码的生成(建议大家使用下面这种,网上有一些其他方法在模拟器上可用,在真机上没有效果,慎用)

下载demo:https://github.com/MartinLi841538513/ProduceQRCodeDemo

1,pod 'libqrencode', '~> 3.4.2'

 

并且引入QRCodeGenerator.h  .m   ,注意这两个文件在我的demo里面。

2,

#import "QRCodeGenerator.h"

self.imgView.image = [QRCodeGenerator qrImageForString:@"test" imageSize:self.imgView.frame.size.width];

 

 

ok了,二维码包含的信息就是“test” 

posted @ 2014-06-25 09:56  坤哥MartinLi  阅读(333)  评论(0编辑  收藏  举报