扫一扫 两种方式

//
//  ScanViewController.m
//  deji
//
//  Created by 张鹤 on 15/10/20.
//  Copyright © 2015年 angww. All rights reserved.
//

#import "ScanViewController.h"
#import "ZBarSDK.h"
#import "AppDelegate.h"

@interface ScanViewController ()<ZBarReaderDelegate,ZBarReaderViewDelegate>
{

    UIAlertView *_resultView;


}
@end

@implementation ScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self QRscanView];
//    [self QRscan];
}
- (void)QRscanView{

    ZBarReaderView *readView = [[ZBarReaderView alloc] init];
    readView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

    //清除原有控件
    
    for (UIView *temp in readView.subviews) {
        
        for (UIButton *button in [temp subviews]) {
            
            if ([button isKindOfClass:[UIButton class]]) {
                
                [button removeFromSuperview];
                
            }
            
        }
        
        for (UIToolbar *toolbar in [temp subviews]) {
            
            if ([toolbar isKindOfClass:[UIToolbar class]]) {
                
                [toolbar setHidden:YES];
                
                [toolbar removeFromSuperview];
                
            }
            
        }
        
    }
    //画中间的基准线
    
    UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 180, 240, 1)];
    
    line.backgroundColor = [UIColor redColor];
    
    [readView addSubview:line];
    
    
    //最上部view
    
    UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    upView.alpha = 0.3;
    
    upView.backgroundColor = [UIColor blackColor];
    
    [readView addSubview:upView];
   
    
    //左侧的view
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 20, 280)];
    
    leftView.alpha = 0.3;
    
    leftView.backgroundColor = [UIColor blackColor];
    
    [readView addSubview:leftView];
    
    
    //右侧的view
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(300, 40, 20, 280)];
    
    rightView.alpha = 0.3;
    
    rightView.backgroundColor = [UIColor blackColor];
    
    [readView addSubview:rightView];
    
    
    //底部view
    
    UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 320, 320, 568-360)];
    
    downView.alpha = 0.3;
    
    downView.backgroundColor = [UIColor blackColor];
    
    [readView addSubview:downView];
    
    
    //用于说明的label
    
    UILabel * labIntroudction= [[UILabel alloc] init];
    
    labIntroudction.backgroundColor = [UIColor clearColor];
    
    labIntroudction.frame=CGRectMake(15, 20, 290, 50);
    
    labIntroudction.numberOfLines=2;
    
    labIntroudction.textColor=[UIColor whiteColor];
    
    labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
    
    [downView addSubview:labIntroudction];
    
    
    
    
    //用于取消操作的button
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    cancelButton.alpha = 0.4;
    
    //    [cancelButton setFrame:CGRectMake(20, 390, 280, 40)];
    [cancelButton setFrame:CGRectMake(20, 410, 280, 40)];
    
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    
    [cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
    
    [readView addSubview:cancelButton];
    
        
    readView.readerDelegate = self;
    readView.allowsPinchZoom = NO;
    [self.view addSubview:readView];
    
    [readView start];

}


- (void)QRscan

{
    AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
    [appDelegate.tabBarViewMember setHidden:YES];
    
    ZBarReaderViewController *reader = [ZBarReaderViewController new];
    
    reader.readerDelegate = self;
    
    //非全屏
    
    //    reader.wantsFullScreenLayout = NO;
    
    reader.edgesForExtendedLayout = UIRectEdgeBottom;
    
    
    
    //隐藏底部控制按钮
    
    reader.showsZBarControls = NO;
    
    //设置自己定义的界面
    
    [self setOverlayPickerView:reader];
    
    ZBarImageScanner *scanner = reader.scanner;
    
    [scanner setSymbology: ZBAR_I25
     
                   config: ZBAR_CFG_ENABLE
     
                       to: 0];
    
    
    [self.navigationController pushViewController:reader animated:YES];
    
    reader.view.backgroundColor = [UIColor whiteColor];

    UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 10, 18)];
    [button setImage:[UIImage imageNamed:@"Back_Arrow"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backTo) forControlEvents:UIControlEventTouchUpInside];
    reader.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
}
- (void)setOverlayPickerView:(ZBarReaderViewController *)reader

{
    
    //清除原有控件
    
    for (UIView *temp in [reader.view subviews]) {
        
        for (UIButton *button in [temp subviews]) {
            
            if ([button isKindOfClass:[UIButton class]]) {
                
                [button removeFromSuperview];
                
            }
            
        }
        
        for (UIToolbar *toolbar in [temp subviews]) {
            
            if ([toolbar isKindOfClass:[UIToolbar class]]) {
                
                [toolbar setHidden:YES];
                
                [toolbar removeFromSuperview];
                
            }
            
        }
        
    }
    
    
    //画中间的基准线
    
    //    UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)];
    UIView* line = [[UIView alloc] initWithFrame:CGRectMake(40, 180, 240, 1)];
    
    line.backgroundColor = [UIColor redColor];
    
    [reader.view addSubview:line];
    
    
    //最上部view
    
    UIView* upView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    upView.alpha = 0.3;
    
    upView.backgroundColor = [UIColor blackColor];
    
    [reader.view addSubview:upView];
    
    //    //用于说明的label
    //
    //    UILabel * labIntroudction= [[UILabel alloc] init];
    //
    //    labIntroudction.backgroundColor = [UIColor clearColor];
    //
    //    labIntroudction.frame=CGRectMake(15, 20, 290, 50);
    //
    //    labIntroudction.numberOfLines=2;
    //
    //    labIntroudction.textColor=[UIColor whiteColor];
    //
    //    labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
    //
    //    [upView addSubview:labIntroudction];
    //
    
    
    //左侧的view
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, 20, 280)];
    
    leftView.alpha = 0.3;
    
    leftView.backgroundColor = [UIColor blackColor];
    
    [reader.view addSubview:leftView];
    
    
    //右侧的view
    
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(300, 40, 20, 280)];
    
    rightView.alpha = 0.3;
    
    rightView.backgroundColor = [UIColor blackColor];
    
    [reader.view addSubview:rightView];
    
    
    //底部view
    
    UIView * downView = [[UIView alloc] initWithFrame:CGRectMake(0, 320, 320, 568-360)];
    
    downView.alpha = 0.3;
    
    downView.backgroundColor = [UIColor blackColor];
    
    [reader.view addSubview:downView];
    
    
    //用于说明的label
    
    UILabel * labIntroudction= [[UILabel alloc] init];
    
    labIntroudction.backgroundColor = [UIColor clearColor];
    
    labIntroudction.frame=CGRectMake(15, 20, 290, 50);
    
    labIntroudction.numberOfLines=2;
    
    labIntroudction.textColor=[UIColor whiteColor];
    
    labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
    
    [downView addSubview:labIntroudction];
    
    
    
    
    //用于取消操作的button
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    cancelButton.alpha = 0.4;
    
    //    [cancelButton setFrame:CGRectMake(20, 390, 280, 40)];
    [cancelButton setFrame:CGRectMake(20, 410, 280, 40)];
    
    
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    
    [cancelButton.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
    
    [cancelButton addTarget:self action:@selector(dismissOverlayView:)forControlEvents:UIControlEventTouchUpInside];
    
    [reader.view addSubview:cancelButton];
    
}

- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info
{
    id<NSFastEnumeration> results =
    [info objectForKey: ZBarReaderControllerResults];
    
    
    //    NSLog(@"扫一扫 返回结果 %@",results);
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    
    NSLog(@"扫一扫 返回结果 %@",symbol);
    //imageview.image =
    [info objectForKey: UIImagePickerControllerOriginalImage];
    
    //    [reader dismissModalViewControllerAnimated: YES];
    [reader dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    NSString *str =  symbol.data ;
    NSRange range = [str rangeOfString:@"http" options:NSCaseInsensitiveSearch];
    NSLog(@"=======range.location =%ld, range.length=%ld",(unsigned long)range.location,(unsigned long)range.length);
    
    if (range.location == 0) {
        NSRange range1 = [str rangeOfString:MAIN_DOMAIN options:NSCaseInsensitiveSearch];
        NSRange range2 = [str rangeOfString:MAIN_IPADDR options:NSCaseInsensitiveSearch];
        if (range1.location == NSNotFound && range2.location == NSNotFound ) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
        else{
            //            RootWebViewController *vc = [[RootWebViewController alloc] init];
            //            vc.requestUrl=str;
            //            vc.webViewtitle = @"扫一扫";
            //            [self.navigationController pushViewController:vc animated:YES];
        }
    } else {
        _resultView=[[UIAlertView alloc]initWithTitle:@"扫描数据为" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [_resultView show];
    }
}
- (void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image{ // 得到扫描的条码内容
    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
    NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
    if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {
        // 是否QR二维码
    }
    
    
    
    
}


@end

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值