我正在尝试使用表达式编码器 sdk 和 wpf c#.net 显示网络摄像头预览、录制视频和抓取快照
我使用了来自http://www.codeproject.com/Articles/285964/WPF-Webcam-Control和http://www.projectcn.org/my-dotnet-softwares/my-webcam的代码。
使用
WebcamPanel.Size = new System.Drawing.Size(1280, 960); \\ xaml control
System.Drawing.Size framesize = new System.Drawing.Size(800, 600);
LiveDeviceSource.PickBestVideoFormat(framesize, 30);
LiveJob.OutputFormat.VideoProfile.Size = framesize;
似乎只能拉伸 320x280 视频,而录制的视频看起来很糟糕(大像素和抖动)。我正在使用高清网络摄像头,从 Windows 8 相机应用程序录制的视频看起来非常好。
网络摄像头.cs:
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder.Live;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project
{
public class WebCam
{
public List<EncoderDevice> VideoDevices { get; set; }
public LiveJob LiveJob { get; set; }
public LiveDeviceSource LiveDeviceSource { get; set; }
public EncoderDevice SelectedVideoDevice { get; set; }
public bool isConnected { get; set; }
public bool isRecording { get; set; }
public FileArchivePublishFormat FileArchivePublishFormat { get; set; }
public WebCam()
{
VideoDevices = new List<EncoderDevice>();
isConnected = false;
}
public int InitializeListVideoDevices()
{
int nb = 0;
foreach (EncoderDevice encoderDevice in EncoderDevices.FindDevices(EncoderDeviceType.Video))
{
VideoDevices.Add(encoderDevice);
nb++;
}
return nb;
}
public bool StartWebcam()
{
if (SelectedVideoDevice == null) return false;
LiveJob = null;
LiveJob = new LiveJob();
LiveDeviceSource = LiveJob.AddDeviceSource(SelectedVideoDevice, null);
System.Drawing.Size framesize = new System.Drawing.Size(800, 600);
LiveDeviceSource.PickBestVideoFormat(framesize, 30);
LiveJob.OutputFormat.VideoProfile.Size = framesize;
LiveJob.ActivateSource(LiveDeviceSource);
isConnected = true;
return true;
}
public void StopWebcam()
{
LiveJob.RemoveDeviceSource(LiveDeviceSource);
LiveDeviceSource = null;
isConnected = false;
}
public bool StartRecording()
{
FileArchivePublishFormat = new FileArchivePublishFormat();
FileArchivePublishFormat.OutputFileName = @"C:\temp.wmv";
LiveJob.PublishFormats.Add(FileArchivePublishFormat);
LiveJob.StartEncoding();
isRecording = true;
return true;
}
public void StopRecording()
{
LiveJob.StopEncoding();
isRecording = false;
}
}
}
捕获.xaml.cs:
using Microsoft.Expression.Encoder.Devices;
using Microsoft.Expression.Encoder.Live;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Project
{
/// <summary>
/// Interaction logic for Capture.xaml
/// </summary>
public partial class Capture : Window
{
public WebCam webcam { get; set; }
public Capture()
{
InitializeComponent();
webcam = new WebCam();
}
public Capture(EncoderDevice d)
: this()
{
webcam.SelectedVideoDevice = d;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
webcam.StartRecording();
btnStart.IsEnabled = false;
btnStop.IsEnabled = true;
btnSnapshot.IsEnabled = false;
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
if (webcam.isRecording)
{
webcam.StopRecording();
btnStop.IsEnabled = false;
btnStart.IsEnabled = true;
btnSnapshot.IsEnabled = true;
}
}
private void btnSnapshot_Click(object sender, RoutedEventArgs e)
{
var md = new MediaDetails();
md.Owner = this;
md.ShowDialog();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
try
{
//WebcamPanel.Size = new System.Drawing.Size(1280, 960);
webcam.StartWebcam();
webcam.LiveDeviceSource.PreviewWindow =
new PreviewWindow(new HandleRef(WebcamPanel, WebcamPanel.Handle));
btnStart.IsEnabled = true;
btnSnapshot.IsEnabled = true;
}
catch
{
this.Close();
}
}
private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
if (webcam.isRecording)
webcam.StopRecording();
if (webcam.isConnected)
webcam.StopWebcam();
}
}
}
捕获.xaml:
<Window x:Class="Project.Capture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Capture" SizeToContent="WidthAndHeight" Loaded="Window_Loaded_1" Closing="Window_Closing_1">
<Grid Margin="0,10,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition />
</Grid.RowDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button x:Name="btnStart" Content="Start" Padding="5" Margin="10,0,0,0" Background="#FFDDDDDD" Click="btnStart_Click" IsEnabled="False" />
<Button Grid.Column="1" x:Name="btnStop" Content="Stop" Margin="10,0,0,0" Padding="5" Click="btnStop_Click" IsEnabled="False" />
<Button Grid.Column="2" x:Name="btnSnapshot" Content="Grab" Margin="10,0,0,0" Padding="5" Click="btnSnapshot_Click" IsEnabled="False"/>
</Grid>
<WindowsFormsHost Grid.Row="1" Margin="10,10,0,0" Name="WinFormHost" Background="{x:Null}" Width="800" Height="600" HorizontalAlignment="Left" VerticalAlignment="Top">
<wf:Panel x:Name="WebcamPanel" />
</WindowsFormsHost>
</Grid>
</Window>