AVFoundation捕获静态图像的方向问题

3

我有一个自定义相机视图控制器,使用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()
            }
        }
    }

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

https://dev59.com/wGQo5IYBdhLWcg3wiv_A#69580115 - YodagamaHeshan
1个回答

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
        }

仿射变换非常适合旋转。可能会有一个函数在捕获图像时设置默认的图像方向。请查阅苹果的参考文献。

你知道是哪个委托调用了 deviceOrientationDidChange 吗?还是它是一个 NSNotification - uhfocuz
这是一个我已经注册的通知。这是为了设备方向变化而设计的。问题似乎或可能是由于捕获图片时的默认方向,而实际上是在捕获时发生的。如果您希望图片随设备旋转,则应执行以下操作。UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications() NSNotificationCenter.defaultCenter().addObserver(self, selector: "deviceOrientationDidChange", name: UIDeviceOrientationDidChangeNotification, object: nil)。 - James Combs
该代码不应盲目复制粘贴,它仅作为可以应用的概念。 - James Combs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接