2017-03-31 46 views
0

我有一個前置攝像頭的問題,它由於某種原因翻轉圖片。我試圖使用imageView.transform = CGAffineTransform(scaleX: -1, y: 1)來避免這個問題,它的工作。但我意識到我也在翻轉背部相機。任何解決方案Swift 3非翻轉前置攝像頭圖片

ImagePreviewViewController:

@IBAction func close(_ sender: Any) { 

    let cameraViewController = storyboard?.instantiateViewController(withIdentifier: "CameraViewController") as! CameraViewController 
    navigationController?.pushViewController(cameraViewController, animated: true) 

} 
@IBOutlet weak var close: UIButton! 
var capturedImage : UIImage? 

@IBOutlet weak var imageView: UIImageView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    imageView.image = capturedImage 
      close.setImage(UIImage(named: "Close.png"), for: UIControlState.normal) 
    //imageView.transform = CGAffineTransform(scaleX: -1, y: 1) 
} 

CameraViewController:

@IBOutlet var cameraView: UIView! 
let session = AVCaptureSession() 
var stillImageOutput: AVCaptureStillImageOutput = AVCaptureStillImageOutput() 
var camera : AVCaptureDevice? 
var cameraPreviewLayer : AVCaptureVideoPreviewLayer? 
var cameraCaptureOutput : AVCapturePhotoOutput? 
var frontCamera: Bool = false 

override func viewDidLoad() { 
    super.viewDidLoad() 
    session.sessionPreset = AVCaptureSessionPresetPhoto 
    frontCamera(frontCamera) 
     if camera != nil{ 

      beginSession() 

    } 
    initializeCaptureSession() 
    shoot.setImage(UIImage(named: "Shoot.png"), for: UIControlState.normal) 
    // Do any additional setup after loading the view, typically from a nib. 
} 

func beginSession(){ 

    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session) 
    self.cameraView.layer.addSublayer(cameraPreviewLayer!) 
    cameraPreviewLayer?.frame = self.cameraView.layer.bounds 
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
    session.startRunning() 
    stillImageOutput.outputSettings = [AVVideoCodecKey : AVVideoCodecJPEG] 
    if session.canAddOutput(stillImageOutput){ 
     session.addOutput(stillImageOutput) 

    } 
} 

func frontCamera(_ front: Bool){ 

    let devices = AVCaptureDevice.devices() 

    do{ 

     try session.removeInput(AVCaptureDeviceInput(device: camera)) 

    } catch { 

     print("error") 
    } 

    for device in devices!{ 

     if((device as AnyObject).hasMediaType(AVMediaTypeVideo)) { 
      if front{ 
       if (device as AnyObject).position == AVCaptureDevicePosition.front { 

        camera = device as? AVCaptureDevice 

        do{ 
         try session.addInput(AVCaptureDeviceInput(device: camera)) 
        } catch { 


        } 
        break 

        } 
      } else { 


      } 
      } 



    } 
} 

func displayCapturedPhoto(capturedPhoto : UIImage) { 

    let imagePreviewViewController = storyboard?.instantiateViewController(withIdentifier: "ImagePreviewViewController") as! ImagePreviewViewController 
    imagePreviewViewController.capturedImage = capturedPhoto 
    navigationController?.pushViewController(imagePreviewViewController, animated: true) 
} 


@IBOutlet weak var shoot: UIButton! 
@IBAction func takePicture(_ sender: Any) { 

    takePicture() 

} 

func initializeCaptureSession() { 

    session.sessionPreset = AVCaptureSessionPresetHigh 
    camera = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo) 

    do { 
     let cameraCaptureInput = try AVCaptureDeviceInput(device: camera!) 
     cameraCaptureOutput = AVCapturePhotoOutput() 

     session.addInput(cameraCaptureInput) 
     session.addOutput(cameraCaptureOutput) 

    } catch { 
     print(error.localizedDescription) 
    } 

    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session) 
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
    cameraPreviewLayer?.frame = view.bounds 
    cameraPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 

    view.layer.insertSublayer(cameraPreviewLayer!, at: 0) 

    session.startRunning() 
} 

func takePicture() { 
    let settings = AVCapturePhotoSettings() 
    settings.flashMode = .off 
    cameraCaptureOutput?.capturePhoto(with: settings, delegate: self as AVCapturePhotoCaptureDelegate) 
} 


@IBOutlet weak var flash: UIButton! 
@IBAction func flash(_ sender: Any) { 



    } 

@IBAction func switchCamera(_ sender: Any) { 

    frontCamera = !frontCamera 
    session.beginConfiguration() 
    let inputs = session.inputs as! [AVCaptureInput] 
    for oldInput: AVCaptureInput in inputs{ 
     session.removeInput(oldInput) 

    } 
    frontCamera(frontCamera) 

    session.commitConfiguration() 
} 

}

延伸CameraViewController:AVCapturePhotoCaptureDelegate {

func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) { 

    if let unwrappedError = error { 
     print(unwrappedError.localizedDescription) 
    } else { 

     if let sampleBuffer = photoSampleBuffer, let dataImage = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: sampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) { 

      if let finalImage = UIImage(data: dataImage) { 

       displayCapturedPhoto(capturedPhoto: finalImage) 
      } 
     } 
    } 
} 

override var prefersStatusBarHidden: Bool { 
    return true 
} 

}

+0

你是如何設定imageView'的'的內容? –

+1

「任何解決方案?」任何代碼? – matt

+0

我添加了CameraViewController代碼和ImagePreviewViewController。感謝你們。 – Abdullah

回答

0

嘗試將連接的isVideoMirrored屬性設置爲true。這會翻轉圖像。

func initializeCaptureSession() { 

... 

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: session) 
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
cameraPreviewLayer?.frame = view.bounds 
cameraPreviewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait 
cameraPreviewLayer?.connection.isVideoMirrored = true 

view.layer.insertSublayer(cameraPreviewLayer!, at: 0) 

session.startRunning() 

}

相關問題