ScanQRController.mm
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
//
// ScanQRController.m
// RTMPiOSDemo
//
// Created by 蓝鲸 on 16/4/1.
// Copyright © 2016年 tencent. All rights reserved.
//
#import "ScanQRController.h"
#import "PlayController.h"
#import "PublishController.h"
static const char *kScanQRCodeQueueName = "ScanQRCodeQueue";
@interface ScanQRController ()<AVCaptureMetadataOutputObjectsDelegate>
{
}
@end
@implementation ScanQRController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_qrResult = NO;
[self startScan];
CGSize size = [[UIScreen mainScreen] bounds].size;
int c_x = size.width/2;
int c_y = size.height/2;
int roi = 160/2;
UIView* top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, c_y - roi)];
top.backgroundColor = [UIColor blackColor];
top.alpha = 0.8;
[self.view addSubview:top];
UIView* left = [[UIView alloc] initWithFrame:CGRectMake(0, c_y - roi, c_x - roi, 2*roi)];
left.backgroundColor = [UIColor blackColor];
left.alpha = 0.8;
[self.view addSubview:left];
UIView* right = [[UIView alloc] initWithFrame:CGRectMake(c_x + roi , c_y-roi, c_x - roi, 2*roi)];
right.backgroundColor = [UIColor blackColor];
right.alpha = 0.8;
[self.view addSubview:right];
UIView* bottom = [[UIView alloc] initWithFrame:CGRectMake(0, c_y + roi, size.width, c_y - roi)];
bottom.backgroundColor = [UIColor blackColor];
bottom.alpha = 0.8;
[self.view addSubview:bottom];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)startScan
{
NSError * error;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([captureDevice lockForConfiguration:nil]) {
//对焦模式,自动对焦
if (captureDevice && [captureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
[captureDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
}
else if(captureDevice && [captureDevice isFocusModeSupported:AVCaptureFocusModeAutoFocus]){
[captureDevice setFocusMode:AVCaptureFocusModeAutoFocus];
}
// 自动白平衡
if (captureDevice && [captureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
captureDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance;
}
else if (captureDevice && [captureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeAutoWhiteBalance]) {
captureDevice.whiteBalanceMode = AVCaptureWhiteBalanceModeAutoWhiteBalance;
}
if (captureDevice && [captureDevice isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
captureDevice.exposureMode = AVCaptureExposureModeContinuousAutoExposure;
}
else if (captureDevice && [captureDevice isExposureModeSupported:AVCaptureExposureModeAutoExpose]) {
captureDevice.exposureMode = AVCaptureExposureModeAutoExpose;
}
[captureDevice unlockForConfiguration];
}
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return ;
}
// 创建会话
_captureSession = [[AVCaptureSession alloc] init];
// 添加输入流
[_captureSession addInput:input];
// 初始化输出流
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
// 添加输出流
[_captureSession addOutput:captureMetadataOutput];
// 创建dispatch queue.
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create(kScanQRCodeQueueName, NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
// 设置元数据类型 AVMetadataObjectTypeQRCode
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
// captureMetadataOutput.rectOfInterest=CGRectMake(0.25,025,0.5, 0.5);
// 创建输出对象
_videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
[_videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[_videoPreviewLayer setFrame:self.view.layer.bounds];
[self.view.layer addSublayer:_videoPreviewLayer];
[_captureSession startRunning];
}
- (void)stopScanQRCode {
[_captureSession stopRunning];
_captureSession = nil;
[_videoPreviewLayer removeFromSuperlayer];
_videoPreviewLayer = nil;
}
- (void)handleScanResult:(NSString *)result
{
[self stopScanQRCode];
if (_qrResult) {
return;
}
_qrResult = YES;
if ([self.pvc isKindOfClass:[PlayController class]]) {
PlayController* vc = (PlayController*)self.pvc;
[vc.txtRtmpUrl setText:result];
}
if ([self.pvc isKindOfClass:[PublishController class]]) {
PublishController* vc = (PublishController*)self.pvc;
[vc.txtRtmpUrl setText:result];
}
[self.navigationController popToViewController:self.pvc animated:NO];
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
if (metadataObjects.count>0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[self performSelectorOnMainThread:@selector(handleScanResult:) withObject:metadataObj.stringValue waitUntilDone:NO];
}
}
}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self stopScanQRCode];
[self.navigationController popViewControllerAnimated:NO];
}
@end