(五)WPF/C# 使用WPFMediaKit.dll打开USB摄像头:实现拍照/重拍功能

一、实现拍照/重拍功能

参考https://blog.csdn.net/ty_soul/article/details/79615421

https://www.cnblogs.com/mcad/p/4124051.html

http://www.firstsolver.com/wordpress/?p=2757

MainWindow.xaml:

<Window x:Class="WPFtest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFtest"
        mc:Ignorable="d"
        xmlns:WPFMediaKit="clr-namespace:WPFMediaKit.DirectShow.Controls;assembly=WPFMediaKit"
        Title="MainWindow" Height="800" Width="1280   ">
    <Grid x:Name="RootGrid" Width="1280   " MaxHeight="800" MinHeight="520" ClipToBounds="True" >
        <StackPanel>
            <ComboBox Name="cb" SelectionChanged="cb_SelectionChanged"/>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=".2*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <WPFMediaKit:VideoCaptureElement Margin="1,0,0,0" Height="600" Width="800" HorizontalAlignment="Left" Name="vce" Grid.Column="1" />
                <!--<Border Background="Transparent" BorderBrush="#FFA1D2F7" BorderThickness="5" HorizontalAlignment="Left"
                        Margin="185,0,0,0" Height="431" Width="431" CornerRadius="215" Panel.ZIndex="2" Grid.Column="1" />
                <Path Panel.ZIndex="1" Fill="White" VerticalAlignment="Top" Grid.Column="1">
                    <Path.Data>
                        <CombinedGeometry GeometryCombineMode="Exclude">
                            <CombinedGeometry.Geometry1>
                                <RectangleGeometry Rect="0,0 802,602"></RectangleGeometry>
                            </CombinedGeometry.Geometry1>
                            <CombinedGeometry.Geometry2>
                                <EllipseGeometry RadiusX="215" RadiusY="215" Center="401,301"></EllipseGeometry>
                            </CombinedGeometry.Geometry2>
                        </CombinedGeometry>
                    </Path.Data>
                </Path>-->
            </Grid>
            <StackPanel Orientation="Horizontal">
                <Button Margin="20,0,0,0" Height="50" x:Name="btnCapture" Content="拍照" Click="btnCapture_Click"></Button>
                <Button Margin="20,0,0,0" Height="50" x:Name="btnanew" Content="重拍" Click="btnanew_Click" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs: 

using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WPFMediaKit.DirectShow.Controls;


namespace WPFtest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string fullPath;
        public MainWindow()
        {
            InitializeComponent();
            cb.ItemsSource = MultimediaUtil.VideoInputNames;
            if (MultimediaUtil.VideoInputNames.Length > 0)
            {
                cb.SelectedIndex = 0;//第0个摄像头为默认摄像头
            }
            else
            {
                MessageBox.Show("电脑没有安装任何可用摄像头");
                return;
            }
        }

        private void cb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vce.VideoCaptureSource = (string)cb.SelectedItem;
        }

        /// <summary>
        /// 拍照
        /// </summary>

        private void btnCapture_Click(object sender, RoutedEventArgs e)
        {
            // 建立目标渲染图像器,高度为前台控件实显高度,此处不能使用.width或.height属性,否则出现错误。
            // 为了避免图像抓取出现黑边现象,需要对图象进行重新测量及缩放
            RenderTargetBitmap bitmap = new RenderTargetBitmap((int)vce.ActualWidth, (int)vce.ActualHeight, 96, 96, PixelFormats.Default);
            //VideoCaptureElement的Stretch="Fill"
            vce.Measure(vce.RenderSize);
            vce.Arrange(new Rect(vce.RenderSize));
            // 指定图像渲染目标
            bitmap.Render(vce);
            // 建立图像解码器。类型为jpeg
            BitmapEncoder encoder = new JpegBitmapEncoder();
            // 将当前渲染器中渲染位图作为一个位图帧加入解码器,进行解码,取得数据流。
            encoder.Frames.Add(BitmapFrame.Create(bitmap));
            // 建立内存流,将得到解码图像流写入内存流。
            using (MemoryStream stream = new MemoryStream())
            {
                encoder.Save(stream);
                byte[] pics = stream.ToArray(); // 将流以文件形式存储于计算机中。
                string fileName = DateTime.Now.ToString("yyyyMMdd-HHmmss");
                fullPath = System.IO.Path.Combine(@"E:\Photo", fileName + "_cap.jpg");
                //保存图片
                File.WriteAllBytes(fullPath, pics);
            }   // 预览效果暂停。
            vce.Pause();
        }

        /// <summary>
        /// 重拍
        /// </summary>

        private void btnanew_Click(object sender, RoutedEventArgs e)
        {
            File.Delete(fullPath.ToString());
            vce.Play();
        }

    }
}

效果图:

 二、录像功能

 

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我爱AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值