通过前置摄像头录制视频 - WP8 - C#

时间:2015-05-01 09:18:08

标签: windows-phone-8 video rotation front-camera

我的Windows手机应用程序需要从前置摄像头录制视频,并通过网络服务将其发送到服务器。

在我尝试录制来自front-camera的视频时,我正在mirror inverted video。意味着前置摄像头记录180度旋转视频。

我认为可能唯一的解决办法是将录制的视频流旋转180度。

问题:

  • 还有其他方法可以通过前置摄像头在wp8
  • 中录制正确的视频
  • 如果没有,如何将视频流旋转180度?任何c# API都可以做到这一点..?

修改

以下是我正在使用的代码:

VideoBrush

的XAML代码
    <Canvas x:Name="CanvasLayoutRoot" RenderTransformOrigin="0.5 0.5"
            Width="{Binding ActualHeight, ElementName=LayoutRoot}"
            Height="{Binding ActualWidth, ElementName=LayoutRoot}"
            Margin="-160 0 0 0">
        <!--Background="Transparent"-->

        <Canvas.Background>
            <VideoBrush x:Name="videoBrush" />
        </Canvas.Background>
        <Canvas.RenderTransform>
            <RotateTransform x:Name="rt" />
        </Canvas.RenderTransform>

    </Canvas>

初始化相机

    public async void InitializeVideoRecorder()
    {
        try
        {
            if (videoCapture == null)
            {
                // below line of code will detect if "Front Camera" is available or not
                // if availble, then open it or it will open "Back Camera"

                videoCapture = await AudioVideoCaptureDevice.OpenAsync(
                    AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Front) ? CameraSensorLocation.Front : CameraSensorLocation.Back,
                    new Windows.Foundation.Size(640, 480));

                videoCapture.RecordingFailed += videoCapture_RecordingFailed;

                videoCapture.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, videoCapture.SensorRotationInDegrees);

                // Initialize the camera if it exists on the phone.
                if (videoCapture != null)
                {
                    videoBrush.SetSource(videoCapture);
                    if (!AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Front))
                    {
                        rt.Angle = videoCapture.SensorRotationInDegrees;
                    }
                    else
                    {
                        rt.Angle = -(videoCapture.SensorRotationInDegrees);
                    }
                }
                else
                {
                    MessageBox.Show("Unable to load Camera. Please try again later.", App.appName, MessageBoxButton.OK);
                    NavigationService.GoBack();
                }
            }
        }
        catch (Exception ex)
        {
            (new WebServices()).catchExceptions(ex);
            NavigationService.GoBack();
        }
    }

启动VideoCapture

    private async Task StartVideoRecording()
    {
        try
        {
            // Gets the application data folder
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            StorageFolder transfersFolder = await (await applicationFolder.GetFolderAsync("Shared")).GetFolderAsync("Transfers");

            // Create the file specified in the application data folder
            videoFileName = selectedQue.response.template_id + "_" + selectedQue.response.id + "_" + selectedQue.response.invite_id +".mp4";
            StorageFile storageFile = await transfersFolder.CreateFileAsync(videoFileName, CreationCollisionOption.ReplaceExisting);

            // Open a file stream, ready to write video data
            randomAccessStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);

            // Video recording to the specified stream
            await videoCapture.StartRecordingToStreamAsync(randomAccessStream);
            isRecordingStarted = true;

            //timer = "0:00";
            tbTimer.Text = "0:00";
            dt.Start();
        }
        catch (Exception ex)
        {
            (new WebServices()).catchExceptions(ex);
        }
    }

1 个答案:

答案 0 :(得分:0)

最后,我通过以下解决方案进行了24小时的努力后解决了我的问题。

通过旋转视频导致问题的代码行如下。

videoCapture.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, videoCapture.SensorRotationInDegrees);

这里videoCapture是AudioVideoCaptureDevice的对象

使用前置摄像头时,我们需要反转cameraSensor的旋转。

所以我在上面videoCapture.SetProperty代码行中使用了上面相同的代码(提到了问题)并进行了一些微小的修改。正确的代码行如下。

videoCapture.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, -(videoCapture.SensorRotationInDegrees));

我只是通过在它前面添加一个减号( - )来反转videoCapture.SensorRotationInDegrees

希望这有助于所有..