//首先下載一個文件ZBarSDK,之後導入文件到工程,然後導入框架如下
//託控件如下
//導入頭文件
#import "ZBarSDK.h"
//添加代理
@interface ViewController () <ZBarReaderDelegate, UIAlertViewDelegate>
//拖拽的imageview控件
@property (weak, nonatomic) IBOutlet UIImageView *myImgaeView;
//拖拽的label控件
@property (weak, nonatomic) IBOutlet UILabel *myLabel;
//提示框
@property (nonatomic, strong)UIAlertView *myAlertView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//掃描按鈕方法的實現
- (IBAction)saomiao:(id)sender {
// 彈出掃描界面
ZBarReaderViewController *readerVC = [ZBarReaderViewController new];
readerVC.readerDelegate = self;
//開始掃描圖片
ZBarImageScanner *sanner = readerVC.scanner;
[sanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
//展示圖片
readerVC.showsZBarControls = YES;
[self presentViewController:readerVC animated:YES completion:^{
}];
}
//使用圖片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//獲取結果
id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];
ZBarSymbol *symbol;
//搜尋結果
for (symbol in results) {
break;
}
//設置圖片
self.myImgaeView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
//判斷結果是二維碼還是條形碼
if ([[symbol.data substringToIndex:4] isEqualToString:@"http"]) {
self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:symbol.data delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往", nil];
[self.myAlertView show];
} else {
self.myAlertView = [[UIAlertView alloc] initWithTitle:@"提示" message:symbol.data delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil];
[self.myAlertView show];
}
self.myLabel.text = symbol.data;
}
//前往按鈕方法的實現
- (IBAction)go:(id)sender {
}
在真機中測試掃描二維碼