1、前往https://github.com/bmorton/ZBarSDK下載SDK 2、添加框架 1. Drag ZBarSDK into your Xcode project. 2. Add these system frameworks to your project: * AVFoundation.framework (weak) * CoreMedia.framework (weak) * CoreVideo.framework (weak) * QuartzCore.framework * libiconv.dylib
3、上代碼git
一、Header.hgithub
#define Width [UIScreen mainScreen].bounds.size.width #define Height [UIScreen mainScreen].bounds.size.height //背景顏色 #define SETBACKCOLOR(a, b, c, alp) [UIColor colorWithRed:(a)/255.0 green:(b)/255.0 blue:(c)/255.0 alpha:(alp)] #define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) #define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) #define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0) #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width) #define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height) #define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT)) #define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT)) #define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0) #define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0) #define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0) #define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
二、QRCodeViewController.m框架
#import "QRCodeViewController.h" #import "ZBarSDK.h" #import "Header.h" @interface QRCodeViewController ()<ZBarReaderViewDelegate,ZBarReaderDelegate,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { ZBarReaderView *readerView; UIImageView * scanZomeBack; UIImageView * readLineView; BOOL is_Anmotion; } @end @implementation QRCodeViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.hidden = YES; is_Anmotion = NO; // Do any additional setup after loading the view from its nib. } -(void)viewWillAppear:(BOOL)animated { readerView =[ZBarReaderView new]; readerView.frame = CGRectMake(0, 0, Width, Height); readerView.readerDelegate =self; readerView.allowsPinchZoom =NO; readerView.showsFPS =NO; readerView.tracksSymbols = NO; readerView.torchMode = 0; [self.view addSubview:readerView]; UIImage *hbImage=[UIImage imageNamed:@"twocode_bg"]; scanZomeBack=[[UIImageView alloc] initWithImage:hbImage]; scanZomeBack.clipsToBounds =YES; CGRect mImagerect; NSLog(@"%f",Width); if (Width==320) { mImagerect=CGRectMake(60, 150, 200, 200); }else { if (IS_IPHONE_6P) { mImagerect=CGRectMake(107, 180, 200, 200); } else { mImagerect=CGRectMake(87, 180, 200, 200); } } [scanZomeBack setFrame:mImagerect]; [readerView addSubview:scanZomeBack]; // readerView.scanCrop=[self getScanCrop:mImagerect readerViewBounds:readerView.bounds]; readerView.scanCrop =CGRectMake(0, 0, 1, 1); [readerView start]; [self loopDrawLine]; //返回 UIButton * goBackButton =[UIButton buttonWithType:UIButtonTypeCustom]; goBackButton.frame =CGRectMake(20, 20, 40, 40); [goBackButton setImage:[UIImage imageNamed:@"erweima_back"] forState:UIControlStateNormal]; [goBackButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; [readerView addSubview:goBackButton]; //去相冊 UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom]; button.frame =CGRectMake(Width-60, 20, 40, 40); [button setImage:[UIImage imageNamed:@"erweima_xiangce"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(xiangce) forControlEvents:UIControlEventTouchUpInside]; [readerView addSubview:button]; //個人二維碼 UILabel * textLabel =[[UILabel alloc]initWithFrame:CGRectMake(scanZomeBack.frame.origin.x, scanZomeBack.frame.origin.y+scanZomeBack.frame.size.height+10, 200, 25)]; textLabel.text =@"將二維碼放入框內,便可自動掃描"; textLabel.font =[UIFont systemFontOfSize:13]; textLabel.textColor =[UIColor whiteColor]; [self.view addSubview:textLabel]; } #pragma mark -- 二維碼掃描動畫 #pragma mark 掃描動畫 -(void)loopDrawLine { CGRect rect = CGRectMake(0, -(scanZomeBack.frame.size.height), (scanZomeBack.frame.size.width), scanZomeBack.frame.size.height); if (readLineView) { [readLineView removeFromSuperview]; } readLineView = [[UIImageView alloc] initWithFrame:rect]; [readLineView setImage:[UIImage imageNamed:@"Qcode_bg"]]; [UIView animateWithDuration:3.0 delay: 0.0 options: UIViewAnimationOptionCurveEaseIn animations:^{ //修改fream的代碼寫在這裏 readLineView.frame =CGRectMake(0, 0, scanZomeBack.frame.size.width, scanZomeBack.frame.size.height); [readLineView setAnimationRepeatCount:0]; } completion:^(BOOL finished){ if (!is_Anmotion) { [self loopDrawLine]; } }]; [scanZomeBack addSubview:readLineView]; } #pragma mark --二維碼回調 - (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image { for (ZBarSymbol *symbol in symbols) { NSLog(@"%@", symbol.data); NSArray * arr =[symbol.data componentsSeparatedByString:@"SMDD://"]; if (arr.count>1) { NSString * contentStr =arr[1]; NSArray * array =[contentStr componentsSeparatedByString:@"||"]; if (array.count>1) { /*返回主頁*/ [[NSNotificationCenter defaultCenter]postNotificationName:@"SKValue" object:nil userInfo:[NSDictionary dictionaryWithObject:contentStr forKey:@"contentStr"]]; [self.navigationController popViewControllerAnimated:YES]; }else { UIAlertView * alter =[[UIAlertView alloc]initWithTitle:@"無效二維碼" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alter show]; } }else { UIAlertView * alter =[[UIAlertView alloc]initWithTitle:@"無效二維碼" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alter show]; } } } #pragma mark -- 調用相冊 -(void)xiangce { ZBarReaderController *reader = [ZBarReaderController new]; reader.allowsEditing = YES; reader.readerDelegate = self; reader.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentViewController:reader animated:YES completion:^{ NSLog(@"跳轉成功---"); }]; } #pragma mark -- 進入相冊回調 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults]; if ([info count]>2) { int quality = 0; ZBarSymbol *bestResult = nil; for(ZBarSymbol *sym in results) { int q = sym.quality; if(quality < q) { quality = q; bestResult = sym; } } [self performSelector: @selector(presentResult:) withObject: bestResult afterDelay: .001]; }else { ZBarSymbol *symbol = nil; for(symbol in results) break; [self performSelector: @selector(presentResult:) withObject: symbol afterDelay: .001]; } [picker dismissViewControllerAnimated:YES completion:^{ }]; } - (void) readerControllerDidFailToRead: (ZBarReaderController*) reader withRetry: (BOOL) retry { [reader popViewControllerAnimated:YES]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:NULL]; } - (void) presentResult: (ZBarSymbol*)sym { if (sym) { NSString *tempStr = sym.data; if ([sym.data canBeConvertedToEncoding:NSShiftJISStringEncoding]) { tempStr = [NSString stringWithCString:[tempStr cStringUsingEncoding:NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding]; } if ([tempStr containsString:@"SMDD:"]) { NSArray * arr =[tempStr componentsSeparatedByString:@"SMDD://"]; if (arr.count>1) { NSString * contentStr =arr[1]; NSArray * array =[contentStr componentsSeparatedByString:@"||"]; if (array.count>1) { [[NSNotificationCenter defaultCenter]postNotificationName:@"SKValue" object:nil userInfo:[NSDictionary dictionaryWithObject:contentStr forKey:@"contentStr"]]; } }else { UIAlertView * alter =[[UIAlertView alloc]initWithTitle:@"無效二維碼" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alter show]; } }else { UIAlertView * alter =[[UIAlertView alloc]initWithTitle:@"無效二維碼" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alter show]; } } } #pragma mark --返回 -(void)goBack { [self.navigationController popViewControllerAnimated:YES]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }