10、员工通过摄像头拍照,WPFMediaKit

1、基础框架准备工作

1.1 同员工管理模块 

1.2 使用第三组件WPFMediaKit 下载WPFMediaKit

1.3 添加引用WPFMediaKit

2、员工编辑页面,添加一个button,text为拍照,方法如下:

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            CaptureWindow win = new CaptureWindow();
            if (win.ShowDialog() == true)
            {
                byte[] data = win.CaptureData;
                ShowImg(data);
                Employee employee = (Employee)gridEmployee.DataContext;
                employee.Photo = data;
            }
        }

 


3、、建立CaptureWindow wpf应用程序

3.1 UI

<Window x:Class="HRMSys.UI.SystemMgr.CaptureWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wpfmedia="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
        Title="拍照" Height="300" Width="300" Loaded="Window_Loaded_1">
    <Grid>
        <DockPanel>
            <ComboBox DockPanel.Dock="Top" Height="30" Name="cbCameras" SelectionChanged="cbCameras_SelectionChanged">ComboBox>
            <wpfmedia:VideoCaptureElement Height="200" DockPanel.Dock="Top" Name="captureElement">wpfmedia:VideoCaptureElement>
            <StackPanel Orientation="Horizontal">
                <Button Height="30" DockPanel.Dock="Top" Name="btnCapture" Content="拍照" Click="btnCapture_Click">Button>
                <Button Height="30" DockPanel.Dock="Top" Name="btnOK" Content="确定">Button>
            StackPanel>
        DockPanel>

    Grid>
Window>

 

3.2 交互

namespace HRMSys.UI.SystemMgr
{
    /// 
    /// CaptureWindow.xaml 的交互逻辑
    /// 
    public partial class CaptureWindow : Window
    {
        public CaptureWindow()
        {
            InitializeComponent();
        }
        public byte[] CaptureData { get; set; }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            cbCameras.ItemsSource = MultimediaUtil.VideoInputNames;
            if (MultimediaUtil.VideoInputNames.Length > 0)
            {
                cbCameras.SelectedIndex = 0;//第0个摄像头为默认摄像头
            }
            else
            {
                MessageBox.Show("电脑没有安装任何可用摄像头");
            }
        }

        private void cbCameras_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            captureElement.VideoCaptureSource = (string)cbCameras.SelectedItem;
        }

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            //captureElement. 怎么抓取高清的原始图像
            //能不能抓视频。
            //todo:怎么只抓取一部分
            RenderTargetBitmap bmp = new RenderTargetBitmap(
                (int)captureElement.ActualWidth, (int)captureElement.ActualHeight,
                96, 96, PixelFormats.Default);
            bmp.Render(captureElement);
            BitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Save(ms);
                CaptureData = ms.ToArray();
            }
            //captureElement.Pause();
            //todo:自己完成重拍的代码
            DialogResult = true;
        }
    }
}

 

转载于:https://www.cnblogs.com/xjgby/archive/2013/05/15/3079623.html

你可能感兴趣的:(10、员工通过摄像头拍照,WPFMediaKit)