标签:http io ar os sp for strong 文件 on
今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相。如何解决这个问题呢?
介绍一个开发包给你,MediaKit。论坛里头的人都说好,但是黑兔觉得大家好才是真的好。你不妨试试~
第一步:添加WPFMediaKit.dll 文件到项目中
第二步:把WPFMediaKit.dll文件引用进来。
步骤 右击引用—>添加引用—>浏览选项卡—>选择WPFMediaKit.dll文件所在的位置.
第三步:在窗口顶端加入如下代码(注意不要该意记)就像using一个类样。
xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
<Window x:Class="IXiahe.Photos"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Photos" Height="300" Width="300"
xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
WindowStartupLocation="CenterScreen" Loaded="loaded">
第四步:添加VideoCaptureElement控件 用来显示一个预览的画面 (需要手写,因为工具箱没有这个控件)
<StackPanel>
<ComboBox Name="cb" SelectionChanged="cb_SelectionChanged"/> <!--选摄像头-->
<WPFMediaKit:VideoCaptureElement Height="200" Name="vce"/> <!--预览画面-->
<Button Height="50" x:Name="btnCapture" Content="拍照" Click="btnCapture_Click"/> <!--拍照按钮-->
</StackPanel> 第五步: 接下来就是后台代码
private void Window_Loaded(object sender, RoutedEventArgs e)
{
cb.ItemsSource = MultimediaUtil.VideoInputNames;
if (MultimediaUtil.VideoInputNames.Length > 0)
{
cb.SelectedIndex = 0;//第0个摄像头为默认摄像头
}
else
{
MessageBox.Show("电脑没有安装任何可用摄像头");
}
}
private void cbCameras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
captureElement.VideoCaptureSource = (string)cbCameras.SelectedItem;
}
/// <summary>
/// 拍照
/// </summary>
private void btnCapture_Click(object sender, RoutedEventArgs e)
{
//captureElement. 怎么抓取高清的原始图像
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)captureElement.ActualWidth,
(int)captureElement.ActualHeight,
96, 96, PixelFormats.Default);
//为避免抓不全的情况,需要在Render之前调用Measure、Arrange
//为避免VideoCaptureElement显示不全,需要把
//VideoCaptureElement的Stretch="Fill"
captureElement.Measure(captureElement.RenderSize);
captureElement.Arrange(new Rect(captureElement.RenderSize));
bmp.Render(captureElement);
//这里需要创建一个流以便存储摄像头拍摄到的图片。//当然,可以使文件流,也可以使内存流。
BitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bmp));
encoder.Save(ms);
captureElement.Pause();
}
/// <summary>
/// 重拍
/// </summary>
private void btnanew_Click(object sender, RoutedEventArgs e)
{
captureElement.Play();
}
/// <summary>
/// 确定
/// </summary>
private void btnOK_Click(object sender, RoutedEventArgs e)
{
CaptureData = ms.ToArray();
ms.Dispose();
DialogResult = true;
}标签:http io ar os sp for strong 文件 on
原文地址:http://www.cnblogs.com/mcad/p/4124051.html