Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Convert interface orientation to video orientation on iOS (#719)
Browse files Browse the repository at this point in the history
* Add missing framework declarations for iOS

* Convert interface orientation to video orientation on iOS
  • Loading branch information
vially authored and jcesarmobile committed Nov 27, 2018
1 parent 9c200e0 commit 26f07b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 4 additions & 1 deletion plugin.xml
Expand Up @@ -21,6 +21,9 @@
<resource-file src="src/ios/scannerOverlay.xib"/>
<resource-file src="src/ios/CDVBarcodeScanner.bundle"/>
<source-file src="src/ios/CDVBarcodeScanner.mm"/>
<!-- frameworks -->
<framework src="AVFoundation.framework" />
<framework src="AudioToolbox.framework" />
</platform>
<platform name="android">
<source-file src="src/android/com/phonegap/plugins/barcodescanner/BarcodeScanner.java" target-dir="src/com/phonegap/plugins/barcodescanner"/>
Expand Down Expand Up @@ -68,4 +71,4 @@
<runs/>
</js-module>
</platform>
</plugin>
</plugin>
19 changes: 17 additions & 2 deletions src/ios/CDVBarcodeScanner.mm
Expand Up @@ -759,7 +759,7 @@ - (void)loadView {
- (void)viewWillAppear:(BOOL)animated {

// set video orientation to what the camera sees
self.processor.previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation) [[UIApplication sharedApplication] statusBarOrientation];
self.processor.previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[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)
Expand All @@ -774,7 +774,7 @@ - (void)viewDidAppear:(BOOL)animated {
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

if ([previewLayer.connection isVideoOrientationSupported]) {
[previewLayer.connection setVideoOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
previewLayer.connection.videoOrientation = [self interfaceOrientationToVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

[self.view.layer insertSublayer:previewLayer below:[[self.view.layer sublayers] objectAtIndex:0]];
Expand All @@ -785,6 +785,21 @@ - (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}

- (AVCaptureVideoOrientation)interfaceOrientationToVideoOrientation:(UIInterfaceOrientation)orientation {
switch (orientation) {
case UIInterfaceOrientationPortrait:
return AVCaptureVideoOrientationPortrait;
case UIInterfaceOrientationPortraitUpsideDown:
return AVCaptureVideoOrientationPortraitUpsideDown;
case UIInterfaceOrientationLandscapeLeft:
return AVCaptureVideoOrientationLandscapeLeft;
case UIInterfaceOrientationLandscapeRight:
return AVCaptureVideoOrientationLandscapeRight;
default:
return AVCaptureVideoOrientationPortrait;
}
}

//--------------------------------------------------------------------------
- (void)startCapturing {
self.processor.capturing = YES;
Expand Down

0 comments on commit 26f07b0

Please sign in to comment.