Swift 3 un-flip前置摄像头图片

时间:2017-03-31 17:12:45

标签: ios iphone swift swift3

前置摄像头出现问题,出于某种原因翻转图片。我尝试使用imageView.transform = CGAffineTransform(scaleX: -1, y: 1)来避免这个问题并且它有效。但我意识到我也正在翻转后置摄像头。任何解决方案?

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
}

}

1 个答案:

答案 0 :(得分: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()

}