iOS如何實現掃描二維碼

iOS如何實現掃描二維碼:

首先導入AVFoundation.framework,CoreMedia.framework,CoreVideo.framework,QuartzCore.framework

本實例採用storyboard拖拽控件的方法,節省代碼:

在.m中:

#import "ViewController.h"

#import "ZBarSDK.h"


@interfaceViewController ()<ZBarReaderDelegate,UIAlertViewDelegate>

@property (strong,nonatomic) IBOutletUIImageView *myimage;

@property (strong,nonatomic) IBOutletUILabel *mylabel;

@property(nonatomic,strong)UIAlertView *myAlert;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

//掃描方法

- (IBAction)saomiao:(id)sender

{

    //彈出掃描界面

    ZBarReaderViewController *readerVC = [ZBarReaderViewControllernew];

    readerVC.readerDelegate =self;

    //掃描圖片

   ZBarImageScanner *sanner = readerVC.scanner;

    [sanner setSymbology:ZBAR_I25config:ZBAR_CFG_ENABLEto:0];

    //展示圖片

    readerVC.showsZBarControls =YES;

   //調用相機進行掃描

    [selfpresentViewController:readerVC animated:YEScompletion:^{

        

    }];

    

}

//當掃描完後,調用的方法.,使用圖片

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //獲取結果

    id<NSFastEnumeration> results = [infoobjectForKey:ZBarReaderControllerResults];

   ZBarSymbol *symbol;

    //搜尋結果

   for (symbol in results) {

       break;

    }

   //在結果中付圖片

    self.myimage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [picker dismissViewControllerAnimated:YEScompletion:^{

        

    }];

    

   //判斷是二維碼還是條形碼

    if ([[symbol.datasubstringToIndex:4]isEqualToString:@"http"]) {

        self.myAlert = [[UIAlertViewalloc]initWithTitle:@"提示"message:symbol.datadelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"前往",nil];

        [self.myAlertshow];

        

    }else {

        self.myAlert = [[UIAlertViewalloc]initWithTitle:@"提示"message:symbol.datadelegate:selfcancelButtonTitle:nilotherButtonTitles:@"確定",nil];

        [self.myAlertshow];

    }

   self.mylabel.text = symbol.data;

    

}

- (IBAction)qianwang:(id)sender

{

    

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章