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
// 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
相关文章推荐
- ios编程--AVCapture编程理解
- iostat 磁盘性能统计
- ios 数组倒序和数组转字符串
- iOS之ipad开发qq空间项目横竖屏幕适配
- ios pch文件创建集锦
- iOS开发日记35-事件处理机制与图像渲染过程
- iOS经典讲解之清除缓存文件
- iOS 界面布局
- iOS-ARC机制
- IOS之导航控制器传值
- 我的iOS开发之路-前言
- iOS Provisioning Profile(Certificate)与Code Signing详解
- iOS navigationBar导航栏底部与self.view的分界线的隐藏
- 浅谈3D Touch 在Xamarin.iOS上的应用
- iOS 键盘
- iOS 整理iOS9适配中出现的坑
- IOS 原生二维码扫描 学习笔记
- iOS 开发 url 整理
- iOS中实现多线程的方法
- IOS开发:xib和storyboard的混用