首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用前置摄像机录制视频- WP8 - C#

用前置摄像机录制视频- WP8 - C#
EN

Stack Overflow用户
提问于 2015-05-01 09:18:08
回答 2查看 346关注 0票数 1

我的windows应用程序需要从前面的摄像头录制一段视频,并通过网络服务发送给服务器。

在这里,当我试图录制来自front-camera的视频时,我得到了mirror inverted video。意味着前摄像头记录180度旋转视频。

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

问题:

  • wp8中,是否有其他解决方案可以用前置摄像机录制适当的视频?
  • 如果没有,如何旋转视频流180度?有任何c# API来做吗..?

编辑:

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

用于VideoBrushXAML代码

代码语言:javascript
复制
    <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>

初始化相机

代码语言:javascript
复制
    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

代码语言:javascript
复制
    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);
        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-02 07:48:53

经过24小时的努力,我终于用下面的方法解决了我的问题。

通过旋转视频引起问题的代码行在下面。

代码语言:javascript
复制
videoCapture.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, videoCapture.SensorRotationInDegrees);

这里videoCapture是AudioVideoCaptureDevice的对象

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

因此,我使用了上面提到的相同的代码,在这段videoCapture.SetProperty代码行中做了一个小小的修改。正确的代码行如下。

代码语言:javascript
复制
videoCapture.SetProperty(KnownCameraGeneralProperties.EncodeWithOrientation, -(videoCapture.SensorRotationInDegrees));

我只是在videoCapture.SensorRotationInDegrees前面加了一个负号(-)来倒置它。

希望这对大家都有帮助。

票数 0
EN

Stack Overflow用户

发布于 2015-05-02 01:00:50

在过去,这对我来说是可行的,它只是一个条形码扫描器应用程序,我是为了满足功能需求而编写的。我把变换放到了<VideoBrush>上。

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29984325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档