Skip to content

Commit

Permalink
Merge pull request #3 from indevizible/master
Browse files Browse the repository at this point in the history
Auto-detect video orientation
  • Loading branch information
Sam Spencer committed Feb 19, 2014
2 parents fb02145 + ecf576d commit ccd3662
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
3 changes: 0 additions & 3 deletions RMScannerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ NS_CLASS_AVAILABLE_IOS(7_0) @interface RMScannerView : UIView <AVCaptureMetadata
@param flashMode The AVCaptureFlashMode which specifies the flash mode, ON, OFF, or AUTO. */
- (void)setDeviceFlash:(AVCaptureFlashMode)flashMode;

// Rotate Camera when device will rotate ,call this from -(void)willRotateToInterfaceOrientation:duration:
-(void)setScannerViewOrientation:(UIInterfaceOrientation)toInterfaceOrientation;

@end

@class RMScannerView;
Expand Down
20 changes: 15 additions & 5 deletions RMScannerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ @implementation RMScannerView

- (void)initialize {
self.captureSession = [[AVCaptureSession alloc] init];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(setScannerViewOrientation:)
name:UIDeviceOrientationDidChangeNotification
object:nil];

}

- (id)initWithCoder:(NSCoder *)aDecoder {
Expand Down Expand Up @@ -76,6 +82,12 @@ - (void)dealloc {
metadataOutput = nil;
videoInput = nil;
previewLayer = nil;

// Remove orient observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIDeviceOrientationDidChangeNotification
object:nil];
}

#pragma mark - Scanner Checks
Expand Down Expand Up @@ -249,9 +261,7 @@ - (void)setupPreviewLayer {
previewLayer.frame = self.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
previewLayer.position = CGPointMake(CGRectGetMidX(self.layer.bounds), CGRectGetMidY(self.layer.bounds));
if ([self.delegate respondsToSelector:@selector(interfaceOrientation)]) {
[[previewLayer connection] setVideoOrientation:((AVCaptureVideoOrientation)[(UIViewController *)self.delegate interfaceOrientation])];
}
[[previewLayer connection] setVideoOrientation:((AVCaptureVideoOrientation)[[UIDevice currentDevice] orientation])];
if ([self.layer.sublayers containsObject:previewLayer] == NO) [self.layer addSublayer:previewLayer];
}

Expand Down Expand Up @@ -441,10 +451,10 @@ - (NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toVie
return [translatedPoints copy];
}

-(void)setScannerViewOrientation:(UIInterfaceOrientation)toInterfaceOrientation
-(void)setScannerViewOrientation:(UIDeviceOrientation)toDeviceOrientation
{
if (previewLayer) {
[[previewLayer connection] setVideoOrientation:(AVCaptureVideoOrientation)toInterfaceOrientation];
[[previewLayer connection] setVideoOrientation:(AVCaptureVideoOrientation)[[UIDevice currentDevice] orientation]];
}
}

Expand Down
5 changes: 0 additions & 5 deletions Scanner App/Scanner App/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,4 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
return UIBarPositionTopAttached;
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[scannerView setScannerViewOrientation:toInterfaceOrientation];
}
@end

0 comments on commit ccd3662

Please sign in to comment.