iOS 原生扫描二维码

//
//  ViewController.m
//  原生二维码扫描
//
//  Created by kesun-macbook-2 on 16/2/18.
//  Copyright © 2016 pilgrim. All rights reserved.
//

#import "ViewController.h"
#import
<AVFoundation/AVFoundation.h>
#import
"DetailViewController.h"

@interface ViewController ()< 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 ViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];
   
   
// 设备
   
self . device = [ AVCaptureDevice defaultDeviceWithMediaType : AVMediaTypeVideo ];
   
   
// 输入流
   
NSError * error = nil ;
   
self . input = [ AVCaptureDeviceInput deviceInputWithDevice : self . device error :&error];
   
if (error) {
       
NSLog ( @" 输入流创建报错 " );
       
return ;
    }
   
   
// 输出流
   
self . output = [[ AVCaptureMetadataOutput alloc ] init ];
    [
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 ];
    }
   
   
self . output . metadataObjectTypes = @[ AVMetadataObjectTypeQRCode ] ;
   
   
//preview
   
self . preview = [ AVCaptureVideoPreviewLayer layerWithSession : self . session ];
   
self . preview . videoGravity = AVLayerVideoGravityResizeAspectFill ;
   
self . preview . frame = CGRectMake ( 0 , 64 , [ UIScreen mainScreen ]. bounds . size . width , [ UIScreen mainScreen ]. bounds . size . height - 64 );
    [
self . view . layer insertSublayer : self . preview atIndex : 0 ];
   
   
//start
    [
self . session startRunning ];
}

- (
void )viewDidAppear:( BOOL )animated
{
   
if ( self . session != nil ) {
       
if ( self . session . isRunning == NO ) {
            [
self . session startRunning ];
        }
    }
    [
super viewDidAppear :animated];
}

#pragma mark AVCaptureMetadataOutputObjectsDelegate

- (
void )captureOutput:( AVCaptureOutput *)captureOutput didOutputMetadataObjects:( NSArray *)metadataObjects fromConnection:( AVCaptureConnection *)connection
{
   
NSString *stringValue;
   
if ([metadataObjects count ] > 0 )
    {
       
// 停止扫描
        [
_session stopRunning ];
       
       
AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex : 0 ];
        stringValue = metadataObject.
stringValue ;
       
NSLog ( @"stringValue:%@" , stringValue);
       
       
DetailViewController * detailVC = [[ DetailViewController alloc ] initWithNibName : @"DetailViewController" bundle : nil ];
        detailVC.
detailURLStr = stringValue;
        [
self . navigationController pushViewController :detailVC animated : YES ];
    }
}

- (
void )didReceiveMemoryWarning {
    [
super didReceiveMemoryWarning ];
   
// Dispose of any resources that can be recreated.
}

@end

参考地址: http://blog.csdn.net/lc_obj/article/details/41549469?utm_source=tuicool&utm_medium=referral

你可能感兴趣的:(ios,扫描,二维码,原生)