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   不胜感激
回复
 
 
posted @ 2015-04-27 15:13  mengxiangtong22  阅读(336)  评论(0编辑  收藏  举报