Silverlight在Web模式下进行Camera视频截图

本文介绍如何利用CaptureDeviceConfiguration类管理和访问客户端系统的摄像头设备,包括获取默认视频捕获设备、请求设备访问权限及实现摄像头截图功能。通过XAML与C#代码结合,演示了从检测摄像头到实现不同格式的截图操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先要先了解几个类:

1) VideoCaptureDevice类: 描述视频捕获设备(如网络摄相机)需要的和支持的视频格式信息。

2) CaptureDeviceConfiguration类: 表示一个帮助器类,该类用来获取有关可用的捕获设备(音频或视频设备)的信息,然后用来请求访问可用设备的捕获操作的客户端用户权限。  他的方法:

公共方法静态成员受 Silverlight for Windows Phone 支持GetDefaultVideoCaptureDevice

返回表示客户端系统上的默认视频捕获设备的 VideoCaptureDevice 对象。

 

公共方法静态成员受 Silverlight for Windows Phone 支持RequestDeviceAccess请求访问客户端系统上提供的所有可能的捕获设备(音频或视频设备)。如果用户根据他们对访问请求对话框中的提示做出的响应授予设备访问权限,或者在某些情况下自动准许了访问请求,则为true如果用户未授予访问权限或者由于其他因素而拒绝了访问请求,则为false

XAML:

<UserControl
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	x:Class="simpledemowebcam.MainPage"
	Width="640" Height="480">

	<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>


xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;



namespace simpledemowebcam
{
	public partial class MainPage : UserControl
	{
		bool _isWorked = false;
        CaptureSource _webCamSource;

		public MainPage()
		{
			// Required to initialize variables
			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);
                //设置如何拉伸 UniformToFill:对 TileBrush 内容进行缩放,从而可以完全填充输出区域,但保持其原始纵横比。
                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)
            {
                //启动一个异步图像捕获请求。通过处理此 CaptureSource 上的 CaptureImageCompleted 事件来检索返回的图像。 
                _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);
        }


	}
}


 源代码: http://download.youkuaiyun.com/detail/eric_k1m/5820199

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值