萤石云 iOSSDK,移植跨平台相关工程
JLChen
2021-02-02 9100afbe1805413504840e6957097be638579045
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
//  EZAddByQRCodeViewController.m
//  EZOpenSDKDemo
//
//  Created by DeJohn Dong on 15/10/28.
//  Copyright © 2015年 Ezviz. All rights reserved.
//
 
#import <AVFoundation/AVFoundation.h>
#import "EZAddByQRCodeViewController.h"
#import "EZQRView.h"
#import "DDKit.h"
 
@interface EZAddByQRCodeViewController ()<AVCaptureMetadataOutputObjectsDelegate>{
    AVAuthorizationStatus authStatus;
}
 
@property (strong, nonatomic) AVCaptureDevice *device;
@property (strong, nonatomic) AVCaptureDeviceInput *input;
@property (strong, nonatomic) AVCaptureMetadataOutput *output;
@property (strong, nonatomic) AVCaptureSession *session;
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *preview;
@property (nonatomic, weak) IBOutlet UIImageView *lineImageView;
@property (nonatomic, weak) IBOutlet EZQRView *qrView;
@property (nonatomic, weak) IBOutlet UIButton *torchButton;
@property (nonatomic, weak) IBOutlet UILabel *tipsLabel;
@property (nonatomic, weak) IBOutlet UIActivityIndicatorView *activityView;
 
@end
 
@implementation EZAddByQRCodeViewController
 
- (void)dealloc
{
    
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"ad_scan_qr_title", @"扫描二维码");
    
    authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (authStatus == AVAuthorizationStatusAuthorized)
    {
        [self qrSetup];
    }
    else if (authStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo
                                 completionHandler:^(BOOL granted) {
                                     if (granted)
                                     {
                                         [self qrSetup];
                                         [_session startRunning];
                                     }
                                 }];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert_title", @"提示")
                                                        message:NSLocalizedString(@"ad_allow_camera",@"请在设备的`设置-隐私-相机`中允许访问相机。")
                                                       delegate:self
                                              cancelButtonTitle:NSLocalizedString(@"done",@"确定")
                                              otherButtonTitles:nil];
        [alert show];
    }
    self.qrView.hidden = YES;
}
 
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[GlobalKit shareKit] clearDeviceInfo];
    if (authStatus == AVAuthorizationStatusDenied ||
        authStatus == AVAuthorizationStatusRestricted)
    {
        self.view.backgroundColor = [UIColor whiteColor];
        self.activityView.hidden = YES;
        return;
    }
    [self.activityView startAnimating];
}
 
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    if (authStatus == AVAuthorizationStatusDenied ||
        authStatus == AVAuthorizationStatusRestricted) {
        return;
    }
    [_session startRunning];
    
    _preview.frame = CGRectMake(0, 64, self.qrView.bounds.size.width, self.qrView.bounds.size.height);
    
    //修正扫描区域
    CGFloat screenHeight = self.qrView.frame.size.height;
    CGFloat screenWidth = self.qrView.frame.size.width;
    CGRect cropRect = CGRectMake((screenWidth - 240.0)/2, (screenHeight - 240.0)/3, 240.0, 240.0);
    
    [_output setRectOfInterest:CGRectMake(cropRect.origin.y/screenHeight, cropRect.origin.x/screenWidth, cropRect.size.height/screenHeight, cropRect.size.width/screenWidth)];
    
    [self addLineAnimation];
    [self.activityView stopAnimating];
    self.activityView.hidden = YES;
    self.qrView.hidden = NO;
}
 
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 
/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
 
#pragma mark - AVCaptureMetadataOutputObjectsDelegate Methods
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    NSString *stringValue;
    if ([metadataObjects count] > 0)
    {
        //停止扫描
        [_session stopRunning];
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    
    [self checkQRCode:stringValue];
}
 
#pragma mark - Custom Methods
 
- (void)qrSetup
{
    if(!_device)
        _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    if(!_input)
        _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    if(!_output)
        _output = [[AVCaptureMetadataOutput alloc] init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    if(!_session)
        _session = [[AVCaptureSession alloc] init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    AVCaptureConnection *outputConnection = [_output connectionWithMediaType:AVMediaTypeVideo];
    outputConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode];
    
    // Preview
    if(!_preview)
        _preview = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _preview.videoGravity = AVLayerVideoGravityResize;
    _preview.frame = self.view.bounds;
    [self.view.layer insertSublayer:_preview atIndex:0];
    
    _preview.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
}
 
- (void)addLineAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    CGFloat height = [UIScreen mainScreen].bounds.size.height - 64.0;
    animation.fromValue = @(height/3.0 - 240);
    animation.toValue = @(height/3.0 - 80);
    animation.duration = 3.0f;
    animation.repeatCount = HUGE_VALF;
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    [_lineImageView.layer addAnimation:animation forKey:nil];
}
 
- (IBAction)torchButtonClicked:(id)sender
{
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([captureDevice hasTorch]) {
        int nTorchMode = captureDevice.torchMode;
        nTorchMode ++;
        nTorchMode = nTorchMode > 1 ? 0 : nTorchMode;
        
        [captureDevice lockForConfiguration:nil];
        captureDevice.torchMode = nTorchMode;
        [captureDevice unlockForConfiguration];
        
        switch (nTorchMode)
        {
            case 0:
            {
                _torchButton.selected = NO;
            }
                break;
            case 1:
            {
                _torchButton.selected = YES;
            }
                break;
            default:
                break;
        }
    }
}
 
- (IBAction)nextAction:(id)sender
{
    [self performSegueWithIdentifier:@"go2InputSerial" sender:nil];
}
 
- (void)checkQRCode:(NSString *)strQRcode
{
    NSArray *arrString = [strQRcode componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
    
    if(arrString.count >=3)
    {
        [GlobalKit shareKit].deviceSerialNo = arrString[1];
        [GlobalKit shareKit].deviceVerifyCode = arrString[2];
        [GlobalKit shareKit].deviceModel = arrString[3];
        [self performSegueWithIdentifier:@"go2Result" sender:self];
    } else {
        [UIView dd_showMessage:NSLocalizedString(@"ad_not_support_scan", @"不支持的二维码类型,转用手动输入")];
        [self nextAction:nil];
    }
}
 
@end