您的位置:首页 > 移动开发 > IOS开发

iOS7二维码的使用

2015-10-19 17:15 519 查看
//

//  ErWeiMaWZK.h

//  ErWeiMa

//

//  Created by psylife on 15/10/19.

//  Copyright © 2015年 psylife. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@protocol ErWeiMaWZKDelegate <NSObject>

- (void)qrCodeComplete:(NSString *)codeString;

- (void)qrCodeError:(NSError *)error;

@end

@interface ErWeiMaWZK :
UIView<AVCaptureMetadataOutputObjectsDelegate>

@property(assign,nonatomic)id<ErWeiMaWZKDelegate> delegate;

@property (strong,nonatomic)AVCaptureDevice *device;

@property (strong,nonatomic)AVCaptureMetadataOutput *output;

@property (strong,nonatomic)AVCaptureDeviceInput *input;

@property (strong,
nonatomic)AVCaptureSession *session;

@property (strong,
nonatomic)AVCaptureVideoPreviewLayer *preview;

@end
----------------------------------------------------

//

//  ErWeiMaWZK.m

//  ErWeiMa

//

//  Created by psylife on 15/10/19.

//  Copyright © 2015年 psylife. All rights reserved.

//

#import "ErWeiMaWZK.h"

@implementation ErWeiMaWZK

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

    [self initUiConfig:rect];

}

- (void)initUiConfig:(CGRect)rect

{

    self.clipsToBounds =
YES;

    [self initUI:rect];

}

- (void)initUI:(CGRect)previewFrame

{

    self.device = [AVCaptureDevice
defaultDeviceWithMediaType:AVMediaTypeVideo];

    

    NSError *error =
nil;

    

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

    

    if (error) {

        

                if ([self.delegate
respondsToSelector:@selector(qrCodeError:)]) {

                    [self.delegate
qrCodeError:error];

                }

        

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

        return;

    }

    

    self.output = [[AVCaptureMetadataOutput
alloc]init];

    

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

    

    

    self.session = [[AVCaptureSession
alloc]init];

    

    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 =
@[AVMetadataObjectTypeEAN13Code,
AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code,
AVMetadataObjectTypeQRCode];

    

    

    self.preview = [AVCaptureVideoPreviewLayer
layerWithSession:self.session];

    

    self.preview.videoGravity =
AVLayerVideoGravityResizeAspectFill;

    

    //    if ([self.session setsupportsVideoOrientation] == YES)

    //    {

    //        [self.session setvideoOrientation:AVCaptureVideoOrientationLandscapeRight| AVCaptureVideoOrientationLandscapeLeft];

    //    }

    if ([self.preview
isOrientationSupported])

    {

        [self.preview
setOrientation:AVCaptureVideoOrientationLandscapeRight];

    }

    self.preview.frame = previewFrame;

    self.preview.cornerRadius = previewFrame.size.width/2.0;

    

    [self.layer
addSublayer:self.preview];

    

    if ([UIScreen
mainScreen].bounds.size.height ==
480)

    {

        [self.session
setSessionPreset:AVCaptureSessionPreset640x480];

    }

    else

    {

        [self.session
setSessionPreset:AVCaptureSessionPresetHigh];

    }

    

    [self.session
startRunning];

}

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

{

    

//    [self.session stopRunning];

//

//    [self.preview removeFromSuperlayer];

    

    NSString *val =
nil;

    if (metadataObjects.count >
0)

    {

        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];

        

        val = obj.stringValue;

        

                if ([self.delegate
respondsToSelector:@selector(qrCodeComplete:)]) {

                    [self.delegate
qrCodeComplete:val];

                }

    }

}

-(void)shju

{

    

    [self.session
beginConfiguration];

    

    

    [self.session
removeInput:self.input];

    

    [self.session
addInput:self.input];

    

    

    [self.session
commitConfiguration];

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: