Tuesday, 15 February 2011

iphone - Disable taking images in any orientation other than Portrait AVFoundation -



iphone - Disable taking images in any orientation other than Portrait AVFoundation -

i using avfoundation show camera.

i prevent photographic camera rotate viewer see photographic camera in portrait , images taken in portrait mode.

i defined supported interface orientation back upwards portrait , view beingness displayed in portrait mode, not photographic camera - beingness rotated device orientation

how can forcefulness avfoundation photographic camera displayed , capture images in portrait uiviewcontroller?

my code set camera:

avcapturevideopreviewlayer* lay = [[avcapturevideopreviewlayer alloc] initwithsession:self.sess]; uiview *view = [self videopreviewview]; calayer *viewlayer = [view layer]; [viewlayer setmaskstobounds:yes]; cgrect bounds = [view bounds]; [lay setframe:bounds]; if ([lay respondstoselector:@selector(connection)]) { if ([lay.connection isvideoorientationsupported]) { [lay.connection setvideoorientation:avcapturevideoorientationportrait]; } } [lay setvideogravity:avlayervideogravityresizeaspectfill]; [viewlayer insertsublayer:lay below:[[viewlayer sublayers] objectatindex:0]]; self.previewlayer = lay;

here partial reply based on understanding of question (which differs other answers have had).

you have app locked portrait orientation. status bar @ portrait top of phone regardless of phone's orientation. locks interface, including avcapture interface. want lock raw image feed photographic camera image horizon parallel status bar.

this ideally need done continuously - if have photographic camera @ 45degree angle image counter-rotated 45 degrees. otherwise, of time, image not aligned correctly (the alternative is out of line until 90degree orientation switch updates, swivel image 90 degrees).

to need utilize core motion , accelerometer. want angle of phone's y-axis true vertical , rotate image accordingly. see here geometry details:

iphone orientation -- how figure out way up?

using core motion, trigger method viewdidload

- (void)startaccelerometerupdates { self.coremotionmanager = [[cmmotionmanager alloc] init]; if ([self.coremotionmanager isaccelerometeravailable] == yes) { cgfloat updateinterval = 0.1; // assign update interval motion manager [self.coremotionmanager setaccelerometerupdateinterval:updateinterval]; [self.coremotionmanager startaccelerometerupdatestoqueue:[nsoperationqueue mainqueue] withhandler: ^(cmaccelerometerdata *accelerometerdata, nserror *error) { cgfloat angle = -atan2( accelerometerdata.acceleration.x, accelerometerdata.acceleration.y) + m_pi ; catransform3d rotate = catransform3dmakerotation(angle, 0, 0, 1); self.previewlayer.transform = rotate; }]; } }

a b c

phone held (a) portrait; (b) rotated ~30deg; (c) landscape .

you may find little jumpy, , there bit of lag between device motion , view. can play updateinterval, , in deeper other core motion trickery dampen movement. (i have not treated case of phone beingness upside down, , if hold photographic camera face downwards or face up, result undefined fixed updated code/ utilize of atan2).

now orientation reasonably correct, image not fit view. there not lot can format of raw photographic camera feed fixed physical dimensions of it's sensor array. workaround zoom image have plenty excess image info @ angles enable crop image fit portrait format want.

either in interface builder:

set previewlayer's view square centered on it's superview, width , height equal diagonal of visible image area (sqrt (width2+height2)

or in code:

- (void)resizecameraview { cgsize size = self. videopreviewview.bounds.size; cgfloat diagonal = sqrt(pow(size.width,2)+pow(size.height,2)); diagonal = 2*ceil(diagonal/2); //rounding self.videopreviewview.bounds = (cgrect){0,0,diagonal,diagonal}; }

if in code, resizecameraview should work if phone call viewdidload. create sure self.videopreviewview iboutlet reference right view.

now when take photo, capture whole of 'raw' image info camera's array, in landscape format. saved orientation flag display rotation. may want save photo seen onscreen. means have rotate , crop photo match onscreen view before saving it, , remove it's orientation metadata. that's work out (the other part of 'partial answer'): suspect might decide whole approach doesn't want (i think you'd photographic camera sensor hardware-rotates against rotation of device maintain horizon stable).

update changed startaccelerometerupdates angle atan2 instead of acos, smoother , takes business relationship of directions without fiddling

update 2 comments, seems rotated preview layer getting stuck? cannot replicate error, must other place in code or settings.

so can check clean code, have added solution apple's avcam project, can check against that. here do:

add core motion framework avcam.

in avcamviewcontroller.m

#import <coremotion/coremotion.h> add startaccelerometerupdates method add resizecameraview method (stick both of these methods near top of class file or may confused, there more 1 @implementations in file) add line: [self resizecameraview]; viewdidload (it can first line of method) add property @property (strong, nonatomic) cmmotionmanager* coremotionmanager @interface (it doesn't need property, method assumes exists, if don't add together have modify method instead).

in startaccelerometerupdates alter line:

self.previewlayer.transform = rotate;

to:

self.capturevideopreviewlayer.transform = rotate;

also, in objects list in avcamviewcontroller.xib, move videopreview view above toolbar (otherwise when enlarge cover controls)

be sure disable rotations - ios<6.0, true, 6.0+ need select portrait in supported orientations in target summary.

i think complete list of changes made avcam, , rotation/orientation working well. suggest seek doing same. if can work smoothly, know there other glitch in code somewhere. if still find rotations stick, curious know more hardware , software environment such devices testing on.

i compiling on xcode 4.6/osx10.8.2, , testing on:

- iphone4s / ios5.1 - iphone3g / ios6.1 - ipad mini / ios6.1

all results smooth , accurate.

iphone objective-c xcode avfoundation uideviceorientation

No comments:

Post a Comment