• iOS 7 二维码


    维码扫描  

    2014-06-13 10:20:29|  分类: iOS|举报|字号 订阅

    下载LOFTER客户端
     
     


    //

    //  TCTosweepScan.m

    //  TongChengSearch

    //

    //  Created by Fighting on 14-6-11.

    //  Copyright (c) 2014 tcsos.com. All rights reserved.

    //

    #import "TCTosweepScan.h"

    #import <AVFoundation/AVFoundation.h>

    @interface TCTosweepScan()<AVCaptureMetadataOutputObjectsDelegate>

    @property (strong, nonatomic)AVCaptureDevice *device;

    @property (strong, nonatomic)AVCaptureDeviceInput *input;

    @property (strong, nonatomic)AVCaptureMetadataOutput *output;

    @property (strong, nonatomic)AVCaptureSession *session;

    @property (strong, nonatomic)AVCaptureVideoPreviewLayer *preview;

    @end

    @implementation TCTosweepScan

    - (instancetype)initConfigWith:(CGRect)frame previewFrame:(CGRect)previewFrame {

        if (self = [super init]) {

            self.frame = frame;

            self.autoresizingMask = YES;

            

            //初始化UI

            [self initUI:previewFrame];

        }

        

        return self;

    }

    - (void)dealloc {

        // 1. 如果扫描完成,停止会话

        [session stopRunning];

        

        // 2. 删除预览图层

        [preview removeFromSuperlayer];

        

        [output setMetadataObjectsDelegate:nil queue:nil];

    }

    //初始化UI

    - (void)initUI:(CGRect)previewFrame {

        // Device

        device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

        

        NSError *error = nil;

        

        // Input

        input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];

        

        if (error) {

            NSLog(@"你手机不支持二维码扫描!");

            return;

        }

        

        // Output

        output = [[AVCaptureMetadataOutput alloc] init];

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

        

        // Session

        session = [[AVCaptureSession alloc] init];

        [session setSessionPreset:AVCaptureSessionPresetHigh];

        

        if ([session canAddInput:input]) {

            [session addInput:input];

        }

        

        if ([session canAddOutput:output]) {

            [session addOutput:output];

        }

        

        // 条码类型

        output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];

        

        // Preview

        preview = [AVCaptureVideoPreviewLayer layerWithSession:session];

        preview.videoGravity = AVLayerVideoGravityResizeAspectFill;

        preview.frame = previewFrame;

        [self.layer addSublayer:preview];

    }

    //启动扫描

    - (void)startScan {

        // Start

        [session startRunning];

    }

    // 此方法是在识别到QRCode,并且完成转换

    // 如果QRCode的内容越大,转换需要的时间就越长

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

        // 1. 如果扫描完成,停止会话

        [session stopRunning];

        

        // 2. 删除预览图层

        [preview removeFromSuperlayer];

        

        NSString *val = nil;

        

        if (metadataObjects.count > 0) {

            AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];

            val = obj.stringValue;

        }

        

        if (delegate && [delegate respondsToSelector:@selector(TCTosweepScanDidEnd:)]) {

            [delegate TCTosweepScanDidEnd:val];

        }

    }

    @synthesize delegate;

    @synthesize device;

    @synthesize input;

    @synthesize output;

    @synthesize session;

    @synthesize preview;

     
     
     
     
     
    你好  如何从相册取出图片  进行识别   在iOS7这个框架下  你可以教我一下吗  我的QQ:2315618550   不胜感激
    回复
     
     
  • 相关阅读:
    shiro实战系列(三)之架构
    shiro实战系列(二)之入门实战续
    ShopNC B2B2C多用户商城2014商业版,带微商城
    开源 SHOPNC B2B2C结算营运版 wap IM客服 API 手机app 短信通知
    PHP5.3下加速器ZendGuardLoader安装 (LNMP/lnmpa)
    XAMPP + Xdebug+Zend Studio
    magento 12 配置安装教程
    C#控件一览表
    GetXamarin.xambe
    新建电子监控点与测速点
  • 原文地址:https://www.cnblogs.com/iOS-mt/p/4460265.html
Copyright © 2020-2023  润新知