Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect device orientation when the Orientation Lock is on and the app only supports Portrait

The issue that I'm running into is that when a user takes a photo with our app, using AVCaptureSession, I have no way of determining whether they took the photo in Portrait or Landscape mode. Our app only supports Portrait and I keep the Orientation Lock on when using my phone so I'm trying to build a solution assuming that others might do the same.

I looked into using [UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications] but when the Orientation Lock is on, no notifications are ever received. I know that this functionality is possible because the base Camera app and the camera in the Google Hangouts app can detect the rotation (animations on the Cancel and Flash buttons are apparent) when my phone has Orientation Lock on.

Is my best bet to use the accelerometer and detect the angle the phone is being rotated to? An old answer, Detect iPhone screen orientation, makes it very obvious that detecting the angle that way is easy to to do (obviously adapting the answer to use Core Motion instead of UIAccelerometer), but I'm curious if there is another way to do it.

like image 693
jer-k Avatar asked May 22 '14 16:05

jer-k


People also ask

How do I rotate my screen when an app doesn't rotate?

Find and turn on the "Auto-rotate" tile in the quick-setting panel. You can also go to Settings > Display > Auto-rotate screen to turn it on. Your phone screen should rotate automatically now if nothing is wrong with the sensors.

How can we detect the orientation of the screen?

You can detect this change in orientation on Android as well as iOS with the following code: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; window.

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

Why is my orientation not working?

For Android users:Swipe your finger down from the top of the screen to bring up your quick settings toolbar. You will have an option for rotation that can be tapped to turn off the rotation lock.


1 Answers

Yes you can do it by looking at the metadata for the image. Don't have time to write up a detailed answer (sorry about that), but I did it for my own project a while back through CMCopyDictionaryOfAttachments(NULL, buffer, kCMAttachmentMode_ShouldPropagate); where I passed in a CMSampleBufferRef for the buffer. I got that buffer from

captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler: ^(CMSampleBufferRef imageDataSampleBuffer, NSError *error){},

but you can get it from

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection as well.

You can find all the keys for that dictionary here.

Did a quick test with the default camera app with the orientation lock on, and I did get a different orientation for the two pictures. 6 for the portrait one, and 3 for the landscape one.

Again, would love to give you more details about this, but I'm sure you can figure it out by looking through the docs.

like image 165
vegather Avatar answered Sep 20 '22 19:09

vegather