/
SGQRCodeScanningVC.m
executable file
·120 lines (101 loc) · 4.13 KB
/
SGQRCodeScanningVC.m
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
//
// SGQRCodeScanningVC.m
// SGQRCodeExample
//
// Created by kingsic on 17/3/20.
// Copyright © 2017年 kingsic. All rights reserved.
//
#import "SGQRCodeScanningVC.h"
#import "SGQRCode.h"
#import "ScanSuccessJumpVC.h"
@interface SGQRCodeScanningVC () <SGQRCodeScanManagerDelegate, SGQRCodeAlbumManagerDelegate>
@property (nonatomic, strong) SGQRCodeScanningView *scanningView;
@end
@implementation SGQRCodeScanningVC
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self.scanningView addTimer];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.scanningView removeTimer];
}
- (void)dealloc {
NSLog(@"SGQRCodeScanningVC - dealloc");
[self removeScanningView];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.view.backgroundColor = [UIColor clearColor];
self.automaticallyAdjustsScrollViewInsets = NO;
[self.view addSubview:self.scanningView];
[self setupNavigationBar];
[self setupQRCodeScanning];
}
- (SGQRCodeScanningView *)scanningView {
if (!_scanningView) {
_scanningView = [SGQRCodeScanningView scanningViewWithFrame:self.view.bounds layer:self.view.layer];
}
return _scanningView;
}
- (void)removeScanningView {
[self.scanningView removeTimer];
[self.scanningView removeFromSuperview];
self.scanningView = nil;
}
- (void)setupNavigationBar {
self.navigationItem.title = @"扫一扫";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"相册" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItenAction)];
}
- (void)rightBarButtonItenAction {
SGQRCodeAlbumManager *manager = [SGQRCodeAlbumManager sharedManager];
[manager SG_readQRCodeFromAlbumWithCurrentController:self];
manager.delegate = self;
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
// 栅栏函数
dispatch_barrier_async(queue, ^{
BOOL isPHAuthorization = manager.isPHAuthorization;
if (isPHAuthorization == YES) {
[self removeScanningView];
}
});
}
- (void)setupQRCodeScanning {
SGQRCodeScanManager *manager = [SGQRCodeScanManager sharedManager];
NSArray *arr = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
// AVCaptureSessionPreset1920x1080 推荐使用,对于小型的二维码读取率较高
[manager SG_setupSessionPreset:AVCaptureSessionPreset1920x1080 metadataObjectTypes:arr currentController:self];
manager.delegate = self;
}
#pragma mark - - - SGQRCodeAlbumManagerDelegate
- (void)QRCodeAlbumManagerDidCancelWithImagePickerController:(SGQRCodeAlbumManager *)albumManager {
[self.view addSubview:self.scanningView];
}
- (void)QRCodeAlbumManager:(SGQRCodeAlbumManager *)albumManager didFinishPickingMediaWithResult:(NSString *)result {
if ([result hasPrefix:@"http"]) {
ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
jumpVC.jump_URL = result;
[self.navigationController pushViewController:jumpVC animated:YES];
} else {
ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
jumpVC.jump_bar_code = result;
[self.navigationController pushViewController:jumpVC animated:YES];
}
}
#pragma mark - - - SGQRCodeScanManagerDelegate
- (void)QRCodeScanManager:(SGQRCodeScanManager *)scanManager didOutputMetadataObjects:(NSArray *)metadataObjects {
NSLog(@"metadataObjects - - %@", metadataObjects);
if (metadataObjects != nil && metadataObjects.count > 0) {
[scanManager SG_palySoundName:@"SGQRCode.bundle/sound.caf"];
[scanManager SG_stopRunning];
[scanManager SG_videoPreviewLayerRemoveFromSuperlayer];
AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
jumpVC.jump_URL = [obj stringValue];
[self.navigationController pushViewController:jumpVC animated:YES];
} else {
NSLog(@"暂未识别出扫描的二维码");
}
}
@end