首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带摄像头的UIImageOrientation

带摄像头的UIImageOrientation
EN

Stack Overflow用户
提问于 2017-02-01 14:46:19
回答 1查看 512关注 0票数 1

这是我的相机的代码,将前置相机换成后置相机,如果我用后置相机拍照,照片的方向是好的(原始),但如果我用前置相机拍照,得到的图像方向不好。

代码语言:javascript
复制
class TakeSelfieViewController: UIViewController, AVCapturePhotoCaptureDelegate {

var captureSession = AVCaptureSession()
var photoOutput = AVCapturePhotoOutput()
var previewLayer : AVCaptureVideoPreviewLayer?
var captureDevice : AVCaptureDevice?

var sessionOutputSetting = AVCapturePhotoSettings(format: [AVVideoCodecKey:AVVideoCodecJPEG])

var toggle = false

@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var tempImageView: UIImageView!
@IBOutlet weak var adorButton: UIButton!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    previewLayer?.frame = cameraView.bounds

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = adorButton.bounds
    blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    adorButton.addSubview(blurEffectView)

}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    pickCamera(which: toggle)

}

func pickCamera(which: Bool) {

    if (which == true) {

        let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.back)

        print("back camera")

        startCamera(deviceDesc: deviceDescovery!)

        toggle = true

    } else if (which == false) {

        let deviceDescovery = AVCaptureDeviceDiscoverySession(deviceTypes: [AVCaptureDeviceType.builtInDualCamera, AVCaptureDeviceType.builtInTelephotoCamera,AVCaptureDeviceType.builtInWideAngleCamera], mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)

        print("front camera")

        startCamera(deviceDesc: deviceDescovery!)

        toggle = false

    }

}


func startCamera(deviceDesc: AVCaptureDeviceDiscoverySession!) {

    for device in (deviceDesc.devices)! {


        if device.position == AVCaptureDevicePosition.back {

            do {

                let input = try AVCaptureDeviceInput(device: device)
                if captureSession.canAddInput(input) {
                    captureSession.addInput(input)

                    if captureSession.canAddOutput(photoOutput) {
                        captureSession.addOutput(photoOutput)


                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait


                        cameraView.layer.addSublayer(previewLayer!)
                        captureSession.startRunning()
                        print("ADD Back")

                    } else { print("Cannot add input - back") }

                }

            } catch {

                print("Error")

            }

        } else if (device.position == AVCaptureDevicePosition.front) {

            do {

                let input = try AVCaptureDeviceInput(device: device)
                print(input)
                if captureSession.canAddInput(input) {

                    captureSession.addInput(input)

                    if captureSession.canAddOutput(photoOutput) {
                        captureSession.addOutput(photoOutput)


                        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                        previewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
                        previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait


                        cameraView.layer.addSublayer(previewLayer!)
                        captureSession.startRunning()
                        print("ADD Front")

                }

                } else { print("Cannot add input - front") }

            } catch {

                print(error)

            }
        }
    }
}

func didPressTakePhoto() {
    if let videoConnection = photoOutput.connection(withMediaType: AVMediaTypeVideo) {
        videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
        let settings = AVCapturePhotoSettings(format: [AVVideoCodecKey : AVVideoCodecJPEG])
        photoOutput.capturePhoto(with: settings, delegate: self)

    }
}

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

    let imageData = AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer!, previewPhotoSampleBuffer: previewPhotoSampleBuffer)
    let dataProvider = CGDataProvider(data: imageData as! CFData)
    let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)

    let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
    self.tempImageView.image = image
    self.tempImageView.isHidden = false
    self.yellowButton.isHidden = true
    self.toggleAction.isHidden = true
    self.adorButton.isHidden = true
    print("Hola")
}

var didTakePhoto = Bool()

@IBOutlet weak var yellowButton: UIButton!
@IBOutlet weak var toggleAction: UIButton!

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if didTakePhoto {
        tempImageView.isHidden = true
        yellowButton.isHidden = false
        toggleAction.isHidden = false
        adorButton.isHidden = false
        didTakePhoto = false
        print("")

    }
}

@IBAction func yellowPressed(_ sender: UIButton) {
    captureSession.startRunning()
    didTakePhoto = true
    didPressTakePhoto()
    print("")
}

@IBAction func toggleCamera(_ sender: Any) {

    if (toggle == false) {

        print("Changing to back camera")

        let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput

        captureSession.removeInput(currentCameraInput)

        toggle = true

        pickCamera(which: toggle)

    } else if (toggle == true) {

        print("Changing to front camera")

        let currentCameraInput: AVCaptureInput = captureSession.inputs[0] as! AVCaptureInput

        captureSession.removeInput(currentCameraInput)

        toggle = false

        pickCamera(which: toggle)

    }

}

override var prefersStatusBarHidden: Bool {

    return true

}
}

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2017-02-01 16:53:36

用前置相机拍摄的图像是镜像的,当你拍摄一张照片时,图像方向是在它的EXIF字典中拍摄的,或者在元数据字典中传递。

大多数情况下,当您将其作为JPG或PNG传递时,如果您不直接处理它,则不会考虑这个值。如果你在风景中拍照,你应该会遇到类似的问题。

在您的捕获方法中,您似乎正在强制将方向设置为固定值,而您应该注意它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41973524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档