2

エクスプレッション エンコーダー 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 ビデオを引き伸ばすように見えるだけで、録画ビデオは悪く見えます (大きなピクセルとジッター)。私は HD ウェブカメラを使用しており、Windows 8 のカメラ アプリで録画したビデオは非常によく見えます。

webcam.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>
4

0 に答える 0