IOS下用Zbar实现摄像头扫描二维码和扫描本地相册二维码

1、首先下载ZbarSDk,这里不再赘余。唯一提醒的一点事找支持arm64的版本的sdk

2、话不多说,直接贴代码:

ScanViewController.h

[objc]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #import "ZBarSDK.h"  
  3. #import <Cordova/CDVPlugin.h>  
  4. #import <Cordova/CDVViewController.h>  
  5.   
  6. @interface ScanViewController : CDVViewController<ZBarReaderDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>  
  7.   
  8. @property (nonatomic,strong) CDVInvokedUrlCommand* command;  
  9. @property (nonatomic,strong) CDVPlugin* scanPlugin;  
  10. @property (nonatomic,assign) int scanType;  
  11. @property (nonatomic,strong) UIImagePickerController* imagePickerController;  
  12.   
  13. - (void) scan2D;  
  14.   
  15. @end  

ScanViewController.m

[objc]  view plain copy
  1. #import "ScanViewController.h"  
  2.   
  3. @interface ScanViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ScanViewController  
  8.   
  9. @synthesize command,scanPlugin,scanType,imagePickerController;  
  10.   
  11. - (void)viewDidLoad {  
  12.     [super viewDidLoad];  
  13. }  
  14.   
  15. - (void)didReceiveMemoryWarning {  
  16.     [super didReceiveMemoryWarning];  
  17. }  
  18.   
  19. - (void) scan2D  
  20. {  
  21.       
  22.     UIAlertView* imageAlertView = [[UIAlertView alloc]initWithTitle:@"扫描方式" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"相机",@"相册选取", nil nil];  
  23.     [imageAlertView show];  
  24. }  
  25.   
  26. /* 
  27.  * 选择框点击事件 
  28.  */  
  29. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex  
  30. {  
  31.     //NSLog(@"buttonIndex is : %li",(long)buttonIndex);  
  32.     switch (buttonIndex) {  
  33.         case 0:{  
  34.               
  35.         }break;  
  36.         case 1:{  
  37.             [self scan2DByCamera];  
  38.             scanType = 1;  
  39.         }break;  
  40.         case 2:{  
  41.             [self scan2DByPhoto];  
  42.             scanType = 2;  
  43.         }break;  
  44.         default:  
  45.             break;  
  46.     }  
  47. }  
  48.   
  49.   
  50. -(void) scan2DByCamera  
  51. {  
  52.     ZBarReaderViewController * reader = [[ZBarReaderViewController alloc] init];  
  53.       
  54.     reader.readerDelegate = self;  
  55.       
  56.     //非全屏  
  57.     reader.wantsFullScreenLayout = NO;  
  58.       
  59.     //隐藏底部控制按钮  
  60.     reader.showsZBarControls = YES;  
  61.       
  62.     //设置自己定义的界面  
  63.     [self setOverlayPickerView:reader];  
  64.       
  65.     ZBarImageScanner * scanner = reader.scanner;  
  66.     [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];  
  67.       
  68.     reader.sourceType = UIImagePickerControllerSourceTypeCamera;  
  69.     reader.showsZBarControls = YES;  
  70.       
  71.     //reader.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;  
  72.       
  73.     [self presentViewController:reader animated:YES completion:nil];  
  74.   
  75. }  
  76.   
  77. -(void) scan2DByPhoto  
  78. {  
  79.     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])  
  80.     {  
  81.         imagePickerController = [[UIImagePickerController alloc]init];  
  82.         imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
  83.         imagePickerController.delegate = self;  
  84.         [self.view addSubview: imagePickerController.view];  
  85.           
  86.     }else{  
  87.         NSLog(@"相册不能用。。。");  
  88.     }  
  89.   
  90. }  
  91.   
  92.   
  93. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
  94. {  
  95.       
  96.     CDVPluginResult* pluginResult = nil;  
  97.       
  98.     if (scanType == 1) {  
  99.         id<NSFastEnumeration> results = [info objectForKey:ZBarReaderControllerResults];  
  100.         ZBarSymbol * symbol;  
  101.         for(symbol in results)  
  102.             break;  
  103.           
  104.         //_imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  105.           
  106.         //NSLog(@"扫描到的二维码:%@",symbol.data);  
  107.           
  108.           
  109.         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];  
  110.   
  111.     }else if(scanType == 2){  
  112.           
  113.         UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
  114.           
  115.         ZBarReaderController* read = [ZBarReaderController new];  
  116.           
  117.         read.readerDelegate = self;  
  118.           
  119.         CGImageRef cgImageRef = image.CGImage;  
  120.           
  121.         ZBarSymbol* symbol = nil;  
  122.           
  123.         for(symbol in [read scanImage:cgImageRef])  
  124.             break;  
  125.           
  126.         pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:symbol.data];  
  127.           
  128.   
  129.     }  
  130.       
  131.     [scanPlugin.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];  
  132.      
  133.     [picker dismissViewControllerAnimated:YES completion:nil];  
  134.     [self.view removeFromSuperview];  
  135.     [self removeFromParentViewController];  
  136.       
  137.     //_label.text = symbol.data;  
  138. }  
  139.   
  140. - (void)setOverlayPickerView:(ZBarReaderViewController *)reader  
  141.   
  142. {  
  143.       
  144.     //清除原有控件  
  145.       
  146.     for (UIView *temp in [reader.view subviews]) {  
  147.           
  148.         for (UIButton *button in [temp subviews]) {  
  149.               
  150.             if ([button isKindOfClass:[UIButton class]]) {  
  151.                   
  152.                 [button removeFromSuperview];  
  153.                   
  154.             }  
  155.               
  156.         }  
  157.           
  158.         for (UIToolbar *toolbar in [temp subviews]) {  
  159.               
  160.             if ([toolbar isKindOfClass:[UIToolbar class]]) {  
  161.                   
  162.                 [toolbar setHidden:YES];  
  163.                   
  164.                 [toolbar removeFromSuperview];  
  165.                   
  166.             }  
  167.               
  168.         }  
  169.           
  170.     }  
  171.       
  172.     //画中间的基准线  
  173.       
  174.     //UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)];  
  175.       
  176.     //line.backgroundColor = [UIColor redColor];  
  177.       
  178.     //[reader.view addSubview:line];  
  179.       
  180.   
  181.       
  182.     //最上部view  
  183.       
  184.     UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0032080)];  
  185.       
  186.     upView.alpha = 0.3;  
  187.       
  188.     upView.backgroundColor = [UIColor blackColor];  
  189.       
  190.     [reader.view addSubview:upView];  
  191.       
  192.     //用于说明的label  
  193.       
  194.     UILabel * labIntroudction= [[UILabel alloc] init];  
  195.       
  196.     labIntroudction.backgroundColor = [UIColor clearColor];  
  197.       
  198.     labIntroudction.frame=CGRectMake(152029050);  
  199.       
  200.     labIntroudction.numberOfLines=2;  
  201.       
  202.     labIntroudction.textColor=[UIColor whiteColor];  
  203.       
  204.     labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";  
  205.       
  206.     [upView addSubview:labIntroudction];  
  207.       
  208.   
  209.       
  210.     //左侧的view  
  211.       
  212.     UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(08020280)];  
  213.       
  214.     leftView.alpha = 0.3;  
  215.       
  216.     leftView.backgroundColor = [UIColor blackColor];  
  217.       
  218.     [reader.view addSubview:leftView];  
  219.   
  220.       
  221.     //右侧的view  
  222.       
  223.     UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(3008020280)];  
  224.       
  225.     rightView.alpha = 0.3;  
  226.       
  227.     rightView.backgroundColor = [UIColor blackColor];  
  228.       
  229.     [reader.view addSubview:rightView];  
  230.       
  231.       
  232.     //底部view  
  233.       
  234.     UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0360320160)];  
  235.       
  236.     downView.alpha = 0.3;  
  237.       
  238.     downView.backgroundColor = [UIColor blackColor];  
  239.       
  240.     [reader.view addSubview:downView];  
  241.       
  242.       
  243.     //用于取消操作的button  
  244.       
  245.     UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  246.       
  247.     cancelButton.alpha = 0.4;  
  248.       
  249.     [cancelButton setFrame:CGRectMake(2039028040)];  
  250.       
  251.     [cancelButton setTitle:@"取消" forState:UIControlStateNormal];  
  252.       
  253.     [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];  
  254.       
  255.     [cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];  
  256.       
  257.     [reader.view addSubview:cancelButton];    
  258.       
  259. }    
  260.   
  261. //取消button方法    
  262.   
  263. - (void)dismissOverlayView:(id)sender{     
  264.       
  265.     [self dismissViewControllerAnimated:YES completion:nil];  
  266.     [self.view removeFromSuperview];  
  267.     [self removeFromParentViewController];  
  268. }  
  269.   
  270. //取消选择相片  
  271. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{  
  272.       
  273.     if (nil != imagePickerController) {  
  274.         [imagePickerController.view removeFromSuperview];  
  275.     }  
  276. }  
  277.   
  278. @end  

你可能感兴趣的:(ios,二维码,相册)