Permalink
Browse files

Merge pull request #839 from shazron/barcodescanner-ios6

[iOS] BarcodeScanner changes for iOS 6, and bug fixes
  • Loading branch information...
2 parents 1f72eed + df7a645 commit 7165f50b6f5d07cf076bc59bf953117116f18be8 @shazron shazron committed Oct 17, 2012
Showing with 71 additions and 13 deletions.
  1. +71 −13 iOS/BarcodeScanner/CDVBarcodeScanner.mm
@@ -16,11 +16,20 @@
//------------------------------------------------------------------------------
#import "zxing-all-in-one.h"
-#ifdef CORDOVA_FRAMEWORK
-#import <CORDOVA/CDVPlugin.h>
-#else
-#import "CDVPlugin.h"
-#endif
+#import <Cordova/CDVPlugin.h>
+
+
+//------------------------------------------------------------------------------
+// Delegate to handle orientation functions
+//
+//------------------------------------------------------------------------------
+@protocol CDVBarcodeScannerOrientationDelegate <NSObject>
+
+- (NSUInteger)supportedInterfaceOrientations;
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
+- (BOOL)shouldAutorotate;
+
+@end
//------------------------------------------------------------------------------
// Adds a shutter button to the UI, and changes the scan from continuous to
@@ -76,11 +85,13 @@ - (void)dumpImage:(UIImage*)image;
//------------------------------------------------------------------------------
// view controller for the ui
//------------------------------------------------------------------------------
-@interface CDVbcsViewController : UIViewController {}
+@interface CDVbcsViewController : UIViewController <CDVBarcodeScannerOrientationDelegate> {}
@property (nonatomic, retain) CDVbcsProcessor* processor;
@property (nonatomic, retain) NSString* alternateXib;
@property (nonatomic) BOOL shutterPressed;
@property (nonatomic, retain) IBOutlet UIView* overlayView;
+// unsafe_unretained is equivalent to assign - used to prevent retain cycles in the property below
+@property (nonatomic, unsafe_unretained) id orientationDelegate;
- (id)initWithProcessor:(CDVbcsProcessor*)processor alternateOverlay:(NSString *)alternateXib;
- (void)startCapturing;
@@ -238,6 +249,8 @@ - (void)scanBarcode {
}
self.viewController = [[[CDVbcsViewController alloc] initWithProcessor: self alternateOverlay:self.alternateXib] autorelease];
+ // here we set the orientation delegate to the MainViewController of the app (orientation controlled in the Project Settings)
+ self.viewController.orientationDelegate = self.plugin.viewController;
// delayed [self openDialog];
[self performSelector:@selector(openDialog) withObject:nil afterDelay:1];
@@ -634,6 +647,17 @@ - (void)loadView {
}
//--------------------------------------------------------------------------
+- (void)viewWillAppear:(BOOL)animated {
+
+ // set video orientation to what the camera sees
+ self.processor.previewLayer.orientation = [[UIApplication sharedApplication] statusBarOrientation];
+
+ // this fixes the bug when the statusbar is landscape, and the preview layer
+ // starts up in portrait (not filling the whole view)
+ self.processor.previewLayer.frame = self.view.bounds;
+}
+
+//--------------------------------------------------------------------------
- (void)viewDidAppear:(BOOL)animated {
[self startCapturing];
@@ -646,13 +670,6 @@ - (void)startCapturing {
}
//--------------------------------------------------------------------------
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- // rotation currently not supported
- if (interfaceOrientation == UIInterfaceOrientationPortrait) return YES;
- return NO;
-}
-
-//--------------------------------------------------------------------------
- (void)shutterButtonPressed {
self.shutterPressed = YES;
}
@@ -800,4 +817,45 @@ - (UIImage*)buildReticleImage {
return result;
}
+#pragma mark CDVBarcodeScannerOrientationDelegate
+
+- (BOOL)shouldAutorotate
+{
+ if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotate)]) {
+ return [self.orientationDelegate shouldAutorotate];
+ }
+
+ return YES;
+}
+
+- (NSUInteger)supportedInterfaceOrientations
+{
+ if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(supportedInterfaceOrientations)]) {
+ return [self.orientationDelegate supportedInterfaceOrientations];
+ }
+
+ return UIInterfaceOrientationMaskPortrait;
+}
+
+- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
+{
+ if ((self.orientationDelegate != nil) && [self.orientationDelegate respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)]) {
+ return [self.orientationDelegate shouldAutorotateToInterfaceOrientation:interfaceOrientation];
+ }
+
+ return YES;
+}
+
+- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
+{
+ [CATransaction begin];
+
+ self.processor.previewLayer.orientation = orientation;
+ [self.processor.previewLayer layoutSublayers];
+ self.processor.previewLayer.frame = self.view.bounds;
+
+ [CATransaction commit];
+ [super willAnimateRotationToInterfaceOrientation:orientation duration:duration];
+}
+
@end

0 comments on commit 7165f50

Please sign in to comment.