我的前置摄像头有问题,由于某种原因,它会翻转图片。我尝试使用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
}


}

最佳答案

尝试将连接的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()


}

关于ios - Swift 3翻盖前置摄像头图片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43146255/

10-09 15:39