This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-iOS/Devkit/Classes/QRScanViewController.m

118 lines
4.5 KiB
Mathematica
Raw Normal View History

2020-02-25 14:20:27 +08:00
/*
* Copyright [2019] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2019-12-04 13:29:26 +08:00
//
2020-02-25 14:20:27 +08:00
// QRScanViewController.m
// Doric
//
// Created by jingpeng.wang on 2020/2/25.
2019-12-04 13:29:26 +08:00
//
#import "QRScanViewController.h"
#import <AVFoundation/AVFoundation.h>
2019-12-18 13:03:20 +08:00
#import <DoricCore/Doric.h>
2020-02-25 14:20:27 +08:00
#import <DoricDevkit/DoricDev.h>
2019-12-04 13:29:26 +08:00
@interface QRScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
@property(strong, nonatomic) AVCaptureDevice *device;
@property(strong, nonatomic) AVCaptureDeviceInput *input;
@property(strong, nonatomic) AVCaptureMetadataOutput *output;
@property(strong, nonatomic) AVCaptureSession *session;
@property(strong, nonatomic) AVCaptureVideoPreviewLayer *previewLayer;
@property(strong, nonatomic) UIPinchGestureRecognizer *pinchGes;
@property(assign, nonatomic) CGFloat scanRegion_W;
@property(assign, nonatomic) CGFloat initScale;
@end
@implementation QRScanViewController
- (void)viewDidLoad {
[super viewDidLoad];
2021-02-24 16:51:23 +08:00
self.title = @"Scan QRCode";
2019-12-04 13:29:26 +08:00
[self configBasicDevice];
[self configPinchGes];
[self.session startRunning];
}
- (void)configBasicDevice {
self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.input = [[AVCaptureDeviceInput alloc] initWithDevice:self.device error:nil];
self.output = [[AVCaptureMetadataOutput alloc] init];
[self.output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
self.session = [[AVCaptureSession alloc] init];
[self.session setSessionPreset:AVCaptureSessionPresetHigh];
if ([self.session canAddInput:self.input]) {
[self.session addInput:self.input];
}
if ([self.session canAddOutput:self.output]) {
[self.session addOutput:self.output];
}
[self.output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
[self.output setRectOfInterest:CGRectMake(0, 0, 1, 1)];
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
self.previewLayer.frame = CGRectMake(0, 0, self.view.width, self.view.height);
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer addSublayer:self.previewLayer];
}
- (void)configPinchGes {
self.pinchGes = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
[self.view addGestureRecognizer:self.pinchGes];
}
- (void)pinchDetected:(UIPinchGestureRecognizer *)recogniser {
if (!_device) {
return;
}
if (recogniser.state == UIGestureRecognizerStateBegan) {
_initScale = _device.videoZoomFactor;
}
NSError *error = nil;
[_device lockForConfiguration:&error];
if (!error) {
CGFloat zoomFactor;
CGFloat scale = recogniser.scale;
if (scale < 1.0f) {
zoomFactor = self.initScale - pow(self.device.activeFormat.videoMaxZoomFactor, 1.0f - recogniser.scale);
} else {
zoomFactor = self.initScale + pow(self.device.activeFormat.videoMaxZoomFactor, (recogniser.scale - 1.0f) / 2.0f);
}
zoomFactor = MIN(15.0f, zoomFactor);
zoomFactor = MAX(1.0f, zoomFactor);
_device.videoZoomFactor = zoomFactor;
[_device unlockForConfiguration];
}
}
#pragma mark - AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
[self.session stopRunning];
if ([metadataObjects count] >= 1) {
AVMetadataMachineReadableCodeObject *qrObject = [metadataObjects lastObject];
2023-08-11 17:30:32 +08:00
NSString *url = qrObject.stringValue;
NSLog(@"Scan result is %@", url);
if (![url containsString:@":"]) {
url = [NSString stringWithFormat:@"%@:7777", url];
}
if (![url containsString:@"://"]) {
url = [NSString stringWithFormat:@"ws://%@", url];
}
[[DoricDev instance] connectDevKit:url];
ShowToast([NSString stringWithFormat:@"Connected to %@", url], DoricGravityBottom);
2019-12-04 13:29:26 +08:00
[self.navigationController popViewControllerAnimated:NO];
}
}
@end