zoukankan      html  css  js  c++  java
  • silverlight4:摄像头占用状态检测以及二种截屏方法

    状态检测主要包括二个方面:是否安装了摄像头,摄像头是否被其它程序占用

    视频截图有二种方法:一是直接利用CaptureSource类的CaptureImageAsync异步截屏,另一种是直接利用WriteableBitmap截屏幕,二种截屏方法的区别在于,CaptureImageAsync始终截的是视频原始内容,而直接用WriteableBitmap对指定区域截屏时,如果视频上面还有其它控件(比如TextBlock),最终截下的图会有其它内容叠在上面(如下图)
     

    Xaml部分代码:

    <UserControl x:Class="CameraCheck.MainPage"
        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"
        mc:Ignorable="d"
        >
        <Grid x:Name="LayoutRoot" Background="White"> 
            <Grid.RowDefinitions>
                <RowDefinition Height="162"></RowDefinition>
                <RowDefinition Height="190"></RowDefinition>
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0">   
                <Canvas Height="120" Width="160" HorizontalAlignment="Center" x:Name="cVideo" Margin="0,10,0,10">
                    <Rectangle Width="160" Height="120" x:Name="rectVideo" StrokeThickness="1" Stroke="Black"></Rectangle>
                    <TextBlock x:Name="lblStatus" Text="菩提树下的杨过"  Foreground="White" Canvas.Top="50" Width="160" TextAlignment="Center"></TextBlock>
                </Canvas>            
                <StackPanel  Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button Width="80" Height="22" Content="检测摄像头" x:Name="btnCheck" Click="btnCheck_Click"></Button>
                    <Button Width="80" Height="22" Content="视频原始截图" x:Name="btnCapture1" Margin="5,0,0,0" Click="btnCapture1_Click"></Button>
                    <Button Width="80" Height="22" Content="bitmap截图" x:Name="btnCapture2" Margin="5,0,0,0" Click="btnCapture2_Click"></Button>
                </StackPanel>            
            </StackPanel>
            
            <ScrollViewer x:Name="imgList" Height="180" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" Margin="0,10,0,0" Grid.Row="1">
                <StackPanel x:Name="sp" Orientation="Horizontal"></StackPanel>
            </ScrollViewer>
        </Grid>
    </UserControl>
    
    

    后端CS代码:

    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    
    
    namespace CameraCheck
    {
        public partial class MainPage : UserControl
        {
            bool _isWorked = false;
            CaptureSource _webCamSource;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void btnCheck_Click(object sender, RoutedEventArgs e)
            {
                if (_isWorked) { return; }
    
                VideoCaptureDevice webCam = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
    
                if (webCam == null) 
                {
                    lblStatus.Text = "未检测到摄像头设备!";
                    return;
                }
    
                if (CaptureDeviceConfiguration.RequestDeviceAccess())
                {
                    _webCamSource = new CaptureSource();
                    _webCamSource.VideoCaptureDevice = webCam;
                    VideoBrush video = new VideoBrush();
                    video.SetSource(_webCamSource);
                    video.Stretch = Stretch.UniformToFill;               
                    try
                    {
                        _webCamSource.Start();                   
                        this.rectVideo.Fill = video;
                        lblStatus.Text = "摄像头正在工作中...";//测试用
                        _isWorked = true;
                        _webCamSource.CaptureFailed += webCamSource_CaptureFailed;
                        _webCamSource.CaptureImageCompleted += webCamSource_CaptureImageCompleted;
                    }
                    catch 
                    {
                        lblStatus.Text = "摄像头无法使用(可能被占用)";
                    }                
                }
                else 
                {
                    lblStatus.Text = "您不同意在本程序\n中使用摄像头设备.";
                }
            }
    
            /// <summary>
            /// CaptureImageAsync视频原始内容截图
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnCapture1_Click(object sender, RoutedEventArgs e)
            {           
                if (_isWorked)
                {               
                    _webCamSource.CaptureImageAsync();
                }
            }
    
    
            void webCamSource_CaptureImageCompleted(object sender, CaptureImageCompletedEventArgs e)
            {
                WriteableBitmap wb = e.Result;            
                Image image = new Image();
                image.Height = 120;
                image.Margin = new Thickness(5);
                image.Source = wb;
                sp.Children.Add(image);            
            }
    
            void webCamSource_CaptureFailed(object sender, ExceptionRoutedEventArgs e)
            {
                lblStatus.Text = "截屏失败!";
            }
    
            /// <summary>
            /// 利用WriteableBitmap截屏
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnCapture2_Click(object sender, RoutedEventArgs e)
            {
                WriteableBitmap wb = new WriteableBitmap(cVideo, null);
                Image image = new Image();
                image.Height = 120;
                image.Margin = new Thickness(5);
                image.Source = wb;
                sp.Children.Add(image);
            }
    
    
        }
    }
    
    

    另外silverlight 4中使用摄像头时,提示用户是否同意使用摄像头的界面中多出了一个"记住我"的选项

    这个功能本来是想方便用户,不必每次都提示用户选择,但是也有一个副作用:比如第一次选择时,如果您不小心勾中了"Remember my answer"并选择了"No",那么以后系统会直接拒绝使用摄像头设备,而且没有任何提示!

    当然也可以右击,在silverlight选项里去掉这种错误的记忆(如下图)

    但问题是:用户很有可能不知道从哪里进入这个界面,所以我个人觉得如果当silverlight了自动记住了"禁止使用摄像头"时,是否能给个提示?这样会显得更友好一些

    最后,从技术上讲silverlight 4的摄像头还有一个功能不如flash做得贴心,flash中摄像头有activity事件可以用来监听摄像头是否有活动,而silverlight中没有。这在某些领域中很有用,比如监控系统中经常需要判断摄像头对着的是不是一个静止不动的物体。(当然sl中变相的办法可以通过不断截屏然后比较二张图的差异,但这毕竟太麻烦了,不知道正式版中是不是会有所改进)

    作者:菩提树下的杨过
    出处:http://yjmyzz.cnblogs.com
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    如何优雅地使用 Stack Overflow
    Quartz总结
    slf4j-api、slf4j-log4j12以及log4j之间什么关系?
    eclipse 安装 spring boot suite 插件遇到的问题
    Java项目结构总结
    netstat 与 telnet
    微服务架构中的安全认证与鉴权
    git 常用命令
    session 、cookie、token的区别
    List和Set区别
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/1688881.html
Copyright © 2011-2022 走看看