http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4300|回复: 5

[综合] 【iOS开发实用框架】扫一扫

[复制链接]
发表于 2019-1-15 11:57:52 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2019-1-15 11:57 编辑

   导读

【iOS开发实用框架】基础教程目录    http://www.sufeinet.com/thread-24348-1-1.html
【iOS开发实用框架】实例目录贴    http://www.sufeinet.com/thread-24654-1-1.html

90B0ED550F848579B3E09FD7708BFD42.png        4E3A5FA4CD65854E3FF09DA74A59CC05.png
   

代码 :
1.HomePageVC.m

[Objective-C] 纯文本查看 复制代码
UIButton *button = [MTools createButtonWithTitle:@"扫一扫" Font:kFont(15) TitleColor:Color_fff];
    button.frame = CGRectMake(50, 100, ScreenWidth - 50*2, 50);
    button.backgroundColor = kColor(orangeColor);
    [button addButtonClickBlock:^(UIButton *btn) {
        SZQRCodeViewController *qr = [SZQRCodeViewController new];
        qr.scanResult = ^(NSString *strResult) {
            self->lbl.text = [NSString stringWithFormat:@"扫码结果:%@",strResult];
        };
        pushToViewController(self, qr, YES);
    }];
    [self.view addSubview:button];
    
    lbl = [MTools creatLblWithFont:kFont(14) TextColor:Color_333];
    lbl.frame = CGRectMake(button.frameX, button.frameBottom + 20, button.frameWidth, 50);
    [self.view addSubview:lbl];


2.SZQRCodeVC.m

[Objective-C] 纯文本查看 复制代码
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kColor(blackColor);
    
    self.title = @"扫一扫";
    
    NSString *mediaType = AVMediaTypeVideo;
    
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有相机权限" message:@"请去设置-隐私-相机中对App授权" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alertController addAction:okAction];
        
        hasCameraRight = NO;
        return;
    }
    hasCameraRight = YES;
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width(30), 100, ScreenWidth - Width(30)*2, 0.8 * self.view.frame.size.width)];
    imageView.image = [UIImage imageNamed:@"contact_scanframe"];
    [self.view addSubview:imageView];
    
    UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(0, imageView.frameBottom + 10, 290, 30)];
    labIntroudction.backgroundColor = [UIColor clearColor];
    labIntroudction.textColor=[UIColor whiteColor];
    labIntroudction.textAlignment = NSTextAlignmentCenter;
    labIntroudction.font = kFont(15);
    labIntroudction.text=@"将取景框对准二维码,即自动扫描";
    [self.view addSubview:labIntroudction];
    
    
    upOrdown = NO;
    num =0;
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(50, 110, 220, 2)];
    _line.image = [UIImage imageNamed:@"line"];
    [self.view addSubview:_line];
    
    [self setupCamera];
}

-(void)animation1
{
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(CGRectGetMinX(_line.frame), 110+2*num, CGRectGetWidth(_line.frame), CGRectGetHeight(_line.frame));
        if (2 * num == CGRectGetHeight(imageView.frame) - 20) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(CGRectGetMinX(_line.frame), 110+2*num, CGRectGetWidth(_line.frame), CGRectGetHeight(_line.frame));
        if (num == 0) {
            upOrdown = NO;
        }
    }
    
}

- (BOOL)navigationShouldPopOnBackButton
{
    [timer invalidate];
    return YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if (hasCameraRight) {
        if (_session && ![_session isRunning]) {
            [_session startRunning];
        }
        timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [timer invalidate];
}

- (void)setupCamera
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 耗时的操作
        // Device
        self->_device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        
        // Input
        self->_input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
        
        // Output
        self->_output = [[AVCaptureMetadataOutput alloc]init];
        //    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        [self->_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
        
        // 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];
        }
        
        // 条码类型 AVMetadataObjectTypeQRCode
        self->_output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
        dispatch_async(dispatch_get_main_queue(), ^{
            // 更新界面
            // Preview
            self->_preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
            self->_preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
            //    _preview.frame =CGRectMake(20,110,280,280);
            self->_preview.frame = self.view.bounds;
            [self.view.layer insertSublayer:self.preview atIndex:0];
            // Start
            [self->_session startRunning];
        });
    });
}

#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    
    NSString *stringValue;
    
    if ([metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
        
        [_session stopRunning];
        [timer invalidate];
        NSLog(@"扫码结果===%@",stringValue);
        _scanResult(stringValue);
        popViewController(self, YES);
        
    }
}

-(NSString *)getParamValueByKey:(NSString *)key Params:(NSArray *)params{
    __block NSString *value;
    [params enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *string = obj;
        if ([string containsString:key]) {
            value = [string substringFromIndex:[key length]];
        }
    }];
    
    return value;
}

附件: 扫一扫.zip (719.43 KB, 下载次数: 0)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-1-15 11:58:49 | 显示全部楼层
我只是路过打酱油的。
发表于 2019-1-15 14:15:23 | 显示全部楼层
我只是路过打酱油的。
发表于 2019-1-15 14:57:11 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-15 15:01:56 | 显示全部楼层
强烈支持楼主ing……
发表于 2019-1-16 08:33:58 | 显示全部楼层
我只是路过打酱油的。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-11-22 09:13

© 2014-2021

快速回复 返回顶部 返回列表