record video by front-camera - WP8 - C#

北城余情 提交于 2019-12-11 10:25:21

问题


My windows phone app needs to record a video from front-camera and send it to the server through a webservice.

Here while I'm trying to record video from front-camera, I'm getting mirror inverted video. Means front-camera records 180 degree rotated video.

what i think probably the only solution of it is to rotate the recorded video stream to 180 degree back.

Question:

  • is there any other solution to record proper video by front-camera in wp8?
  • if not, how to rotate the video stream 180 degree? any c# API to do it..?

Edit:

Here is code that I'm using:

XAML code for 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>

Initializing camera

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

Starting 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:


This worked for me in the past, it was just a barcode scanner app that I coded up to fulfill a functional requirement. I put the transform on the <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>



回答2:


Finally i solved my problem after 24 hours of efforts with below solution.

The line of code that causing issue by rotating video was below.

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

here videoCapture is object of AudioVideoCaptureDevice

While using front camera, we need to invert the rotation of cameraSensor.

So I've used the above same code (mentioned in question) with one tiny modification in this videoCapture.SetProperty line of code. the correct line of code is as below.

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

I just inverted the videoCapture.SensorRotationInDegrees by adding one minus sign (-) before it.

Hope this helps all..



来源:https://stackoverflow.com/questions/29984325/record-video-by-front-camera-wp8-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!