Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Camera video preview layer should not rotate with orientation change #7

Closed
gitmh opened this issue May 29, 2016 · 5 comments
Closed

Comments

@gitmh
Copy link

gitmh commented May 29, 2016

When you have a landscape cordova app and start scanning the camera is not rotated correctly. The AVCaptureVideoOrientation should change with rotating the device.

@bitjson
Copy link
Contributor

bitjson commented May 31, 2016

Hey @gitmh, thanks for the report! That's something I've not considered. Does the rotation matter when scanning QR codes? (Does it scan differently?) How do you think the plugin should handle device orientation?

@gitmh
Copy link
Author

gitmh commented May 31, 2016

I have a cordova app which is fixed to landscape only. The scanning also works without rotation but it is very annoying to "find" the code if you move left and the preview moves right and vice versa because the video preview is rotated 180° on your screen (up is down and down is up).

I'm not very familiar with Swift so I tried to fix this by adding something like this:

        let app = UIApplication.sharedApplication()
        switch app.statusBarOrientation {
        case .Portrait:
            captureVideoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait;
        case .PortraitUpsideDown:
            captureVideoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown;
        case .LandscapeLeft:
            captureVideoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft;
        case .LandscapeRight:
            captureVideoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight;
        default:
            captureVideoPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight;
        }

I managed to solve the issue with a NSTimer calling an additional function adjustCameraOrientation. It also needs to resize the frame of the UIView and the previewLayer to work properly on rotation. My current code is here: https://www.dropbox.com/s/ll24xadtgrpbolb/QRScanner.swift?dl=0

Maybe a timer is not a proper solution for this problem, sorry I never used Swift before.

@bitjson
Copy link
Contributor

bitjson commented Jun 27, 2016

Hey @gitmh, thanks again for the report. Just wanted to let you know that I'm currently finishing up with the browser implementation (#6), but this is on my radar. I plan to take a swing at this issue once the initial browser implementation is merged.

@bitjson
Copy link
Contributor

bitjson commented Jul 8, 2016

Still looking at this – I added some device orientation controls to the manual tests to help with testing for this issue: d9a11cd#diff-2a8a5fef3397df87ab538f028a5c6b50R96

I'm not very familiar with iOS's layout system – I think the best solution is to disable orientation changes on just the video preview layer (such that the camera always stays oriented with the screen as the device is rotated). I've not been able to get a working solution yet – pull requests welcome!

@bitjson bitjson added bug and removed enhancement labels Jul 8, 2016
@bitjson bitjson changed the title Landscape Camera Orientation Camera video preview layer should not rotate with orientation change Jul 8, 2016
@gitmh
Copy link
Author

gitmh commented Jul 8, 2016

@bitjson I also very unfamiliar with this orientation stuff. The solution I mentioned in the dropbox link with a timer works very well in my app. The problem is that I was unable to register a proper event handler for the orientation change.

@bitjson bitjson changed the title Camera video preview layer should not rotate with orientation change iOS: Camera video preview layer should not rotate with orientation change Jul 26, 2016
@bitjson bitjson added the iOS label Sep 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants