-->

WP8 - - C#通过前置摄像头录制视频(record video by front-ca

2019-10-22 22:31发布

我的Windows手机应用程序需要记录的前置摄像头的视频,它通过一个Web服务发送到服务器。

在这里,而我试图记录从视频front-camera ,我越来越mirror inverted video 。 意味着前置摄像头记录180度旋转的视频。

我想大概是它的唯一的解决办法是录制的视频流,以180度旋转回来。

题:

  • 还有没有其他的解决方案通过前置摄像头在记录视频的正常wp8
  • 如果没有,如何旋转视频流180度? 任何c# API来做到这一点..?

编辑:

下面是代码,我使用的是:

为XAML代码VideoBrush

    <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);
        }
    }

Answer 1:

这在过去的工作对我来说,这只是一个条形码扫描应用程序,我编写了完成一个功能性需求。 我穿上变换<VideoBrush>

<Grid x:Name="ContentPanel" Margin="12,0,12,0">
    <Canvas x:Name="cam_canvas" Width="480" Height="480">                
        <Canvas.Background>
            <VideoBrush x:Name="cam_video_brush" Stretch="None">
                <VideoBrush.RelativeTransform>
                    <CompositeTransform Rotation="90" CenterX="0.5" CenterY="0.5" />
                </VideoBrush.RelativeTransform>
            </VideoBrush>
        </Canvas.Background>
    </Canvas>
</Grid>


Answer 2:

最后,我24小时后的下层溶液经过努力解决我的问题。

的代码,通过旋转视频引起问题的线低于。

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

这里videoCapture是AudioVideoCaptureDevice的对象

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

所以我用在这一个细小的改动使用上述相同的代码(在问题中提到) videoCapture.SetProperty的代码行。 的代码的正确路线如下所示。

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

我刚倒了videoCapture.SensorRotationInDegrees前- ()加入一个减号。

希望这有助于所有..



文章来源: record video by front-camera - WP8 - C#