AVFoundation CaptureStillImage问题与结果图像的方向

时间:2015-05-14 14:14:29

标签: ios swift avfoundation

我有一个自定义相机视图控制器,它使用AVFoundation捕获静态图像,您可以在此处看到:Capture Preview

我的问题是,一旦我捕获图像,方向总是侧面的,你可以在这里看到Captured Image

我的代码如下:

    var captureInput: AVCaptureInput?
    var stillImageOutput: AVCaptureStillImageOutput?
    var previewLayer: AVCaptureVideoPreviewLayer?
    var currentDevice: AVCaptureDevice?
    var currentLayer: AVCaptureVideoPreviewLayer?

    @IBAction func captureButtonTap(sender: UIButton) {
        if(!capturingPhoto){
        capturingPhoto = true
        if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
            videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
            self.stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
                if (sampleBuffer != nil) {
                    var imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                    var image = UIImage(data: imageData)
                    var imageWidth = image?.size.width
                    var cropRect = CGRect(x: 0, y: 0, width: imageWidth!, height: imageWidth!)
                    var imageRef: CGImageRef = CGImageCreateWithImageInRect(image!.CGImage, cropRect)
                    var croppedImage: UIImage = UIImage(CGImage: imageRef)!
                    self.imagePreview.image = croppedImage
                    self.image = croppedImage
                    self.switchModeToPreview()
                }
            })
            }
            }
        }
    }

        func initCameraSession(){
        configureDevice()
        NSLog(String(selectedCamera))
        captureSession = AVCaptureSession()
        captureSession!.sessionPreset = AVCaptureSessionPresetPhoto
        if(selectedCamera == 0){
            var backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
            currentDevice = backCamera
        }else if(selectedCamera == 1){
            var frontCamera = frontCameraIfAvalible()
            currentDevice = frontCamera
        }
        if(currentDevice == nil){
            NSException(name: "Error", reason: "Device Has No Camera.", userInfo: nil)
        }
        updateButtons()

        var error: NSError?
        var input = AVCaptureDeviceInput(device: currentDevice, error: &error)

        if error == nil && captureSession!.canAddInput(input) {
            captureSession!.addInput(input)
            captureInput = input
            stillImageOutput = AVCaptureStillImageOutput()
            stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
            if captureSession!.canAddOutput(stillImageOutput) {
                captureSession!.addOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
                previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
                cameraPreview.layer.addSublayer(previewLayer)
                previewLayer?.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.width + 139)
                if(currentLayer != nil){
                    currentLayer!.removeFromSuperlayer()
                }
                currentLayer = previewLayer!
                captureSession!.startRunning()
            }
        }
    }

请注意我已经故意留下了一些代码,如果您需要更多信息,请提出要求。谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

尝试使用类似的东西,当我旋转设备时,我用它来转换视图上的按钮。这可以与拍摄的图像一起使用。如果您曾参加过该课程,请从线性代数中调出标准变换旋转矩阵。基本上,只需更改此代码即可旋转已捕获图像的帧,然后确保将其帧边界设置回视图的框架,以使其在视图中正确适应。这可能比需要的更复杂,但我之前从未遇到过这个问题。请记住,由于已注册的旋转更改观察器,您可能不需要使用它。但这个基本概念使用了很多

任何时候我都有旋转问题,我会用它。

/**************************************************************************
    DEVICE ORIENTATION DID CHANGE
    **************************************************************************/
    func deviceOrientationDidChange() {

        println("DEVICE ORIENTATION DID CHANGE CALLED")

        let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation

        //------ IGNORE THESE ORIENTATIONS ------
        if orientation == UIDeviceOrientation.FaceUp || orientation == UIDeviceOrientation.FaceDown || orientation == UIDeviceOrientation.Unknown || orientation == UIDeviceOrientation.PortraitUpsideDown || self.currentOrientation == orientation {

            println("device orientation does not need to change --- returning...")

            return
        }


        self.currentOrientation = orientation


        //------ APPLY A ROTATION USING THE STANDARD ROTATION TRANSFORMATION MATRIX in R3 ------

        /*

            x       y       z
            ---           ---
        x | cosø    sinø    0 |
        y | -sinø   consø   0 |
        z | 0       0       1 |
            ---           ---

        */

        //----- PERFORM BUTTON AND VIDEO DATA BUFFER ROTATIONS ------

        switch orientation {

        case UIDeviceOrientation.Portrait:

            println("Device Orientation Portrait")

            if self.usingFrontCamera == true {
            }
            else {

                self.playBackTransformation = CGAffineTransformMakeRotation(self.degrees0)
                self.switchCaptureSession.transform = self.playBackTransformation!
                self.switchCaptureSession.frame = self.view.bounds
                self.flipCamera.transform = self.playBackTransformation!
                self.flipCamera.frame = self.view.bounds
                self.captureButton.transform = self.playBackTransformation!
                self.captureButton.frame = self.view.bounds
            }

            break

        case UIDeviceOrientation.LandscapeLeft:

            println("Device Orientation LandScapeLeft")

            if self.usingFrontCamera == true {
            }
            else {

                self.playBackTransformation = CGAffineTransformMakeRotation(CGFloat(self.degrees90))
                self.switchCaptureSession.transform = self.playBackTransformation!
                self.switchCaptureSession.frame = self.view.bounds
                self.flipCamera.transform = self.playBackTransformation!
                self.flipCamera.frame = self.view.bounds
                self.captureButton.transform = self.playBackTransformation!
                self.captureButton.frame = self.view.bounds
            }

            break

        case UIDeviceOrientation.LandscapeRight:

            println("Device Orientation LandscapeRight")

            if self.usingFrontCamera == true {
            }
            else {

                self.playBackTransformation = CGAffineTransformMakeRotation(-self.degrees90)
                self.switchCaptureSession.transform = self.playBackTransformation!
                self.switchCaptureSession.frame = self.view.bounds
                self.flipCamera.transform = self.playBackTransformation!
                self.flipCamera.frame = self.view.bounds
                self.captureButton.transform = self.playBackTransformation!
                self.captureButton.frame = self.view.bounds
            }

            break

        default:

            break
        }

仿射变换非常适合轮换。可能存在在捕获时设置默认图像方向的功能。查看Apple的参考资料。