通过前置摄像头录制视频-WP8-C# [英] record video by front-camera - WP8 - C#
问题描述
我的Windows Phone应用需要记录来自前置摄像头的视频,然后通过网络服务将其发送到服务器.
My windows phone app needs to record a video from front-camera and send it to the server through a webservice.
在这里,当我尝试从front-camera
录制视频时,我得到了mirror inverted video
.意味着前置摄像头记录了180度旋转的视频.
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.
我认为可能唯一的解决方案是将录制的视频流向后旋转180度.
what i think probably the only solution of it is to rotate the recorded video stream to 180 degree back.
问题:
- 还有其他解决方案可以通过前置摄像头在
wp8
中录制正确的视频吗? - 如果没有,如何将视频流旋转180度?任何
c#
API都可以做到..?
- 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..?
这是我正在使用的代码:
Here is code that I'm using:
VideoBrush
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>
初始化相机
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);
}
}
推荐答案
最后,经过24小时的以下解决方案,我解决了我的问题.
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);
此处videoCapture是AudioVideoCaptureDevice的对象
here videoCapture is object of AudioVideoCaptureDevice
在使用前置摄像头时,我们需要反转cameraSensor
的旋转.
While using front camera, we need to invert the rotation of cameraSensor
.
因此,我在代码的videoCapture.SetProperty
行中使用了与上面相同的代码(有问题的提及),并对它进行了微小的修改.正确的代码行如下.
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));
我只是在videoCapture.SensorRotationInDegrees
前面加上一个减号(-)来反转它.
I just inverted the videoCapture.SensorRotationInDegrees
by adding one minus sign (-) before it.
希望这对所有人有帮助.
Hope this helps all..
这篇关于通过前置摄像头录制视频-WP8-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!