feat:add scan qr code to iOS
This commit is contained in:
@@ -26,6 +26,7 @@ - (instancetype)initWithPath:(NSString *)filePath {
|
||||
|
||||
- (void)viewDidLoad {
|
||||
self.title = self.filePath;
|
||||
self.view.backgroundColor = [UIColor whiteColor];
|
||||
NSString *path = [[NSBundle mainBundle] bundlePath];
|
||||
NSString *demoPath = [path stringByAppendingPathComponent:@"demo"];
|
||||
NSString *fullPath = [demoPath stringByAppendingPathComponent:self.filePath];
|
||||
|
@@ -2,6 +2,8 @@
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSCameraUsageDescription</key>
|
||||
<string>Scan QR Code</string>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
|
10
iOS/Example/Example/QRScanViewController.h
Normal file
10
iOS/Example/Example/QRScanViewController.h
Normal file
@@ -0,0 +1,10 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/21.
|
||||
// Copyright (c) 2019 pengfei.zhou. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface QRScanViewController : UIViewController
|
||||
@end
|
93
iOS/Example/Example/QRScanViewController.m
Normal file
93
iOS/Example/Example/QRScanViewController.m
Normal file
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/11/21.
|
||||
// Copyright (c) 2019 pengfei.zhou. All rights reserved.
|
||||
//
|
||||
|
||||
#import "QRScanViewController.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
#import "Doric.h"
|
||||
|
||||
@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];
|
||||
self.title = @"扫一扫";
|
||||
[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];
|
||||
NSString *result = qrObject.stringValue;
|
||||
NSLog(@"Scan result is %@", result);
|
||||
[[DoricDriver instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]];
|
||||
showToast([NSString stringWithFormat:@"Connected to %@", result], BOTTOM);
|
||||
[self.navigationController popViewControllerAnimated:NO];
|
||||
}
|
||||
}
|
||||
@end
|
@@ -9,6 +9,7 @@
|
||||
#import "ViewController.h"
|
||||
#import "Doric.h"
|
||||
#import "DemoVC.h"
|
||||
#import "QRScanViewController.h"
|
||||
|
||||
@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
|
||||
@property(nonatomic, copy) NSArray <NSString *> *demoFilePaths;
|
||||
@@ -25,6 +26,9 @@ - (void)viewDidLoad {
|
||||
self.demoFilePaths = [[mgr subpathsAtPath:demoPath] filter:^BOOL(NSString *obj) {
|
||||
return ![obj containsString:@".map"];
|
||||
}];
|
||||
NSMutableArray <NSString *> *tmp = [self.demoFilePaths mutableCopy];
|
||||
[tmp insertObject:@"Dev Kit" atIndex:0];
|
||||
self.demoFilePaths = tmp;
|
||||
[self.view addSubview:[[UITableView new] also:^(UITableView *it) {
|
||||
it.width = self.view.width;
|
||||
it.height = self.view.height;
|
||||
@@ -50,6 +54,10 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
|
||||
}
|
||||
|
||||
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if (indexPath.row == 0) {
|
||||
[self.navigationController pushViewController:[QRScanViewController new] animated:NO];
|
||||
return;
|
||||
}
|
||||
DemoVC *demoVC = [[DemoVC alloc] initWithPath:self.demoFilePaths[(NSUInteger) indexPath.row]];
|
||||
[self.navigationController pushViewController:demoVC animated:NO];
|
||||
}
|
||||
|
Reference in New Issue
Block a user