关闭

iOS 扫码登录

标签: ios
1415人阅读 评论(0) 收藏 举报
分类:

//

//  CZPreView.h

//  06-二维码扫描

//

//  Created by 0426iOS on 15/7/17.

//  Copyright (c) 2015 0426iOS. All rights reserved.

//


#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@interface CZPreView : UIView


@property (nonatomic,strong)UIImageView *lineImageView;

@property (nonatomic,strong)NSTimer *timer;

@property (nonatomic,strong)AVCaptureSession *session;

@end

    //

//  CZPreView.m

//  06-二维码扫描

//

//  Created by 0426iOS on 15/7/17.

//  Copyright (c) 2015 0426iOS. All rights reserved.

//


#import "CZPreView.h"


@interface CZPreView ()

@property (nonatomic,strong)UIImageView *imageView;


@property(nonatomic,strong)UIView * topView;

@property(nonatomic,strong)UIView * leftView;

@property(nonatomic,strong)UIView * bottomView;


@property(nonatomic,strong)UIView * rightView;


@property(nonatomic,strong)UIView * redView;

@end

@implementation CZPreView

//- (UIView *)redView{

//    if (!_redView) {

//        _redView = [[UIView alloc] initWithFrame:CGRectMake((screenWidth - 240) * 0.5, (screenHeight - 220)* 0.4, 240, 2)];

//        _redView.backgroundColor = [UIColor redColor];

//    }

//    return _redView;

//}


- (UIView *)topView{

    if (!_topView) {

//        (self.bounds.size.width * 0.5 - 140, self.bounds.size.height * 0.5 - 140, 280, 280);

        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT * 0.4 - 100*KWIDTH)];

        _topView.backgroundColor = [UIColor grayColor];

        _topView.alpha = 0.6;

    }

    return _topView;

    

}


- (UIView *)bottomView{

    if (!_bottomView) {

        

        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, IPHONE_HEIGHT * 0.4 - 100*KWIDTH+200*KWIDTH, IPHONE_WIDTH, IPHONE_HEIGHT * 0.6-100*KWIDTH )];

        _bottomView.backgroundColor = [UIColor grayColor];

        _bottomView.alpha = 0.6;

    }

    return _bottomView;

    

}


- (UIView *)leftView{

    if (!_leftView) {

        

        _leftView = [[UIView alloc] initWithFrame:CGRectMake(0, IPHONE_HEIGHT*0.4-100*KWIDTH, IPHONE_WIDTH * 0.5-100*KWIDTH, 200*KWIDTH)];

        _leftView.backgroundColor = [UIColor grayColor];

        _leftView.alpha = 0.6;

    }

    return _leftView;

    

}


- (UIView *)rightView{

    if (!_rightView) {

        

        _rightView = [[UIView alloc] initWithFrame:CGRectMake(IPHONE_WIDTH * 0.5 +100*KWIDTH, IPHONE_HEIGHT * 0.4-100*KWIDTH, IPHONE_WIDTH  * 0.5-100*KWIDTH, 200*KWIDTH)];

        _rightView.backgroundColor = [UIColor grayColor];

        _rightView.alpha = 0.6;

    }

    return _rightView;

    

}


/**

 *  layer的类型

 *

 *  @return AVCaptureVideoPreviewLayer 特殊的layer 可以展示输入设备采集到得信息

 */

+ (Class)layerClass

{

    return [AVCaptureVideoPreviewLayer class];

}


- (void)setSession:(AVCaptureSession *)session

{

    _session = session;

    

    AVCaptureVideoPreviewLayer *layer = (AVCaptureVideoPreviewLayer *)  self.layer;

    layer.session = session;

}


- (instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        [self initUiConfig];

    }

    return self;

}

- (void)initUiConfig


{

    [self addSubview:self.topView];

    [self.layer addSublayer:self.bottomView.layer];

    [self.layer addSublayer:self.leftView.layer];

    [self.layer addSublayer:self.rightView.layer];

//    [self.layer addSublayer:self.redView.layer];


    UILabel *scanLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 400*KWIDTH, IPHONE_WIDTH, 30)];

//    scanLabel.backgroundColor=[UIColor redColor];

    scanLabel.textAlignment=NSTextAlignmentCenter;

    scanLabel.text=@"将二维码图片对准扫描框即可自动扫描";

    scanLabel.textColor=[UIColor blackColor];

    

    [self addSubview:scanLabel];

    //设置背景图片

    _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pick_bg.png"]];

    //设置位置到界面的中间

    _imageView.frame = CGRectMake(IPHONE_WIDTH * 0.5 - 100*KWIDTH, IPHONE_HEIGHT * 0.4 - 100*KWIDTH, 200*KWIDTH, 200*KWIDTH);

    //添加到视图上

    [self addSubview:_imageView];

    

    //初始化二维码的扫描线的位置

    _lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(IPHONE_WIDTH * 0.5 - 150*KWIDTH,IPHONE_HEIGHT * 0.4 - 220*KWIDTH, 180*KWIDTH, 2)];

    _lineImageView.image = [UIImage imageNamed:@"line.png"];

    [_imageView addSubview:_lineImageView];

    


}


@end




//

//  SViewController.h

//  123

//

//  Created by vip on 15/11/26.

//  Copyright © 2015 vip. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "ParentsViewController.h"


@interface SViewController :ParentsViewController


@end









//

//  ViewController.m

//  06-二维码扫描

//

//  Created by 0426iOS on 15/7/17.

//  Copyright (c) 2015 0426iOS. All rights reserved.

//


#import "SViewController.h"

#import <AVFoundation/AVFoundation.h>

#import "CZPreView.h"

@interface SViewController ()<AVCaptureMetadataOutputObjectsDelegate>


//1.输入设备(从外界采集信息输入设备很多种  摄像头  麦克风  键盘

@property (nonatomic,strong)AVCaptureDeviceInput *input;

//2.输出设备 (解析采集来得内容 然后获取到数据) Metadata 元数据

@property (nonatomic,strong)AVCaptureMetadataOutput *output;

//3.会话 session (连接输入和输出进行工作)

@property (nonatomic,strong)AVCaptureSession *session;

//4.layer  (特殊的layer  展示输入设备采集到的信息)

@property (nonatomic,strong)CZPreView *preView;




@end


@implementation SViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self creatTitle];

    [self creatBackItem];

    

    

    


   

         [self sview];

   

//    -------

//    全部状态

//    typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {

//        ALAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application

//        ALAuthorizationStatusRestricted, // This application is not authorized to access photo data.

//        // The user cannot change this application’s status, possibly due to active restrictions

//        // such as parental controls being in place.

//        ALAuthorizationStatusDenied, // User has explicitly denied this application access to photos data.

//        ALAuthorizationStatusAuthorized // User has authorized this application to access photos data.

//    }

    


}

-(void)sview

{

    //1.输入设备(从外界采集信息)

    //创建具体的设备  摄像头

    //AVMediaTypeVideo  摄像头       AVMediaTypeAudio 麦克风

    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    self.input = [AVCaptureDeviceInput deviceInputWithDevice:device error:NULL];

    

    //2.输出设备 (解析采集来得内容 然后获取到数据) Metadata 元数据

    self.output = [[AVCaptureMetadataOutput alloc]init];

    

    

    //3.会话 session (连接输入和输出进行工作)

    

    self.session  = [[AVCaptureSession alloc]init];

    //会话扫描展示的大小

    [self.session setSessionPreset:AVCaptureSessionPresetHigh];

    //添加输入设备和输出设备

    if ([self.session canAddInput:self.input]) {

        [self.session addInput:self.input];

    }

    if ([self.session canAddOutput:self.output]) {

        [self.session addOutput:self.output];

    }

    

    

    //指定输出设备的 代理   来返回 解析到得数据

    [self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    

    //设置元数据类型 QRCode 二维码

    [self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];

    

    

    //4.layer  (特殊的layer  展示输入设备采集到的信息)

    //self.preViewLayer  = [[AVCaptureVideoPreviewLayer alloc]initWithSession:self.session];

    //指定layer 大小

    //self.preViewLayer.frame = self.view.bounds;

    

    //[self.view.layer addSublayer:self.preViewLayer];

    //4.创建特殊视图展示一个二维码界面

    CZPreView *preview = [[CZPreView alloc]initWithFrame:self.view.bounds];

    preview.session = self.session;

    self.preView=preview;

    //开启定时器

   preview.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(animation) userInfo:nil repeats:YES];

    [self.view addSubview:preview];

    //5.开启 会话

    [self.session startRunning];

    

}

- (void)animation

{

    [UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

        

      self.preView.lineImageView.frame = CGRectMake(IPHONE_WIDTH * 0.5 - 150*KWIDTH,IPHONE_HEIGHT * 0.4-38*KWIDTH , 180*KWIDTH, 2);

        

    } completion:^(BOOL finished) {

        

        

        self.preView.lineImageView.frame = CGRectMake(IPHONE_WIDTH * 0.5 - 150*KWIDTH,IPHONE_HEIGHT * 0.4 - 220*KWIDTH, 180*KWIDTH, 2);

    }];

}

-(void)creatTitle

{

    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0 , 100, 44)];

    titleLabel.backgroundColor = [UIColor clearColor];  //设置Label背景透明

    titleLabel.font = [UIFont boldSystemFontOfSize:KTitle];  //设置文本字体与大小

    titleLabel.textColor = [UIColor whiteColor];  //设置文本颜色

    titleLabel.textAlignment = NSTextAlignmentCenter;

    titleLabel.text = @"模拟器扫码"//设置标题

    self.navigationItem.titleView = titleLabel;

}

-(void)creatBackItem{

    //    CGFloat frameY = 0.0f;

    UIBarButtonItem * leftItem = nil;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

        //        frameY = 0.0f;

        leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"title_bar_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(returnClicked)];

    } else {

        //        frameY = 0.0f;

        leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"title_bar_back"] style:UIBarButtonItemStyleDone target:self action:@selector(returnClicked)];

    }

    self.navigationItem.leftBarButtonItem = leftItem;

    

}


-(void)returnClicked {

    

    UIViewController *cvc = [[UIViewController alloc]init];

    cvc.view.tag = 1;

    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:cvc,@"Hidden", nil];

    

    NSNotification *notification = [NSNotification notificationWithName:@"tongzhiHidden" object:nil userInfo:dict];

    [[NSNotificationCenter defaultCenter]postNotification:notification];

    

    

    

    

    

    

    [self.navigationController popViewControllerAnimated:YES];

}


/**

 *  解析出元数据就会调用

 *

 *  @param captureOutput   输出

 *  @param metadataObjects 元数据数组

 *  @param connection      连接

 */

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection

{

    //停止会话

    [self.session stopRunning];

    //移除layer

    //[self.preViewLayer removeFromSuperlayer];

    

    

    

    for (AVMetadataMachineReadableCodeObject * objc  in metadataObjects) {

        NSString *string=[NSString stringWithFormat:@"%@",objc.stringValue];


        DSLog(@"高尔夫登录%@",string);

        if (string.length>=37) {

            NSString *string1=[string substringToIndex:32];

            NSString *string2=[string substringFromIndex:33];

            NSInteger length=[string2 length];

            NSString *string3=[string2 substringToIndex:length-2];

            NSString *string4=[string2 substringFromIndex:length-1];


        

        DSLog(@"%@==%@",string,string1);

        

//        if ([string4 isEqualToString:@"A"]||[string4 isEqualToString:@"B"]||[string4 isEqualToString:@"C"]) {

            

            

            NSString *urlStr= [NSString stringWithFormat:KScanCode];

            AFHTTPRequestOperationManager *registe=[AFHTTPRequestOperationManager manager];

            

            registe.responseSerializer = [AFHTTPResponseSerializer serializer];

            registe.requestSerializer = [AFJSONRequestSerializer serializer];

            

            

            NSMutableDictionary *param = [NSMutableDictionary dictionary];

            

            [param setValue:KmemberId forKey:@"memberId"];

            

            [param setValue:string3 forKey:@"machine"];

            [param setValue:string4 forKey:@"server"];

            

          

            [param setValue:string1 forKey:@"qrCode"];

            

            [param setValue:KAPPID forKey:@"appId"];

            

            [param setValue:KappSecret forKey:@"appSecret"];

            

            [param setValue:[CommonTools getTimeScamp] forKey:@"timestamp"];

            

            

            

            

            //加密规则:sign=md5(app_id + lon+lat + time + key)

            [param setValue:[CommonTools tokenmd5StringFromArray:@[[NSString stringWithFormat:@"memberId%@",param[@"memberId"]],[NSString stringWithFormat:@"machine%@",param[@"machine"]],[NSString stringWithFormat:@"server%@",param[@"server"]],[NSString stringWithFormat:@"qrCode%@",param[@"qrCode"]],[NSString stringWithFormat:@"appId%@",param[@"appId"]],[NSString stringWithFormat:@"appSecret%@",param[@"appSecret"]],[NSString stringWithFormat:@"timestamp%@",param[@"timestamp"]]]] forKey:@"digest"];

            

            

            

            DSLog(@"加密后memberId%@appId%@appSecret%@timestamp%@digest%@",param[@"memberId"],param[@"appId"],param[@"appSecret"],param[@"timestamp"],param[@"digest"]);

            

            

            [registe POST:urlStr parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {

                NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

                NSLog(@"%@",dict);

                if ([dict[@"returnCode"]integerValue]==000000) {


                    DSLog(@"登录成功");

                    //移除视图

                    [self.preView removeFromSuperview];

[UIView animateWithDuration:2.0 animations:^{


    [MBProgressHUD showSuccess:@"登录成功" toView:nil];

    [self returnClicked];

}];

 

                }else

                {

                           DSLog(@"登录失败");

                    [UIView animateWithDuration:2.0 animations:^{

                        [MBProgressHUD showError:@"登录失败" toView:nil];

                        [self.session startRunning];

                }];


                    

             

                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

                NSLog(@"失败");

            }];

            //


            

//        }

            

//            NSLog(@"%@==%@",string,string1);

//            

//            if ([string4 isEqualToString:@"A"]||[string4 isEqualToString:@"B"]||[string4 isEqualToString:@"C"]) {

//                NSString *urlStr= [NSString stringWithFormat:KScanCode];

//                AFHTTPRequestOperationManager *registe=[AFHTTPRequestOperationManager manager];

//                registe.responseSerializer = [AFHTTPResponseSerializer serializer];

//                registe.requestSerializer = [AFJSONRequestSerializer serializer];

//                [registe POST:urlStr parameters:@{@"memberId":[UserModel sharedInstance].memberId,@"server":string4,@"machine":string3,@"qrCode":string1,@"appId":@"ET12412",@"appSecret":@"4124bc0a9335c27f086f24ba207a4912"} success:^(AFHTTPRequestOperation *operation, id responseObject) {

//                    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

//                    NSLog(@"%@",dict);

//                    if ([dict[@"returnCode"]integerValue]==000000) {

//                        //                    [UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{

//                        //

//                        //

//                        //                    } completion:^(BOOL finished) {

//                        //

//                        //

//                        //                    }];

//                        DSLog(@"登录成功");

//                        //移除视图

//                        [self.preView removeFromSuperview];

//                        [UIView animateWithDuration:2.0 animations:^{

//                            

//                            [MBProgressHUD showSuccess:@"登录成功" toView:nil];

//                            [self returnClicked];

//                        }];

//                        

//                    }else

//                    {

//                        DSLog(@"登录失败");

//                        [UIView animateWithDuration:2.0 animations:^{

//                            [MBProgressHUD showError:@"登录失败" toView:nil];

//                            [self.session startRunning];

//                        }];

//                        

//                        

//                        

//                    }

//                } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

//                    NSLog(@"失败");

//                }];

//                //

//                

            

                

                

                

//            }

            

//            

//            else{

//                [UIView animateWithDuration:2.0 animations:^{

//                    [MBProgressHUD showError:@"需要扫描城市高尔夫二维码" toView:nil];

//                    [self.session startRunning];

//                }];

//                

//            }

        

        }else

        {

            [MBProgressHUD showError:@"需要扫描城市高尔夫二维码" toView:nil];

            [self.session startRunning];

           


        }


    

       

        

        

    }

}


@end




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:75783次
    • 积分:1265
    • 等级:
    • 排名:千里之外
    • 原创:49篇
    • 转载:8篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论