扫描二维码ios7

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@interface ScanBarViewController : UIViewController<AVCaptureMetadataOutputObjectsDelegate>
{
    int num;
    BOOL upOrdown;
    NSTimer * timer;
}
@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;
@property (nonatomic, retain) UIImageView * line;

@end

//
//  ScanBarViewController.m
//  PurchaseTreasure
//
//  Created by  黄怡盛 on 15/1/29.
//  Copyright (c) 2015年 Volley. All rights reserved.
//

#import "ScanBarViewController.h"
#import "CardInfoViewController.h"

@interface ScanBarViewController ()

@end

static BOOL hasExsitTimer = NO;

@implementation ScanBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"扫描二维码";
    self.view.backgroundColor = PLaceHolder;
/*
    float x = 100;
    float width = SCREEN_WIDTH - x * 2;
    float height = 40;
    float y = SCREEN_HEIGHT - height - 20;
    self.view.backgroundColor = [UIColor grayColor];
    UIButton * scanButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [scanButton setTitle:@"取消" forState:UIControlStateNormal];
    scanButton.frame = CGRectMake(x, y, width, height);
    scanButton.titleLabel.font = FONT(20);
    [scanButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:scanButton];
*/
    float x = 15;
    float y = 10;
    float width = SCREEN_WIDTH - x * 2;
    float height = 50;
    UILabel * labIntroudction= [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    labIntroudction.backgroundColor = [UIColor clearColor];
    labIntroudction.numberOfLines=2;
    labIntroudction.textColor=[UIColor whiteColor];
    labIntroudction.text=@"将二维码图像置于矩形方框内,离手机摄像头10CM左右,系统会自动识别。";
    [self.view addSubview:labIntroudction];
    
    x = 10;
    y += height + 10;
    width = SCREEN_WIDTH - x * 2;
    height = width;
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    imageView.image = [UIImage imageNamed:@"pick_bg"];
    [self.view addSubview:imageView];
    
    x = 50;
    y += 10;
    width = SCREEN_WIDTH - x * 2;
    height = 2;
    upOrdown = NO;
    num =0;
    _line = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _line.image = [UIImage imageNamed:@"pickLine"];
    [self.view addSubview:_line];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    hasExsitTimer = YES;

}

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

-(void)dealloc
{
    
}

-(void)animation1
{
    if (upOrdown == NO) {
        num ++;
        _line.frame = CGRectMake(50, 80+2*num, 220, 2);
        if (2*num == 280) {
            upOrdown = YES;
        }
    }
    else {
        num --;
        _line.frame = CGRectMake(50, 80+2*num, 220, 2);
        if (num == 0) {
            upOrdown = NO;
        }
    }
    
}
/*
-(void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        [timer invalidate];
    }];
}
 */
-(void)viewWillAppear:(BOOL)animated
{
    if (!hasExsitTimer) {
        timer = [NSTimer scheduledTimerWithTimeInterval:.02 target:self selector:@selector(animation1) userInfo:nil repeats:YES];
    }
    [self setupCamera];
}
- (void)setupCamera
{
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    _session = [[AVCaptureSession alloc]init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode];
    
    float width = SCREEN_WIDTH - 20 * 2;
    float height = width;
    // Preview
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame =CGRectMake(20,80,width,height);
    [self.view.layer insertSublayer:self.preview atIndex:0];
    
    
    // Start
    [_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];
    [self.preview removeFromSuperlayer];
    hasExsitTimer = NO;
    
    NSData *stringData = [stringValue dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *valueDictionary = [NSJSONSerialization JSONObjectWithData:stringData options:NSJSONReadingMutableContainers error:nil];
    if ([valueDictionary[@"identifier"] isEqualToString:@"weyee"]) {
        CardInfoViewController * cardInfoCtl = [[CardInfoViewController alloc] init];
        cardInfoCtl.type = kControllerType_Keep;
        cardInfoCtl.value = valueDictionary;
        [self.navigationController pushViewController:cardInfoCtl animated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}


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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

posted @ 2015-04-27 10:47  Volley  阅读(123)  评论(0编辑  收藏  举报