-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathQRCodeViewController.m
76 lines (65 loc) · 2.32 KB
/
QRCodeViewController.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
//
// Created by clowwindy on 14-2-17.
// Copyright (c) 2014 clowwindy. All rights reserved.
//
#import "QRCodeViewController.h"
@implementation QRCodeViewController {
QRCodeViewControllerReturnBlock returnBlock;
UIView *cameraView;
ZXCapture *capture;
UIButton *cancelButton;
BOOL stopped;
}
- (id)initWithReturnBlock:(QRCodeViewControllerReturnBlock)block {
self = [super init];
if (self) {
returnBlock = block;
stopped = NO;
}
return self;
}
- (void)viewDidLoad {
capture = [[ZXCapture alloc] init];
capture.rotation = 90.0f;
capture.camera = capture.back;
capture.layer.frame = self.view.bounds;
cameraView = [[UIView alloc] initWithFrame:self.view.bounds];
cameraView.autoresizingMask = (UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
cameraView.backgroundColor = [UIColor blackColor];
[cameraView.layer addSublayer:capture.layer];
[self.view addSubview:cameraView];
capture.delegate = self;
#if !TARGET_IPHONE_SIMULATOR
AVCaptureDevice *currentDevice = capture.captureDevice;
if ([currentDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]){
NSError *error = nil;
[currentDevice lockForConfiguration:&error];
if (!error) {
[currentDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
[currentDevice unlockForConfiguration];
}
}
#endif
cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width - 44 - 10, 20, 44, 44)];
[cancelButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cancelButton];
}
- (void)cancel {
[capture stop];
[self dismissModalViewControllerAnimated:YES];
}
- (void)captureResult:(ZXCapture *)_capture result:(ZXResult *)result {
if (result && !stopped) {
stopped = YES;
[capture stop];
[self dismissModalViewControllerAnimated:YES];
returnBlock([result text]);
}
}
@end