From 2b9861cf5411a3617de4fc9be445b0c84ee8f028 Mon Sep 17 00:00:00 2001 From: Sergio Fraile Carmena Date: Fri, 21 May 2021 11:53:21 +0100 Subject: [PATCH 1/2] Updating objc-c sample app --- examples/BarcodeScanner/AppDelegate.m | 3 - .../BarcodeScanner/BarcodeScanner-Info.plist | 4 + .../BarcodeScanner.xcodeproj/project.pbxproj | 24 +- examples/BarcodeScanner/Default-568h@2x.png | Bin 18594 -> 0 bytes .../BarcodeScanner/LaunchScreen.storyboard | 31 ++ examples/BarcodeScanner/Main.storyboard | 56 ++++ examples/BarcodeScanner/ViewController.m | 314 +++++++++--------- .../en.lproj/ViewController.xib | 53 --- 8 files changed, 259 insertions(+), 226 deletions(-) delete mode 100644 examples/BarcodeScanner/Default-568h@2x.png create mode 100644 examples/BarcodeScanner/LaunchScreen.storyboard create mode 100644 examples/BarcodeScanner/Main.storyboard delete mode 100644 examples/BarcodeScanner/en.lproj/ViewController.xib diff --git a/examples/BarcodeScanner/AppDelegate.m b/examples/BarcodeScanner/AppDelegate.m index e17d53afd..498c7f127 100644 --- a/examples/BarcodeScanner/AppDelegate.m +++ b/examples/BarcodeScanner/AppDelegate.m @@ -20,9 +20,6 @@ @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - self.window.rootViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; - [self.window makeKeyAndVisible]; return YES; } diff --git a/examples/BarcodeScanner/BarcodeScanner-Info.plist b/examples/BarcodeScanner/BarcodeScanner-Info.plist index 132b5f405..9022ce328 100644 --- a/examples/BarcodeScanner/BarcodeScanner-Info.plist +++ b/examples/BarcodeScanner/BarcodeScanner-Info.plist @@ -2,6 +2,10 @@ + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main CFBundleDevelopmentRegion en CFBundleDisplayName diff --git a/examples/BarcodeScanner/BarcodeScanner.xcodeproj/project.pbxproj b/examples/BarcodeScanner/BarcodeScanner.xcodeproj/project.pbxproj index e62a3e743..a21308f7e 100644 --- a/examples/BarcodeScanner/BarcodeScanner.xcodeproj/project.pbxproj +++ b/examples/BarcodeScanner/BarcodeScanner.xcodeproj/project.pbxproj @@ -18,11 +18,11 @@ 02C6AFD7156FCB330052B145 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 02C6AFD6156FCB330052B145 /* main.m */; }; 02C6AFDA156FCB4F0052B145 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 02C6AFD9156FCB4F0052B145 /* ViewController.m */; }; 02C6AFDD156FCB660052B145 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 02C6AFDC156FCB660052B145 /* AppDelegate.m */; }; - 02C6AFE0156FCBA00052B145 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 02C6AFDE156FCBA00052B145 /* ViewController.xib */; }; 02C6AFE3156FCBA80052B145 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 02C6AFE1156FCBA80052B145 /* InfoPlist.strings */; }; - 25404718166AC1D200E13304 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 25404717166AC1D200E13304 /* Default-568h@2x.png */; }; 25404737166AC2EA00E13304 /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 25404736166AC2EA00E13304 /* AVFoundation.framework */; }; 25FFF6A61839839400C2E985 /* libZXingObjC-iOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 25FFF69B1839837F00C2E985 /* libZXingObjC-iOS.a */; }; + 52D7D9D52657C33700EBEE2A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 52D7D9D42657C33700EBEE2A /* Main.storyboard */; }; + 52D7D9D72657C4A200EBEE2A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 52D7D9D62657C4A200EBEE2A /* LaunchScreen.storyboard */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -94,11 +94,11 @@ 02C6AFD9156FCB4F0052B145 /* ViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = SOURCE_ROOT; }; 02C6AFDB156FCB660052B145 /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = SOURCE_ROOT; }; 02C6AFDC156FCB660052B145 /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = SOURCE_ROOT; }; - 02C6AFDF156FCBA00052B145 /* en */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = en; path = en.lproj/ViewController.xib; sourceTree = SOURCE_ROOT; }; 02C6AFE2156FCBA80052B145 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = SOURCE_ROOT; }; - 25404717166AC1D200E13304 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = SOURCE_ROOT; }; 25404736166AC2EA00E13304 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; }; 25FFF6921839837E00C2E985 /* ZXingObjC.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = ZXingObjC.xcodeproj; path = ../../ZXingObjC.xcodeproj; sourceTree = ""; }; + 52D7D9D42657C33700EBEE2A /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = SOURCE_ROOT; }; + 52D7D9D62657C4A200EBEE2A /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -163,8 +163,8 @@ 02C6AFDC156FCB660052B145 /* AppDelegate.m */, 02C6AFD8156FCB4F0052B145 /* ViewController.h */, 02C6AFD9156FCB4F0052B145 /* ViewController.m */, - 02C6AFDE156FCBA00052B145 /* ViewController.xib */, - 25404717166AC1D200E13304 /* Default-568h@2x.png */, + 52D7D9D42657C33700EBEE2A /* Main.storyboard */, + 52D7D9D62657C4A200EBEE2A /* LaunchScreen.storyboard */, 024D10E6156EAB6F00FE6872 /* Supporting Files */, ); path = BarcodeScanner; @@ -297,9 +297,9 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - 02C6AFE0156FCBA00052B145 /* ViewController.xib in Resources */, 02C6AFE3156FCBA80052B145 /* InfoPlist.strings in Resources */, - 25404718166AC1D200E13304 /* Default-568h@2x.png in Resources */, + 52D7D9D72657C4A200EBEE2A /* LaunchScreen.storyboard in Resources */, + 52D7D9D52657C33700EBEE2A /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -327,14 +327,6 @@ /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ - 02C6AFDE156FCBA00052B145 /* ViewController.xib */ = { - isa = PBXVariantGroup; - children = ( - 02C6AFDF156FCBA00052B145 /* en */, - ); - name = ViewController.xib; - sourceTree = ""; - }; 02C6AFE1156FCBA80052B145 /* InfoPlist.strings */ = { isa = PBXVariantGroup; children = ( diff --git a/examples/BarcodeScanner/Default-568h@2x.png b/examples/BarcodeScanner/Default-568h@2x.png deleted file mode 100644 index 0891b7aabfcf3422423b109c8beed2bab838c607..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 18594 zcmeI4X;f257Jx&9fS`ixvS;&$x8J@slQFSel)6zJN=?13FB7H(lQjRkSy8x_-S~tvu2gzn1oS+dLcF#eqtq$ z%tf9TTvX?`)R@}3uBI;jzS-=ZR-Td&MHaS&;!0?Ni*#$#`n*~CcQK)Q9vAQ~TUpnI!j)a2biYK^R)M~A5wUDZhx?ULMX z3x1P&qt=trOY6P2U67L=m=U?F|5#Uj(eCueNTZaHs_ceWiHeET+j+tp3Jt9g(ekqP z2WOvfR{qV+9r+o4J5?qK>7;;^+I7tGv-i)es$X_D=EoKF+S?zsyj^oRFElP}c}JT< zd8SUs-?O?}2YD#ngKbnHgzHBcboxK_2r9l(?eNCl-pEzkJm}fY?WC*jnS?VBE4EpY zO$fEejz6fU;W2Kl>JeQBZBl-%Irg`obSlg*@4QB;Dd1H7^Oi5wvt4d{RZ!8Og?^aE z)k0$1g+V3fd(gdQ3d&q2q-FL*uy#}|bc^=VhFsl0jBgUGJ+-s3U8MK9A!YJJMxpci z5hJ%|{DwV48fZn0{n5l$N_KcSb#NKE4plB`9I6Zt=Z!~-zw0{9tg$L&Ju1F0X)Cy8 zKF;(&lJ>x)Jw(=;p~sF(Sd9VWGwFE2rnyS9!f^DZ8+aCLq zQ};>lcJ1GDLqjm6Hd>|Eabno@P`~Bn(~6^aD_#yoEH(a?Nm1S<;S+hSxI5d16^<1lEM3NPFi zkqPrpL)+ zgnseFikg`gJVBha1&7C4;O6>h=dt~`ND+;Zd?W(4v2JIb7Pt>Td42%M-Ju-XAH#Pns762L}K3 zDhvsRqN0Ni(1UrishD2YvV?4*h2iFj$+&N||Fn$4n|^NSU+o?~jq`0jVQt8T9l{7b zXiwwODFh2V!Q6sqP9S>WH$oOf$N~=d0-bqTlD61!=`&0eAP-F>XN?*|gtOXX{ zQVTWyYo4ZK0GAw!GHf|pz9`D;-bbb*5LBX*{bnz|+)$@&P9|ORM2o?95{;ejvo&r- zq8cBhTN6nn)7~W>54U)%-F_-b?YKdfk5I8MHcuzBD5)!;yv#Z&R&^y=@=>VTIMy#r zX&U<=BsPkdqcMe<_}2+>H%XKyrr5ZR8_KVe>ZqYN z^=^~TFD};;rHJ$U;{~w^hYojl4hRI@SH$^K{YEo=sg)WY87r!*7blQK&qnpDo0`Vn zkl)9u9g=mCh&ZCJS(L4yN3k0kQ zuvg$h2KEEk51T+O0JQ+r0`R>g{jvqM0Mr6d3qUOZwE!?PI7HY@CE|dr sfw?Q;rAv?G4&^^8-z_>&sWXMxvD*gPOU4CBe-*@OtE+wfmVJNyHv)PfH~;_u diff --git a/examples/BarcodeScanner/LaunchScreen.storyboard b/examples/BarcodeScanner/LaunchScreen.storyboard new file mode 100644 index 000000000..0b905fb5a --- /dev/null +++ b/examples/BarcodeScanner/LaunchScreen.storyboard @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/BarcodeScanner/Main.storyboard b/examples/BarcodeScanner/Main.storyboard new file mode 100644 index 000000000..75e29c499 --- /dev/null +++ b/examples/BarcodeScanner/Main.storyboard @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/examples/BarcodeScanner/ViewController.m b/examples/BarcodeScanner/ViewController.m index 582df65b5..06c4ed3f5 100644 --- a/examples/BarcodeScanner/ViewController.m +++ b/examples/BarcodeScanner/ViewController.m @@ -20,101 +20,107 @@ @interface ViewController () @property (nonatomic, strong) ZXCapture *capture; -@property (nonatomic, weak) IBOutlet UIView *scanRectView; -@property (nonatomic, weak) IBOutlet UILabel *decodedLabel; -@property (nonatomic) BOOL scanning; +@property (nonatomic, weak) IBOutlet UIView *scanView; +@property (nonatomic, weak) IBOutlet UILabel *resultLabel; +@property (nonatomic) BOOL isScanning; @property (nonatomic) BOOL isFirstApplyOrientation; @end @implementation ViewController { - CGAffineTransform _captureSizeTransform; + CGAffineTransform _captureSizeTransform; } #pragma mark - View Controller Methods - (void)dealloc { - [self.capture.layer removeFromSuperlayer]; + [self.capture.layer removeFromSuperlayer]; } - (void)viewDidLoad { - [super viewDidLoad]; - - self.capture = [[ZXCapture alloc] init]; - self.capture.sessionPreset = AVCaptureSessionPreset1920x1080; - self.capture.camera = self.capture.back; - self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus; - self.capture.delegate = self; - - self.scanning = NO; - - [self.view.layer addSublayer:self.capture.layer]; - - [self.view bringSubviewToFront:self.scanRectView]; - [self.view bringSubviewToFront:self.decodedLabel]; - - // [self.capture setLuminance: TRUE]; - // [self.capture.luminance setFrame: CGRectMake(150, 30, 100, 100)]; - // [self.view.layer addSublayer: self.capture.luminance]; - - // [self.capture enableHeuristic]; + [super viewDidLoad]; + [self setup]; } - (void)viewWillAppear:(BOOL)animated { - [super viewWillAppear:animated]; + [super viewWillAppear:animated]; } - (void)viewDidLayoutSubviews { - [super viewDidLayoutSubviews]; - - if (_isFirstApplyOrientation) return; - _isFirstApplyOrientation = TRUE; - [self applyOrientation]; + [super viewDidLayoutSubviews]; + + if (_isFirstApplyOrientation) return; + _isFirstApplyOrientation = TRUE; + [self applyOrientation]; } - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id )coordinator { - [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; - [coordinator animateAlongsideTransition:^(id context) { - } completion:^(id context) - { - [self applyOrientation]; - }]; + [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; + [coordinator animateAlongsideTransition:^(id context) { + } completion:^(id context) + { + [self applyOrientation]; + }]; } #pragma mark - Private +- (void)setup { + self.capture = [[ZXCapture alloc] init]; + self.capture.sessionPreset = AVCaptureSessionPreset1920x1080; + self.capture.camera = self.capture.back; + self.capture.focusMode = AVCaptureFocusModeContinuousAutoFocus; + self.capture.delegate = self; + + self.isScanning = NO; + + [self.view.layer addSublayer:self.capture.layer]; + + [self.view bringSubviewToFront:self.scanView]; + [self.view bringSubviewToFront:self.resultLabel]; + + // [self.capture setLuminance: TRUE]; + // [self.capture.luminance setFrame: CGRectMake(150, 30, 100, 100)]; + // [self.view.layer addSublayer: self.capture.luminance]; + + // [self.capture enableHeuristic]; +} + - (void)applyOrientation { - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - float scanRectRotation; - float captureRotation; - - switch (orientation) { - case UIInterfaceOrientationPortrait: - captureRotation = 0; - scanRectRotation = 90; - break; - case UIInterfaceOrientationLandscapeLeft: - captureRotation = 90; - scanRectRotation = 180; - break; - case UIInterfaceOrientationLandscapeRight: - captureRotation = 270; - scanRectRotation = 0; - break; - case UIInterfaceOrientationPortraitUpsideDown: - captureRotation = 180; - scanRectRotation = 270; - break; - default: - captureRotation = 0; - scanRectRotation = 90; - break; - } - self.capture.layer.frame = self.view.frame; - CGAffineTransform transform = CGAffineTransformMakeRotation((CGFloat) (captureRotation / 180 * M_PI)); - [self.capture setTransform:transform]; - [self.capture setRotation:scanRectRotation]; - - [self applyRectOfInterest:orientation]; + UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; + float scanRectRotation; + float captureRotation; + + switch (orientation) { + case UIInterfaceOrientationPortrait: + captureRotation = 0; + scanRectRotation = 90; + break; + case UIInterfaceOrientationLandscapeLeft: + captureRotation = 90; + scanRectRotation = 180; + break; + case UIInterfaceOrientationLandscapeRight: + captureRotation = 270; + scanRectRotation = 0; + break; + case UIInterfaceOrientationPortraitUpsideDown: + captureRotation = 180; + scanRectRotation = 270; + break; + default: + captureRotation = 0; + scanRectRotation = 90; + break; + } + + [self applyRectOfInterest:orientation]; + + CGFloat angleRadius = captureRotation / 180 * M_PI; + CGAffineTransform transform = CGAffineTransformMakeRotation(angleRadius); + + [self.capture setTransform:transform]; + [self.capture setRotation:scanRectRotation]; + self.capture.layer.frame = self.view.frame; } - (void)applyRectOfInterest:(UIInterfaceOrientation)orientation { @@ -153,99 +159,99 @@ - (void)applyRectOfInterest:(UIInterfaceOrientation)orientation { #pragma mark - Private Methods - (NSString *)barcodeFormatToString:(ZXBarcodeFormat)format { - switch (format) { - case kBarcodeFormatAztec: - return @"Aztec"; - - case kBarcodeFormatCodabar: - return @"CODABAR"; - - case kBarcodeFormatCode39: - return @"Code 39"; - - case kBarcodeFormatCode93: - return @"Code 93"; - - case kBarcodeFormatCode128: - return @"Code 128"; - - case kBarcodeFormatDataMatrix: - return @"Data Matrix"; - - case kBarcodeFormatEan8: - return @"EAN-8"; - - case kBarcodeFormatEan13: - return @"EAN-13"; - - case kBarcodeFormatITF: - return @"ITF"; - - case kBarcodeFormatPDF417: - return @"PDF417"; - - case kBarcodeFormatQRCode: - return @"QR Code"; - - case kBarcodeFormatRSS14: - return @"RSS 14"; - - case kBarcodeFormatRSSExpanded: - return @"RSS Expanded"; - - case kBarcodeFormatUPCA: - return @"UPCA"; - - case kBarcodeFormatUPCE: - return @"UPCE"; - - case kBarcodeFormatUPCEANExtension: - return @"UPC/EAN extension"; - - default: - return @"Unknown"; - } + switch (format) { + case kBarcodeFormatAztec: + return @"Aztec"; + + case kBarcodeFormatCodabar: + return @"CODABAR"; + + case kBarcodeFormatCode39: + return @"Code 39"; + + case kBarcodeFormatCode93: + return @"Code 93"; + + case kBarcodeFormatCode128: + return @"Code 128"; + + case kBarcodeFormatDataMatrix: + return @"Data Matrix"; + + case kBarcodeFormatEan8: + return @"EAN-8"; + + case kBarcodeFormatEan13: + return @"EAN-13"; + + case kBarcodeFormatITF: + return @"ITF"; + + case kBarcodeFormatPDF417: + return @"PDF417"; + + case kBarcodeFormatQRCode: + return @"QR Code"; + + case kBarcodeFormatRSS14: + return @"RSS 14"; + + case kBarcodeFormatRSSExpanded: + return @"RSS Expanded"; + + case kBarcodeFormatUPCA: + return @"UPCA"; + + case kBarcodeFormatUPCE: + return @"UPCE"; + + case kBarcodeFormatUPCEANExtension: + return @"UPC/EAN extension"; + + default: + return @"Unknown"; + } } #pragma mark - ZXCaptureDelegate Methods - (void)captureCameraIsReady:(ZXCapture *)capture { - self.scanning = YES; + self.isScanning = YES; } - (void)captureResult:(ZXCapture *)capture result:(ZXResult *)result { - if (!self.scanning) return; - if (!result) return; - - // We got a result. - [self.capture stop]; - self.scanning = NO; - - // Display found barcode location - CGAffineTransform inverse = CGAffineTransformInvert(_captureSizeTransform); - NSMutableArray *points = [[NSMutableArray alloc] init]; - NSString *location = @""; - for (ZXResultPoint *resultPoint in result.resultPoints) { - CGPoint cgPoint = CGPointMake(resultPoint.x, resultPoint.y); - CGPoint transformedPoint = CGPointApplyAffineTransform(cgPoint, inverse); - transformedPoint = [self.scanRectView convertPoint:transformedPoint toView:self.scanRectView.window]; - NSValue* windowPointValue = [NSValue valueWithCGPoint:transformedPoint]; - location = [NSString stringWithFormat:@"%@ (%f, %f)", location, transformedPoint.x, transformedPoint.y]; - [points addObject:windowPointValue]; - } - - // Display information about the result onscreen. - NSString *formatString = [self barcodeFormatToString:result.barcodeFormat]; - NSString *display = [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@\nLocation: %@", formatString, result.text, location]; - [self.decodedLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES]; - - // Vibrate - AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); - - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ - self.scanning = YES; - [self.capture start]; - }); + if (!self.isScanning) return; + if (!result) return; + + // We got a result. + [self.capture stop]; + self.isScanning = NO; + + // Display found barcode location + CGAffineTransform inverse = CGAffineTransformInvert(_captureSizeTransform); + NSMutableArray *points = [[NSMutableArray alloc] init]; + NSString *location = @""; + for (ZXResultPoint *resultPoint in result.resultPoints) { + CGPoint cgPoint = CGPointMake(resultPoint.x, resultPoint.y); + CGPoint transformedPoint = CGPointApplyAffineTransform(cgPoint, inverse); + transformedPoint = [self.scanView convertPoint:transformedPoint toView:self.scanView.window]; + NSValue* windowPointValue = [NSValue valueWithCGPoint:transformedPoint]; + location = [NSString stringWithFormat:@"%@ (%f, %f)", location, transformedPoint.x, transformedPoint.y]; + [points addObject:windowPointValue]; + } + + // Display information about the result onscreen. + NSString *formatString = [self barcodeFormatToString:result.barcodeFormat]; + NSString *display = [NSString stringWithFormat:@"Scanned!\n\nFormat: %@\n\nContents:\n%@\nLocation: %@", formatString, result.text, location]; + [self.resultLabel performSelectorOnMainThread:@selector(setText:) withObject:display waitUntilDone:YES]; + + // Vibrate + AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ + self.isScanning = YES; + [self.capture start]; + }); } @end diff --git a/examples/BarcodeScanner/en.lproj/ViewController.xib b/examples/BarcodeScanner/en.lproj/ViewController.xib deleted file mode 100644 index 81996b9d5..000000000 --- a/examples/BarcodeScanner/en.lproj/ViewController.xib +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 486c7d412bed166a37c72c0e92501bbabb44fb2a Mon Sep 17 00:00:00 2001 From: Sergio Fraile Carmena Date: Fri, 21 May 2021 11:46:11 +0100 Subject: [PATCH 2/2] Fixing applyRectOfInterest method to work with different screen sizes --- examples/BarcodeScanner/ViewController.m | 41 +++++--------- .../BarcodeScannerSwift/ViewController.swift | 54 +++++++------------ 2 files changed, 30 insertions(+), 65 deletions(-) diff --git a/examples/BarcodeScanner/ViewController.m b/examples/BarcodeScanner/ViewController.m index 06c4ed3f5..de3b067e2 100644 --- a/examples/BarcodeScanner/ViewController.m +++ b/examples/BarcodeScanner/ViewController.m @@ -124,36 +124,19 @@ - (void)applyOrientation { } - (void)applyRectOfInterest:(UIInterfaceOrientation)orientation { - CGFloat scaleVideoX, scaleVideoY; - CGFloat videoSizeX, videoSizeY; - CGRect transformedVideoRect = self.scanRectView.frame; - if([self.capture.sessionPreset isEqualToString:AVCaptureSessionPreset1920x1080]) { - videoSizeX = 1080; - videoSizeY = 1920; - } else { - videoSizeX = 720; - videoSizeY = 1280; - } - if(UIInterfaceOrientationIsPortrait(orientation)) { - scaleVideoX = self.capture.layer.frame.size.width / videoSizeX; - scaleVideoY = self.capture.layer.frame.size.height / videoSizeY; - - // Convert CGPoint under portrait mode to map with orientation of image - // because the image will be cropped before rotate - // reference: https://github.com/TheLevelUp/ZXingObjC/issues/222 - CGFloat realX = transformedVideoRect.origin.y; - CGFloat realY = self.capture.layer.frame.size.width - transformedVideoRect.size.width - transformedVideoRect.origin.x; - CGFloat realWidth = transformedVideoRect.size.height; - CGFloat realHeight = transformedVideoRect.size.width; - transformedVideoRect = CGRectMake(realX, realY, realWidth, realHeight); + CGRect transformedScanRect; + if (UIInterfaceOrientationIsLandscape(orientation)) { + transformedScanRect = CGRectMake(_scanView.frame.origin.y, + _scanView.frame.origin.x, + _scanView.frame.size.height, + _scanView.frame.size.width); + } else { + transformedScanRect = _scanView.frame; + } - } else { - scaleVideoX = self.capture.layer.frame.size.width / videoSizeY; - scaleVideoY = self.capture.layer.frame.size.height / videoSizeX; - } - - _captureSizeTransform = CGAffineTransformMakeScale(1.0/scaleVideoX, 1.0/scaleVideoY); - self.capture.scanRect = CGRectApplyAffineTransform(transformedVideoRect, _captureSizeTransform); + CGRect metadataOutputRect = [(AVCaptureVideoPreviewLayer *) _capture.layer metadataOutputRectOfInterestForRect:transformedScanRect]; + CGRect rectOfInterest = [_capture.output rectForMetadataOutputRectOfInterest:metadataOutputRect]; + _capture.scanRect = rectOfInterest; } #pragma mark - Private Methods diff --git a/examples/BarcodeScannerSwift/BarcodeScannerSwift/ViewController.swift b/examples/BarcodeScannerSwift/BarcodeScannerSwift/ViewController.swift index e12bc9d36..a1a385b0d 100644 --- a/examples/BarcodeScannerSwift/BarcodeScannerSwift/ViewController.swift +++ b/examples/BarcodeScannerSwift/BarcodeScannerSwift/ViewController.swift @@ -28,7 +28,6 @@ class ViewController: UIViewController { fileprivate var isScanning: Bool? fileprivate var isFirstApplyOrientation: Bool? - fileprivate var captureSizeTransform: CGAffineTransform? // MARK: Life Circles @@ -119,44 +118,27 @@ extension ViewController { } func applyRectOfInterest(orientation: UIInterfaceOrientation) { - guard var transformedVideoRect = scanView?.frame, - let cameraSessionPreset = capture?.sessionPreset - else { return } - - var scaleVideoX, scaleVideoY: CGFloat - var videoHeight, videoWidth: CGFloat - - // Currently support only for 1920x1080 || 1280x720 - if cameraSessionPreset == AVCaptureSession.Preset.hd1920x1080.rawValue { - videoHeight = 1080.0 - videoWidth = 1920.0 - } else { - videoHeight = 720.0 - videoWidth = 1280.0 - } - - if orientation == UIInterfaceOrientation.portrait { - scaleVideoX = self.view.frame.width / videoHeight - scaleVideoY = self.view.frame.height / videoWidth - - // Convert CGPoint under portrait mode to map with orientation of image - // because the image will be cropped before rotate - // reference: https://github.com/TheLevelUp/ZXingObjC/issues/222 - let realX = transformedVideoRect.origin.y; - let realY = self.view.frame.size.width - transformedVideoRect.size.width - transformedVideoRect.origin.x; - let realWidth = transformedVideoRect.size.height; - let realHeight = transformedVideoRect.size.width; - transformedVideoRect = CGRect(x: realX, y: realY, width: realWidth, height: realHeight); - + guard + let capture = capture, + let captureLayer = capture.layer as? AVCaptureVideoPreviewLayer, + let scanRect = scanView?.frame + else { return } + + let transformedScanRect: CGRect + if orientation.isLandscape { + transformedScanRect = CGRect( + x: scanRect.origin.y, + y: scanRect.origin.x, + width: scanRect.size.height, + height: scanRect.size.width + ) } else { - scaleVideoX = self.view.frame.width / videoWidth - scaleVideoY = self.view.frame.height / videoHeight + transformedScanRect = scanRect } - captureSizeTransform = CGAffineTransform(scaleX: 1.0/scaleVideoX, y: 1.0/scaleVideoY) - guard let _captureSizeTransform = captureSizeTransform else { return } - let transformRect = transformedVideoRect.applying(_captureSizeTransform) - capture?.scanRect = transformRect + let metadataOutputRect = captureLayer.metadataOutputRectConverted(fromLayerRect: transformedScanRect) + let rectOfInterest = capture.output.outputRectConverted(fromMetadataOutputRect: metadataOutputRect) + capture.scanRect = rectOfInterest } func barcodeFormatToString(format: ZXBarcodeFormat) -> String {