zoukankan      html  css  js  c++  java
  • WPF 摄像头拍照技术

    第一步:添加WPFMediaKit.dll 文件到项目中

    第二步:把WPFMediaKit.dll文件引用进来。

      步骤 右击引用—>添加引用—>浏览选项卡—>选择WPFMediaKit.dll文件所在的位置.

    第三步:在窗口顶端加入如下代码(注意不要该意记)就像using一个类样。

      xmlns:wpfmedia="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)
            {
                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;
            }
    
            /// <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;
            }
        
  • 相关阅读:
    ASP.NET 配置log4net启用写错误日志功能
    jQuery formValidator表单验证插件
    jQuery Easy Validate Plugin
    https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
    Request 分别获取具有相同 name 属性表单元素值
    asp.net开源CMS推荐
    客户对账单本月欠款
    AR 客户本月回款
    应收帐款汇总
    采购订单关闭之PL/SQL实现方法
  • 原文地址:https://www.cnblogs.com/qintangtao/p/2984066.html
Copyright © 2011-2022 走看看