使用前置摄像头时需要镜像视频方向和手柄旋转

詹姆斯·科姆斯

我不太清楚如何处理前置摄像头的视频捕获方向。拍摄视频和图片时,我对后置摄像头进行了所有旋转处理,拍摄照片时,我对前摄像头进行了所有处理,并以正确的方向保存了捕获的视频和图片(前摄像头视频捕获除外)。

第一个问题是,在任一横向模式下,视频均未正确以正确的方向保存。第二个问题是已保存的视频已镜像。虽然我知道如何使用前置摄像头处理图片的这种镜像效果,但是我不确定调用视频处理该如何调用。

我很难找到专门针对此问题的任何东西,但未能做到。如果有人可以指出我要解决这个特定问题的线程,那将是很好的。

无论哪种方式,这都是在设备方向发生变化时处理视频方向的调用方法。如果使用前置摄像头,我不确定要在代码中添加什么内容。

/**************************************************************************
    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:

            rotateButtons(self.degrees0)

            if self.usingFrontCamera == true {


            }
            else {

            }

            println("Device Orientation Portrait")

            break

        case UIDeviceOrientation.LandscapeLeft:

            println("Device Orientation LandScapeLeft")

            rotateButtons(self.degrees90)

            if self.usingFrontCamera == true {

                println("Using front camera, rotation in landscape left")

//                if let connection = self.captureConnection {
//                    
//                    connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
//                    
//                    println("Capture connection Orientation is LandScape Right")
//                }
//                else {
//                    
//                    println("Capture connection is nil, could not change video orientation")
//                }
            }
            else {

                if let connection = self.captureConnection {

                    connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight

                    println("Capture connection Orientation is LandScape Right")
                }
                else {

                    println("Capture connection is nil, could not change video orientation")
                }
            }

            break

        case UIDeviceOrientation.LandscapeRight:

            println("Device Orientation LandscapeRight")

            rotateButtons(-self.degrees90)

            if self.usingFrontCamera == true {

                println("Using front camera, rotation in landscape right")

//                if let connection = self.captureConnection {
//                    
//                    connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
//                    
//                    println("Capture connection Orientation is LandScape Left")
//                }
//                else {
//                    
//                    println("Capture connection is nil, could not change video orientation")
//                }
            }
            else {

                if let connection = self.captureConnection {

                    connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft

                    println("Capture connection Orientation is LandScape Left")
                }
                else {

                    println("Capture connection is nil, could not change video orientation")
                }
            }

            break

        default:

            break
        }
    }
山姆

基于此答案:视频保存方向错误AVCaptureSession

我也遇到了同样的问题,并在发布此帖子后得以解决。

var videoConnection:AVCaptureConnection?
  for connection in self.fileOutput.connections {
    for port in connection.inputPorts! {
      if port.mediaType == AVMediaTypeVideo {
        videoConnection = connection as? AVCaptureConnection
          if videoConnection!.supportsVideoMirroring {
            videoConnection!.videoMirrored = true
          }
        }
      }
   }
}

请让我知道是否对您有帮助

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

修复使用前置摄像头 ios 拍摄的图像的方向

使用cv2时如何“镜像”实时网络摄像头视频?

使用前置摄像头时不会保存照片

如何使用AVCaptureDeviceDiscoverySession获取前置摄像头,后置摄像头和音频

在iOS和Android上严格使用HTML5输入字段和视频时,如何默认使用前置摄像头?

网络摄像头和手机中的摄像头方向

如何使用camerax前置摄像头保存正确的旋转图像?

如何使前置摄像头捕获的视频不是反Android?

iOS在UIImagePickerController上使用前置摄像头时方向错误

打开前置摄像头

在 Xamarin.iOS 中使用 UIImagePickerController 时,当我选择任何前置摄像头图像时,它会逆时针旋转 90 度

使用ARKit的ARSCNView时,可以使用iPhone的前置摄像头吗?

切换到前置摄像头和后置摄像头Android SurfaceView

如何在javascript中切换前置摄像头和后置摄像头?

尝试启动前置摄像头时出现错误(Swift)

Camera2 录制视频,按下录制按钮后屏幕冻结(仅在使用前置摄像头时和在某些设备上)

同时访问后置和前置摄像头

使用前置摄像头拍摄时未检测到脸部

使用Circuit SDK在WebRTC通话时在Android上切换前置/后置摄像头

如何在Android和iOS上打开前置摄像头并使用Vue.Js拍照?

检测使用前置摄像头还是后置摄像头

旋转时使用拖动手柄调整div的大小

无法使用前置摄像头2拍照

无法在Swift中使用前置摄像头

使用AVCaptureSession切换前置/后置摄像头

在Android服务中使用前置摄像头拍照

Web rtc、android库、镜像前置摄像头显示

如何在镜像中显示OpenCV JavaCameraView前置摄像头(Android)?

前置摄像头上延伸的摄像头预览