iOS 二维码 2
来源:互联网 发布:阿里云服务器ip地址 编辑:程序博客网 时间:2024/06/06 16:44
//
// SYQRCodeViewController.h
// SYQRCodeDemo
//
// Created by sunbb on 15-1-9.
// Copyright (c) 2015年 SY. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SYQRCodeViewController :UIViewController
@property (nonatomic,copy) void (^SYQRCodeCancleBlock) (SYQRCodeViewController *);//扫描取消
@property (nonatomic,copy) void (^SYQRCodeSuncessBlock) (SYQRCodeViewController *,NSString *);//扫描结果
@property (nonatomic,copy) void (^SYQRCodeFailBlock) (SYQRCodeViewController *);//扫描失败
@end
//
// SYQRCodeViewController.m
// SYQRCodeDemo
//
// Created by sunbb on 15-1-9.
// Copyright (c) 2015年 SY. All rights reserved.
//
#import "SYQRCodeViewController.h"
#import <AVFoundation/AVFoundation.h>
//设备宽/高/坐标
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width
#define KDeviceHeight [UIScreen mainScreen].bounds.size.height
#define KDeviceFrame [UIScreen mainScreen].bounds
static constfloat kLineMinY = 185;
static constfloat kLineMaxY = 385;
static constfloat kReaderViewWidth =200;
static constfloat kReaderViewHeight =200;
@interface SYQRCodeViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property (nonatomic,strong) AVCaptureSession *qrSession;//回话
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *qrVideoPreviewLayer;//读取
@property (nonatomic,strong) UIImageView *line;//交互线
@property (nonatomic,strong) NSTimer *lineTimer;//交互线控制
@end
@implementation SYQRCodeViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColorwhiteColor];
[selfinitUI];
[selfsetOverlayPickerView];
[selfstartSYQRCodeReading];
[selfinitTitleView];
[selfcreateBackBtn];
}
- (void)dealloc
{
if (_qrSession) {
[_qrSessionstopRunning];
_qrSession =nil;
}
if (_qrVideoPreviewLayer) {
_qrVideoPreviewLayer =nil;
}
if (_line) {
_line =nil;
}
if (_lineTimer)
{
[_lineTimerinvalidate];
_lineTimer =nil;
}
}
- (void)initTitleView
{
UIView *bgView = [[UIViewalloc] initWithFrame:CGRectMake(0,0,kDeviceWidth,64)];
bgView.backgroundColor = [UIColorcolorWithRed:62.0/255green:199.0/255blue:153.0/255alpha:1.0];
[self.viewaddSubview:bgView];
UILabel *titleLab = [[UILabelalloc] initWithFrame:CGRectMake((kDeviceWidth - 40) / 2.0,28, 40,20)];
//scanCropView.image=[UIImage imageNamed:@""];
//titleLab.layer.borderColor = [UIColor greenColor].CGColor;
//titleLab.layer.borderWidth = 2.0;
//titleLab.backgroundColor = [UIColor colorWithRed:62.0/255 green:199.0/255 blue:153.0/255 alpha:1.0];
titleLab.text =@"扫题";
titleLab.shadowColor = [UIColorlightGrayColor];
titleLab.shadowOffset =CGSizeMake(0, -1);
titleLab.font = [UIFontboldSystemFontOfSize:18.0];
titleLab.textColor = [UIColorwhiteColor];
titleLab.textAlignment =NSTextAlignmentCenter;
[self.viewaddSubview:titleLab];
}
- (void)createBackBtn
{
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(20,28, 60,24)];
[btn setImage:[UIImageimageNamed:@"bar_back"]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(cancleSYQRCodeReading)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
- (void)initUI
{
AVCaptureDevice *device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
//摄像头判断
NSError *error =nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInputdeviceInputWithDevice:deviceerror:&error];
if (error)
{
NSLog(@"没有摄像头-%@", error.localizedDescription);
return;
}
//设置输出(Metadata元数据)
AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutputalloc] init];
//设置输出的代理
//使用主线程队列,相应比较同步,使用其他队列,相应不同步,容易让用户产生不好的体验
[output setMetadataObjectsDelegate:selfqueue:dispatch_get_main_queue()];
[output setRectOfInterest:[selfgetReaderViewBoundsWithSize:CGSizeMake(kReaderViewWidth,kReaderViewHeight)]];
//拍摄会话
AVCaptureSession *session = [[AVCaptureSessionalloc] init];
// 读取质量,质量越高,可读取小尺寸的二维码
if ([sessioncanSetSessionPreset:AVCaptureSessionPreset1920x1080])
{
[session setSessionPreset:AVCaptureSessionPreset1920x1080];
}
elseif ([sessioncanSetSessionPreset:AVCaptureSessionPreset1280x720])
{
[session setSessionPreset:AVCaptureSessionPreset1280x720];
}
else
{
[session setSessionPreset:AVCaptureSessionPresetPhoto];
}
if ([sessioncanAddInput:input])
{
[session addInput:input];
}
if ([sessioncanAddOutput:output])
{
[session addOutput:output];
}
//设置输出的格式
//一定要先设置会话的输出为output之后,再指定输出的元数据类型
[output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
//设置预览图层
AVCaptureVideoPreviewLayer *preview = [AVCaptureVideoPreviewLayerlayerWithSession:session];
//设置preview图层的属性
//preview.borderColor = [UIColor redColor].CGColor;
//preview.borderWidth = 1.5;
[preview setVideoGravity:AVLayerVideoGravityResizeAspectFill];
//设置preview图层的大小
preview.frame =self.view.layer.bounds;
//[preview setFrame:CGRectMake(0, 0, kDeviceWidth, KDeviceHeight)];
//将图层添加到视图的图层
[self.view.layerinsertSublayer:preview atIndex:0];
//[self.view.layer addSublayer:preview];
self.qrVideoPreviewLayer = preview;
self.qrSession = session;
}
- (CGRect)getReaderViewBoundsWithSize:(CGSize)asize
{
returnCGRectMake(kLineMinY /KDeviceHeight, ((kDeviceWidth - asize.width) /2.0) / kDeviceWidth, asize.height /KDeviceHeight, asize.width /kDeviceWidth);
}
- (void)setOverlayPickerView
{
//画中间的基准线
_line = [[UIImageViewalloc] initWithFrame:CGRectMake((kDeviceWidth - 300) / 2.0,kLineMinY, 300,12 * 300 /320.0)];
[_linesetImage:[UIImageimageNamed:@"QRCodeLine"]];
[self.viewaddSubview:_line];
//最上部view
UIView* upView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, kDeviceWidth,kLineMinY)];//80
upView.alpha =0.3;
upView.backgroundColor = [UIColorblackColor];
[self.viewaddSubview:upView];
//左侧的view
UIView *leftView = [[UIViewalloc] initWithFrame:CGRectMake(0,kLineMinY, (kDeviceWidth -kReaderViewWidth) / 2.0, kReaderViewHeight)];
leftView.alpha =0.3;
leftView.backgroundColor = [UIColorblackColor];
[self.viewaddSubview:leftView];
//右侧的view
UIView *rightView = [[UIViewalloc] initWithFrame:CGRectMake(kDeviceWidth - CGRectGetMaxX(leftView.frame),kLineMinY, CGRectGetMaxX(leftView.frame),kReaderViewHeight)];
rightView.alpha =0.3;
rightView.backgroundColor = [UIColorblackColor];
[self.viewaddSubview:rightView];
CGFloat space_h =KDeviceHeight - kLineMaxY;
//底部view
UIView *downView = [[UIViewalloc] initWithFrame:CGRectMake(0,kLineMaxY, kDeviceWidth, space_h)];
downView.alpha =0.3;
downView.backgroundColor = [UIColorblackColor];
[self.viewaddSubview:downView];
//四个边角
UIImage *cornerImage = [UIImageimageNamed:@"QRCodeTopLeft"];
//左侧的view
UIImageView *leftView_image = [[UIImageViewalloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame) - cornerImage.size.width /2.0, CGRectGetMaxY(upView.frame) - cornerImage.size.height / 2.0, cornerImage.size.width, cornerImage.size.height)];
leftView_image.image = cornerImage;
[self.viewaddSubview:leftView_image];
cornerImage = [UIImageimageNamed:@"QRCodeTopRight"];
//右侧的view
UIImageView *rightView_image = [[UIImageViewalloc] initWithFrame:CGRectMake(CGRectGetMinX(rightView.frame) - cornerImage.size.width /2.0, CGRectGetMaxY(upView.frame) - cornerImage.size.height / 2.0, cornerImage.size.width, cornerImage.size.height)];
rightView_image.image = cornerImage;
[self.viewaddSubview:rightView_image];
cornerImage = [UIImageimageNamed:@"QRCodebottomLeft"];
//底部view
UIImageView *downView_image = [[UIImageViewalloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame) - cornerImage.size.width /2.0, CGRectGetMinY(downView.frame) - cornerImage.size.height / 2.0, cornerImage.size.width, cornerImage.size.height)];
downView_image.image = cornerImage;
//downView.backgroundColor = [UIColor blackColor];
[self.viewaddSubview:downView_image];
cornerImage = [UIImageimageNamed:@"QRCodebottomRight"];
UIImageView *downViewRight_image = [[UIImageViewalloc] initWithFrame:CGRectMake(CGRectGetMinX(rightView.frame) - cornerImage.size.width /2.0, CGRectGetMinY(downView.frame) - cornerImage.size.height / 2.0, cornerImage.size.width, cornerImage.size.height)];
downViewRight_image.image = cornerImage;
//downView.backgroundColor = [UIColor blackColor];
[self.viewaddSubview:downViewRight_image];
//说明label
UILabel *labIntroudction = [[UILabelalloc] init];
labIntroudction.backgroundColor = [UIColorclearColor];
labIntroudction.frame =CGRectMake(CGRectGetMaxX(leftView.frame),CGRectGetMinY(downView.frame) +25, kReaderViewWidth,20);
labIntroudction.textAlignment =NSTextAlignmentCenter;
labIntroudction.font = [UIFontboldSystemFontOfSize:13.0];
labIntroudction.textColor = [UIColorwhiteColor];
labIntroudction.text =@"将二维码置于框内,即可自动扫描";
[self.viewaddSubview:labIntroudction];
UIView *scanCropView = [[UIViewalloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftView.frame) - 1,kLineMinY,self.view.frame.size.width - 2 * CGRectGetMaxX(leftView.frame) +2, kReaderViewHeight +2)];
scanCropView.layer.borderColor = [UIColorgreenColor].CGColor;
scanCropView.layer.borderWidth =2.0;
[self.viewaddSubview:scanCropView];
}
#pragma mark -
#pragma mark 输出代理方法
//此方法是在识别到QRCode,并且完成转换
//如果QRCode的内容越大,转换需要的时间就越长
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
//扫描结果
if (metadataObjects.count >0)
{
[selfstopSYQRCodeReading];
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
if (obj.stringValue && ![obj.stringValueisEqualToString:@""] && obj.stringValue.length > 0)
{
NSLog(@"---------%@",obj.stringValue);
if ([obj.stringValuecontainsString:@"http"])
{
if (self.SYQRCodeSuncessBlock) {
self.SYQRCodeSuncessBlock(self,obj.stringValue);
}
}
else
{
if (self.SYQRCodeFailBlock) {
self.SYQRCodeFailBlock(self);
}
}
}
else
{
if (self.SYQRCodeFailBlock) {
self.SYQRCodeFailBlock(self);
}
}
}
else
{
if (self.SYQRCodeFailBlock) {
self.SYQRCodeFailBlock(self);
}
}
}
#pragma mark -
#pragma mark 交互事件
- (void)startSYQRCodeReading
{
_lineTimer = [NSTimerscheduledTimerWithTimeInterval:1.0 /20 target:selfselector:@selector(animationLine)userInfo:nilrepeats:YES];
[self.qrSessionstartRunning];
NSLog(@"start reading");
}
- (void)stopSYQRCodeReading
{
if (_lineTimer)
{
[_lineTimerinvalidate];
_lineTimer =nil;
}
[self.qrSessionstopRunning];
NSLog(@"stop reading");
}
//取消扫描
- (void)cancleSYQRCodeReading
{
[selfstopSYQRCodeReading];
if (self.SYQRCodeCancleBlock)
{
self.SYQRCodeCancleBlock(self);
}
NSLog(@"cancle reading");
}
#pragma mark -
#pragma mark 上下滚动交互线
- (void)animationLine
{
__blockCGRect frame = _line.frame;
staticBOOL flag = YES;
if (flag)
{
frame.origin.y =kLineMinY;
flag = NO;
[UIViewanimateWithDuration:1.0 /20 animations:^{
frame.origin.y +=5;
_line.frame = frame;
} completion:nil];
}
else
{
if (_line.frame.origin.y >= kLineMinY)
{
if (_line.frame.origin.y >= kLineMaxY - 12)
{
frame.origin.y =kLineMinY;
_line.frame = frame;
flag = YES;
}
else
{
[UIViewanimateWithDuration:1.0 /20 animations:^{
frame.origin.y +=5;
_line.frame = frame;
} completion:nil];
}
}
else
{
flag = !flag;
}
}
//NSLog(@"_line.frame.origin.y==%f",_line.frame.origin.y);
}
@end
//打开摄像头并扫描
- (IBAction)saomiaoAction:(id)sender
{
//扫描二维码
SYQRCodeViewController *qrcodevc = [[SYQRCodeViewControlleralloc] init];
qrcodevc.SYQRCodeSuncessBlock = ^(SYQRCodeViewController *aqrvc,NSString *qrString){
self.saomiaoLabel.text = qrString;
[aqrvc dismissViewControllerAnimated:NOcompletion:nil];
};
qrcodevc.SYQRCodeFailBlock = ^(SYQRCodeViewController *aqrvc){
self.saomiaoLabel.text =@"fail~";
[aqrvc dismissViewControllerAnimated:NOcompletion:nil];
};
qrcodevc.SYQRCodeCancleBlock = ^(SYQRCodeViewController *aqrvc){
[aqrvc dismissViewControllerAnimated:NOcompletion:nil];
self.saomiaoLabel.text =@"cancle~";
};
[selfpresentViewController:qrcodevcanimated:YEScompletion:nil];
}
- iOS 二维码 2
- ios 二维码
- 二维码 ios
- iOS 二维码
- ios二维码
- ios 二维码
- iOS - 二维码
- iOS 二维码
- iOS二维码
- IOS 二维码
- iOS二维码
- IOS生成二维码、扫二维码
- iOS,二维码识别,保存二维码
- IOS二维码解决方案
- 【iOS开发】---- 二维码
- iOS读取制作二维码
- iOS-二维码扫描
- iOS 二维码,条形码扫描
- POS接口与GE接口区别
- php转换字节数为其他单位
- iOS 多线程篇3—线程安全
- 类似5.0涟漪效果,低版本简陋版
- Android 延迟执行某方法
- iOS 二维码 2
- HTML杂谈
- Java中字符串String的常用分割方法
- Android开发板之串口开发
- 正则表达式
- request.getParameter(“参数名”) 中文乱码解决方法
- Java核心知识点-JVM结构和工作方式
- svn dump 失败后的处理
- 堆排序