diff --git a/iOS/Example/Example.xcodeproj/project.pbxproj b/iOS/Example/Example.xcodeproj/project.pbxproj index 66134c6b..ccd4467c 100644 --- a/iOS/Example/Example.xcodeproj/project.pbxproj +++ b/iOS/Example/Example.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ AAAF5D5802FC6E7E4F481F5D /* libPods-ExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 942158E9B500A4C975FD37F3 /* libPods-ExampleTests.a */; }; B1AF6AE00A9B4675290F5947 /* libPods-ExampleUITests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 64B9242B26FEC4AEC20FF43D /* libPods-ExampleUITests.a */; }; D751D4B065D8D4FA6594B5EE /* DemoVC.m in Sources */ = {isa = PBXBuildFile; fileRef = D751D19E97EF4EDD7588FEBE /* DemoVC.m */; }; + D751D4FCC0A2322211DE3D55 /* QRScanViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D751DA399F1ADB6D34563B5D /* QRScanViewController.m */; }; D9E087DE6FF1DB083B30D9E7 /* libPods-Example.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C043B7BA79339ABA1CF46881 /* libPods-Example.a */; }; E2334AF022E9D2060098A085 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334AEF22E9D2060098A085 /* AppDelegate.m */; }; E2334AF322E9D2060098A085 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E2334AF222E9D2060098A085 /* ViewController.m */; }; @@ -49,7 +50,9 @@ 942158E9B500A4C975FD37F3 /* libPods-ExampleTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ExampleTests.a"; sourceTree = BUILT_PRODUCTS_DIR; }; A5AE090AE3815360EC768C8D /* Pods-Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Example/Pods-Example.release.xcconfig"; sourceTree = ""; }; C043B7BA79339ABA1CF46881 /* libPods-Example.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-Example.a"; sourceTree = BUILT_PRODUCTS_DIR; }; + D751D18AD6496F4A9BE1AB45 /* QRScanViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QRScanViewController.h; sourceTree = ""; }; D751D19E97EF4EDD7588FEBE /* DemoVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DemoVC.m; sourceTree = ""; }; + D751DA399F1ADB6D34563B5D /* QRScanViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QRScanViewController.m; sourceTree = ""; }; D751DDEC114E037231257E64 /* DemoVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DemoVC.h; sourceTree = ""; }; E2334AEB22E9D2060098A085 /* Doric Playground.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Doric Playground.app"; sourceTree = BUILT_PRODUCTS_DIR; }; E2334AEE22E9D2060098A085 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; @@ -158,6 +161,8 @@ E2334AFD22E9D2070098A085 /* main.m */, D751D19E97EF4EDD7588FEBE /* DemoVC.m */, D751DDEC114E037231257E64 /* DemoVC.h */, + D751DA399F1ADB6D34563B5D /* QRScanViewController.m */, + D751D18AD6496F4A9BE1AB45 /* QRScanViewController.h */, ); path = Example; sourceTree = ""; @@ -424,6 +429,7 @@ E2334AFE22E9D2070098A085 /* main.m in Sources */, E2334AF022E9D2060098A085 /* AppDelegate.m in Sources */, D751D4B065D8D4FA6594B5EE /* DemoVC.m in Sources */, + D751D4FCC0A2322211DE3D55 /* QRScanViewController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/iOS/Example/Example/DemoVC.m b/iOS/Example/Example/DemoVC.m index 2c142ac6..38e215e1 100644 --- a/iOS/Example/Example/DemoVC.m +++ b/iOS/Example/Example/DemoVC.m @@ -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]; diff --git a/iOS/Example/Example/Info.plist b/iOS/Example/Example/Info.plist index f3caaf95..dc0608e9 100644 --- a/iOS/Example/Example/Info.plist +++ b/iOS/Example/Example/Info.plist @@ -2,6 +2,8 @@ + NSCameraUsageDescription + Scan QR Code CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable diff --git a/iOS/Example/Example/QRScanViewController.h b/iOS/Example/Example/QRScanViewController.h new file mode 100644 index 00000000..05ffa4e6 --- /dev/null +++ b/iOS/Example/Example/QRScanViewController.h @@ -0,0 +1,10 @@ +// +// Created by pengfei.zhou on 2019/11/21. +// Copyright (c) 2019 pengfei.zhou. All rights reserved. +// + +#import +#import + +@interface QRScanViewController : UIViewController +@end \ No newline at end of file diff --git a/iOS/Example/Example/QRScanViewController.m b/iOS/Example/Example/QRScanViewController.m new file mode 100644 index 00000000..b6baedca --- /dev/null +++ b/iOS/Example/Example/QRScanViewController.m @@ -0,0 +1,93 @@ +// +// Created by pengfei.zhou on 2019/11/21. +// Copyright (c) 2019 pengfei.zhou. All rights reserved. +// + +#import "QRScanViewController.h" +#import +#import "Doric.h" + +@interface QRScanViewController () +@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 \ No newline at end of file diff --git a/iOS/Example/Example/ViewController.m b/iOS/Example/Example/ViewController.m index 27231476..8acce53f 100644 --- a/iOS/Example/Example/ViewController.m +++ b/iOS/Example/Example/ViewController.m @@ -9,6 +9,7 @@ #import "ViewController.h" #import "Doric.h" #import "DemoVC.h" +#import "QRScanViewController.h" @interface ViewController () @property(nonatomic, copy) NSArray *demoFilePaths; @@ -25,6 +26,9 @@ - (void)viewDidLoad { self.demoFilePaths = [[mgr subpathsAtPath:demoPath] filter:^BOOL(NSString *obj) { return ![obj containsString:@".map"]; }]; + NSMutableArray *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]; } diff --git a/iOS/Pod/Classes/Doric.h b/iOS/Pod/Classes/Doric.h index 9a2236dc..21adbf24 100644 --- a/iOS/Pod/Classes/Doric.h +++ b/iOS/Pod/Classes/Doric.h @@ -19,3 +19,4 @@ #import "DoricViewNode.h" #import "DoricRootNode.h" #import "UIView+Doric.h" +#import "DoricUtil.h" \ No newline at end of file