@@ -67,6 +67,14 @@ public class NSCameraView: UIView, NextLevelVideoDelegate, NextLevelPhotoDelegat
6767 self . nextLevel? . torchMode = NextLevelTorchMode ( rawValue: newValue) !
6868 }
6969 }
70+ public var focusMode : Int {
71+ get {
72+ return ( self . nextLevel? . focusMode ?? NextLevelFocusMode . autoFocus) . rawValue
73+ }
74+ set {
75+ self . nextLevel? . focusMode = NextLevelFocusMode ( rawValue: newValue) !
76+ }
77+ }
7078
7179 func commonInit( ) {
7280 self . autoresizingMask = [ . flexibleWidth, . flexibleHeight]
@@ -100,7 +108,7 @@ public class NSCameraView: UIView, NextLevelVideoDelegate, NextLevelPhotoDelegat
100108
101109 // audio configuration
102110 // for now disable audio
103- nextLevel. captureMode = NextLevelCaptureMode . videoWithoutAudio
111+ nextLevel. captureMode = NextLevelCaptureMode . photo
104112 // nextLevel.audioConfiguration.bitRate = 96000
105113 // nextLevel.disableAudioInputDevice()
106114 // metadata objects configuration
@@ -110,7 +118,9 @@ public class NSCameraView: UIView, NextLevelVideoDelegate, NextLevelPhotoDelegat
110118
111119 public func startPreview( ) throws {
112120 do {
121+ if ( self . nextLevel? . session == nil ) {
113122 try self . nextLevel? . start ( )
123+ }
114124 } catch {
115125 let nextLevelError = error as! NextLevelError
116126 // re throw the error with the description so that N can correctly show the error message
@@ -140,7 +150,7 @@ public class NSCameraView: UIView, NextLevelVideoDelegate, NextLevelPhotoDelegat
140150 }
141151 }
142152 public func capturePhoto( ) {
143- if let nextLevel = self . nextLevel , self . canCapturePhoto {
153+ if let nextLevel = self . nextLevel , self . canCapturePhoto {
144154 if ( nextLevel. captureMode == NextLevelCaptureMode . photo) {
145155 nextLevel. capturePhoto ( )
146156 } else {
@@ -275,19 +285,64 @@ public class NSCameraView: UIView, NextLevelVideoDelegate, NextLevelPhotoDelegat
275285 public func nextLevel( _ nextLevel: NextLevel , output: AVCapturePhotoOutput , willBeginCaptureFor resolvedSettings: AVCaptureResolvedPhotoSettings , photoConfiguration: NextLevelPhotoConfiguration ) {
276286
277287 }
278-
288+ var deviceOrientationOnCapture : UIDeviceOrientation ?
279289 public func nextLevel( _ nextLevel: NextLevel , output: AVCapturePhotoOutput , willCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings , photoConfiguration: NextLevelPhotoConfiguration ) {
290+ self . deviceOrientationOnCapture = UIDevice . current. orientation
280291 }
281292
282293 public func nextLevel( _ nextLevel: NextLevel , output: AVCapturePhotoOutput , didCapturePhotoFor resolvedSettings: AVCaptureResolvedPhotoSettings , photoConfiguration: NextLevelPhotoConfiguration ) {
283294 }
284295
285296 public func nextLevel( _ nextLevel: NextLevel , didFinishProcessingPhoto photo: AVCapturePhoto , photoDict: [ String : Any ] , photoConfiguration: NextLevelPhotoConfiguration ) {
286- self . photoDelegate? . cameraView ( self , didFinishProcessingPhoto: photo, photoDict: photoDict, photoConfiguration: NSCameraViewPhotoConfiguration ( configuration: photoConfiguration) )
297+ let photoMetadata = photo. metadata
298+ // Returns corresponting NSCFNumber. It seems to specify the origin of the image
299+ // print("Metadata orientation: ",photoMetadata["Orientation"])
300+
301+ // Returns corresponting NSCFNumber. It seems to specify the origin of the image
302+ print ( " Metadata orientation with key: " , photoMetadata [ String ( kCGImagePropertyOrientation) ] as Any )
303+
304+ guard let imageData = photo. fileDataRepresentation ( ) else {
305+ print ( " Error while generating image from photo capture data. " ) ;
306+ return
307+ }
308+
309+ guard let uiImage = UIImage ( data: imageData) else {
310+ print ( " Unable to generate UIImage from image data. " ) ;
311+ return
312+ }
313+
314+ // generate a corresponding CGImage
315+ guard let cgImage = uiImage. cgImage else {
316+ print ( " Error generating CGImage " )
317+ return
318+ }
319+
320+ guard let deviceOrientationOnCapture = self . deviceOrientationOnCapture else {
321+ print ( " Error retrieving orientation on capture " )
322+ return
323+ }
324+
325+ var image = UIImage ( cgImage: cgImage, scale: 1.0 , orientation: deviceOrientationOnCapture. getUIImageOrientationFromDevice ( ) )
326+
327+ self . photoDelegate? . cameraView ( self , didFinishProcessingPhoto: image, photoDict: photoDict, photoConfiguration: NSCameraViewPhotoConfiguration ( configuration: photoConfiguration) )
287328 }
288329
289330 public func nextLevelDidCompletePhotoCapture( _ nextLevel: NextLevel ) {
290331
291332 }
292333
293334}
335+
336+ extension UIDeviceOrientation {
337+ func getUIImageOrientationFromDevice( ) -> UIImage . Orientation {
338+ // return CGImagePropertyOrientation based on Device Orientation
339+ // This extented function has been determined based on experimentation with how an UIImage gets displayed.
340+ switch self {
341+ case UIDeviceOrientation . portrait, . faceUp: return UIImage . Orientation. right
342+ case UIDeviceOrientation . portraitUpsideDown, . faceDown: return UIImage . Orientation. left
343+ case UIDeviceOrientation . landscapeLeft: return UIImage . Orientation. up // this is the base orientation
344+ case UIDeviceOrientation . landscapeRight: return UIImage . Orientation. down
345+ case UIDeviceOrientation . unknown: return UIImage . Orientation. up
346+ }
347+ }
348+ }
0 commit comments