美文网首页
iOS中Zbar二维码扫描的使用 2015-04-29

iOS中Zbar二维码扫描的使用 2015-04-29

作者: blaze冰叔 | 来源:发表于2019-07-19 14:47 被阅读0次

    1)由于苹果的要求,从2015年2月1日开始,所有新上传的APP必须支持64位已经使用iOS8的SDK。具体苹果方便的声明信息的链接如下:https://developer.apple.com/news/?id=10202014a,当我们在进行二维码扫描时,会发现原来的Zbar库会不支持如下的设备:iPhone 5s,iPhone 6,iPhone 6P,原因就是这三类设备的指令集为arm64。

    2)由于必须支持64位的要求,我们引用的第三方库也必须遵守。(主要针对第三方库已静态库文件.a和框架形framework形式存在的),我们可以在终端按下面的操作来判断当前第三个库支持哪些架构(指令集)

    a)进入到相应包含第三方框架中静态文件后框架的文件路径

    b)在终端使用如下命令 lipo -info libzbar.a

    [url=%20https://github.com/eric-schmitt/ZBar-arm64,1] https://github.com/eric-schmitt/ZBar-arm64[/url]

    如若我的Zbar库在桌面,你可以按如下步骤。

    a)cd /Users/pianopeng/Desktop/ZBar_arm64

    b)lipo -info libzbar.a

    终端会如下输出:Architectures in the fat file: libzbar.a are: armv7 armv7s i386 x86_64 arm64

    其中armv7为iPhone4、4s的CPU使用的指令集,armv7s为5、5c的CPU使用的指令集,arm64为iPhone 5s,iPhone 6,iPhone 6P的CPU使用的指令集。i386 x86_64为模拟器使用的。

    有关指令集与架构的相关信息可以参考如下链接的信息http://www.cocoachina.com/industry/20140527/8566.html,以及如何支持64位http://www.cocoachina.com/ios/20141024/10031.html

    3)开始部署Zbar库

    a)将Zbar库拖到自己的工程的根目录下,(当然可以按照你自己的喜欢,尽情的添加到哪里,只要你觉得合适)

    b)添加相应的框架AVFoundation.framework,CoreMedia.framework,CoreVideo.framework,QuartzCore.framework,libiconv.dylib。

    c)开始代码实现,在需要实现扫描的页面里导入#import "ZBarSDK.h"。并遵守相关的协议。具体的代码以及注释如下所示。

    #import "ECScanViewController.h"
    #import "ZBarSDK.h"
    @interface ECScanViewController ()<ZBarReaderDelegate>
    @property (strong, nonatomic) ZBarReaderViewController *readerVC;
    @property (assign, nonatomic) NSInteger num;
    @property (strong, nonatomic) NSTimer *timer;
    @end
    @implementation ECScanViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.navigationItem.title = @"扫一扫";
        self.view.backgroundColor = [UIColor clearColor];
        
        //创建readerVC
        self.readerVC= [[ZBarReaderViewController alloc] init];
        //设置代理
        self.readerVC.readerDelegate = self;
        //设置支持的扫描方向
        self.readerVC.supportedOrientationsMask = ZBarOrientationMaskAll;
        //设置是否显示底部控制按钮
        self.readerVC.showsZBarControls = NO;
        //设置扫描有效区域
       // self.readerVC.scanCrop= CGRectMake(0.1,32/(ScreenHeight - 64), 0.8, (ScreenHeight/2)/(ScreenHeight - 64));
        [self.readerVC.scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
        //必须将其添加到self.view上
        [self.view addSubview:self.readerVC.view];
        
        NSLog(@"self.view %@, self.readerVC.view %@, self.readVC.readerView %@",NSStringFromCGRect(self.view.frame), NSStringFromCGRect(self.readerVC.view.frame), NSStringFromCGRect(self.readerVC.readerView.frame));
        
        //设置覆盖物
        [self setOverlayPickerView:self.readerVC];
        //设置背景图
        [self setScanBackground:self.readerVC];
        //添加底部的相册与闪光灯
        [self setCameraAndtorchView:self.readerVC];
        
        //创建定时器,使扫描线上下移动
        self.timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(movePick_line) userInfo:nil repeats:YES];
    }
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        if (self.readerVC) {
            [self.readerVC.readerView start];
        }
    }
    - (void)viewWillDisappear:(BOOL)animated{
        if (self.readerVC) {
            [self.readerVC.readerView stop];
        }
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    #pragma mark - setOverlayPickerView
    - (void)setOverlayPickerView:(ZBarReaderViewController *)reader
    {
        
        //最上部view
        UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 32)];
        upView.alpha = 0.3;
        upView.backgroundColor = [UIColor blackColor];
        [reader.view addSubview:upView];
        
        //左侧的view
        UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 32 , ScreenWidth/10, ScreenHeight / 2)];
        leftView.alpha = 0.3;
        leftView.backgroundColor = [UIColor blackColor];
        [reader.view addSubview:leftView];
        
        
        //右侧的view
        UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - ScreenWidth/10, 32, ScreenWidth * 0.8, ScreenHeight/2)];
        rightView.alpha = 0.3;
        rightView.backgroundColor = [UIColor blackColor];
        [reader.view addSubview:rightView];
        
        
        //底部view
        UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight/2 + 32, ScreenWidth, 60)];
        downView.alpha = 0.3;
        downView.backgroundColor = [UIColor blackColor];
        [reader.view addSubview:downView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/10, 20, ScreenWidth * 0.8, 21)];
        titleLabel.text = @"将二维码/条码放入框内,即可自动扫描";
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [downView addSubview:titleLabel];
    }
    #pragma mark - setScanBackground
    - (void)setScanBackground:(ZBarReaderViewController *)reader{
        UIImageView *pick_bgImgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/10, 32, ScreenWidth * 0.8, ScreenHeight/2)];
        pick_bgImgView.image = [UIImage imageNamed:@"pick_bg"];
        [reader.view addSubview:pick_bgImgView];
        UIImageView *pick_lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/10 + 5, 32, ScreenWidth * 0.8 - 10, 6)];
        pick_lineImgView.image = [UIImage imageNamed:@"pick_line"];
        pick_lineImgView.tag = 101;
        [reader.view addSubview:pick_lineImgView];
    }
    #pragma mark - 底部的相册以及闪光灯
    - (void)setCameraAndtorchView:(ZBarReaderViewController *)reader{
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight *0.5 +32 +60, ScreenWidth, ScreenHeight *0.5 - 92 - 64)];
        view.alpha = 0.8;
        view.backgroundColor = [UIColor blackColor];
        [reader.view addSubview:view];
    }
    #pragma mark - movePick_line
    - (void)movePick_line{
        UIImageView *pick_lineImgView = (UIImageView *)[self.readerVC.view viewWithTag:101];
        
        if (pick_lineImgView.frame.origin.y < 32 + ScreenHeight/2 - 6) {
            CGFloat y = pick_lineImgView.frame.origin.y +  2;
            pick_lineImgView.frame = CGRectMake(ScreenWidth/10 + 5, y , ScreenWidth *0.8 - 10, 6) ;
        }
            
        if (pick_lineImgView.frame.origin.y == 32 + ScreenHeight/2 - 6) {
            self.num = 0;
            pick_lineImgView.frame = CGRectMake(ScreenWidth/10 + 5, 32, ScreenWidth * 0.8 -10, 6);
        }
    }
    #pragma mark - ZBarReaderDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //     id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
    //    
    //    ZBarSymbol *symbol = nil;
    //    for(symbol in results)
    //        break;
    //    
    //    NSString *barcode = symbol.data;
    //    NSLog(@"barcode %@", barcode);
        
        UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
        //初始化
        ZBarReaderController * readerCtl = [ZBarReaderController new];
        //设置代理
        readerCtl.readerDelegate = self;
        
        CGImageRef cgImageRef = image.CGImage;
        ZBarSymbol * symbol = nil;
        id <NSFastEnumeration> results = [readerCtl scanImage:cgImageRef];
        for (symbol in results)
            break;
        
        NSString * result;
        if ([symbol.data canBeConvertedToEncoding:NSShiftJISStringEncoding]){
            result = [NSString stringWithCString:[symbol.data cStringUsingEncoding: NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
        }
        else{
            result = symbol.data;
        }
        NSLog(@"%@",result);
        //发送扫描结果的通知
        [[NSNotificationCenter defaultCenter] postNotificationName:kBarCodeRusult object:self userInfo:@{kBarCodeRusult : result}];
        
        //扫描完毕时,定时器失效,以免耗费内存
        [self.timer invalidate];
         self.timer = nil;
        //停止扫描
        [self.readerVC.readerView stop];
        //扫描界面退出
        [self.navigationController popViewControllerAnimated:YES];
    }
    @end
    
    
    21_417688_ca081746e1abcca.png

    相关文章

      网友评论

          本文标题:iOS中Zbar二维码扫描的使用 2015-04-29

          本文链接:https://www.haomeiwen.com/subject/ttxslctx.html