ios - AVFoundation CaptureStillImage issue with orientation of result image -


i have custom camera view controller uses avfoundation capture still image can see here: capture preview

my issue once capture image, orientation sideways can see herecaptured image

my code follows:

    var captureinput: avcaptureinput?     var stillimageoutput: avcapturestillimageoutput?     var previewlayer: avcapturevideopreviewlayer?     var currentdevice: avcapturedevice?     var currentlayer: avcapturevideopreviewlayer?      @ibaction func capturebuttontap(sender: uibutton) {         if(!capturingphoto){         capturingphoto = true         if let videoconnection = stillimageoutput!.connectionwithmediatype(avmediatypevideo) {             dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0)) {             videoconnection.videoorientation = avcapturevideoorientation.portrait             self.stillimageoutput?.capturestillimageasynchronouslyfromconnection(videoconnection, completionhandler: {(samplebuffer, error) in                 if (samplebuffer != nil) {                     var imagedata = avcapturestillimageoutput.jpegstillimagensdatarepresentation(samplebuffer)                     var image = uiimage(data: imagedata)                     var imagewidth = image?.size.width                     var croprect = cgrect(x: 0, y: 0, width: imagewidth!, height: imagewidth!)                     var imageref: cgimageref = cgimagecreatewithimageinrect(image!.cgimage, croprect)                     var croppedimage: uiimage = uiimage(cgimage: imageref)!                     self.imagepreview.image = croppedimage                     self.image = croppedimage                     self.switchmodetopreview()                 }             })             }             }         }     }          func initcamerasession(){         configuredevice()         nslog(string(selectedcamera))         capturesession = avcapturesession()         capturesession!.sessionpreset = avcapturesessionpresetphoto         if(selectedcamera == 0){             var backcamera = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo)             currentdevice = backcamera         }else if(selectedcamera == 1){             var frontcamera = frontcameraifavalible()             currentdevice = frontcamera         }         if(currentdevice == nil){             nsexception(name: "error", reason: "device has no camera.", userinfo: nil)         }         updatebuttons()          var error: nserror?         var input = avcapturedeviceinput(device: currentdevice, error: &error)          if error == nil && capturesession!.canaddinput(input) {             capturesession!.addinput(input)             captureinput = input             stillimageoutput = avcapturestillimageoutput()             stillimageoutput!.outputsettings = [avvideocodeckey: avvideocodecjpeg]             if capturesession!.canaddoutput(stillimageoutput) {                 capturesession!.addoutput(stillimageoutput)                 previewlayer = avcapturevideopreviewlayer(session: capturesession)                 previewlayer!.videogravity = avlayervideogravityresizeaspectfill                 previewlayer!.connection?.videoorientation = avcapturevideoorientation.portrait                 camerapreview.layer.addsublayer(previewlayer)                 previewlayer?.frame = cgrect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.width + 139)                 if(currentlayer != nil){                     currentlayer!.removefromsuperlayer()                 }                 currentlayer = previewlayer!                 capturesession!.startrunning()             }         }     } 

please aware have left code out on purpose , if need further information, request it. help.

try using this, use transform buttons on view when rotate device. can used image taken. recall standard transformation rotation matrices linear algebra if have ever taken class. basically, alter code rotate frame of image have captured , make sure set frame bounds view's frame in order have fit correctly in view. may more complicated need be, have never had issue before images. keep in mind being called due rotation change observer has been registered, may not need use. underlying concept used lot

any time have rotation issues, use this.

/**************************************************************************     device orientation did change     **************************************************************************/     func deviceorientationdidchange() {          println("device orientation did change called")          let orientation: uideviceorientation = uidevice.currentdevice().orientation          //------ ignore these orientations ------         if orientation == uideviceorientation.faceup || orientation == uideviceorientation.facedown || orientation == uideviceorientation.unknown || orientation == uideviceorientation.portraitupsidedown || self.currentorientation == orientation {              println("device orientation not need change --- returning...")              return         }           self.currentorientation = orientation           //------ apply rotation using standard rotation transformation matrix in r3 ------          /*              x       y       z             ---           ---         x | cosø    sinø    0 |         y | -sinø   consø   0 |         z | 0       0       1 |             ---           ---          */          //----- perform button , video data buffer rotations ------          switch orientation {          case uideviceorientation.portrait:              println("device orientation portrait")              if self.usingfrontcamera == true {             }             else {                  self.playbacktransformation = cgaffinetransformmakerotation(self.degrees0)                 self.switchcapturesession.transform = self.playbacktransformation!                 self.switchcapturesession.frame = self.view.bounds                 self.flipcamera.transform = self.playbacktransformation!                 self.flipcamera.frame = self.view.bounds                 self.capturebutton.transform = self.playbacktransformation!                 self.capturebutton.frame = self.view.bounds             }              break          case uideviceorientation.landscapeleft:              println("device orientation landscapeleft")              if self.usingfrontcamera == true {             }             else {                  self.playbacktransformation = cgaffinetransformmakerotation(cgfloat(self.degrees90))                 self.switchcapturesession.transform = self.playbacktransformation!                 self.switchcapturesession.frame = self.view.bounds                 self.flipcamera.transform = self.playbacktransformation!                 self.flipcamera.frame = self.view.bounds                 self.capturebutton.transform = self.playbacktransformation!                 self.capturebutton.frame = self.view.bounds             }              break          case uideviceorientation.landscaperight:              println("device orientation landscaperight")              if self.usingfrontcamera == true {             }             else {                  self.playbacktransformation = cgaffinetransformmakerotation(-self.degrees90)                 self.switchcapturesession.transform = self.playbacktransformation!                 self.switchcapturesession.frame = self.view.bounds                 self.flipcamera.transform = self.playbacktransformation!                 self.flipcamera.frame = self.view.bounds                 self.capturebutton.transform = self.playbacktransformation!                 self.capturebutton.frame = self.view.bounds             }              break          default:              break         } 

affine transformation great rotations. there may function sets default image orientation when captured. check apple's references.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -