Silverlight OOB模式中显示摄像头画面
来源:互联网 发布:php常用扩展有哪些 编辑:程序博客网 时间:2024/03/29 17:49
启动WebCam需要首先了解一下几个类:
1) CaptureSource类:提供用来处理来自关联捕获设备的特定音频或视频捕获的方法。
通过使用 CaptureSource API,您可启用下列方案:
通过调用 CaptureImageAsync 抓取视频帧,并将其转换为静态图像,并处理CaptureImageCompleted 以获取结果。
通过调用 Start 和Stop 来将完整视频捕获到此CaptureSource。然后,将此CaptureSource 用作SetSource 的值,以播放视频。
CaptureSource 不启用对原始音频或视频的访问。若要访问数据,必须提供AudioSink.OnSamples 或VideoSink.OnSample 重写。将Start 和Stop 用于原始格式方案启动和停止该捕获,以便接收器可以处理生成的流。
2)CaptureDeviceConfiguration类:表示一个帮助器类,该类用来获取有关可用的捕获设备(音频或视频设备)的信息,然后用来请求访问可用设备的捕获操作的客户端用户权限。
访问可用设备的捕获操作的客户端用户权限由域中的站点限定范围,而且必须每个会话启用一次。
如果有多个设备可用,用户可以选择其中一个设备以用作 Silverlight 配置对话(“网络摄像机/麦克风”选项卡)的一部分。在配置 UI 中,音频设备和视频设备单独列表,且每个都可以有一个默认值。如果设备在配置 UI 中不可用,那么没有CaptureDevice 实例表示它。
在某些系统上,不会报告音频设备,除非插入并启用了一个麦克风;声卡存在但没有为其插入麦克风将不会以列表中的音频捕获设备的形式显示。
3) VideoBrush类:使用视频内容绘制一个区域。
VideoBrush 概述 中记录了有关如何使用VideoBrush 的详细演练。
VideoBrush 是一种类似于LinearGradientBrush 或ImageBrush 的Brush 对象。但是,该对象使用视频内容来绘制区域,而不是使用纯色、渐变或图像。这些视频内容通过MediaElement 提供。与其他画笔类型相同,可以使用VideoBrush 来绘制形状的Fill,例如Path 的Rectangle 或几何图形内容、Canvas 的Background、TextBlock 或 Run 的Foreground。
若要使用 VideoBrush,可以创建MediaElement,将VideoBrush 应用于要绘制的对象,然后将 VideoBrush 对象的SourceName 属性设置为您创建的MediaElement 的名称。
Silverlight 4 引入用于网络摄像机方案的 SetSource(CaptureSource) 的重载。对于这种情况,您应使用CaptureSource 为VideoBrush 设置源视频。有关获取CaptureSource 和网络摄像机方案的更多信息,请参见CaptureSource 和VideoSink。
MainPage.xaml: 这里要使用一个自制的空间,就是点击X后,关闭当前OOB窗口。
<UserControlxmlns="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:TutorialCamDemo" mc:Ignorable="d"x:Class="TutorialCamDemo.MainPage"Width="145" Height="138"><UserControl.Resources><Style x:Key="GrayGradientStyle" TargetType="Button"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Grid><VisualStateManager.VisualStateGroups><VisualStateGroup x:Name="CommonStates"><VisualStateGroup.Transitions><VisualTransition GeneratedDuration="0:0:0.3"/></VisualStateGroup.Transitions><VisualState x:Name="Disabled"/><VisualState x:Name="Normal"/><VisualState x:Name="MouseOver"><Storyboard><ColorAnimation Duration="0" To="#FF5E626E" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[0].(GradientStop.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/><ColorAnimation Duration="0" To="#FF5C606A" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/><ColorAnimation Duration="0" To="#FF939399" Storyboard.TargetProperty="(Shape.Fill).(GradientBrush.GradientStops)[2].(GradientStop.Color)" Storyboard.TargetName="rectangle" d:IsOptimized="True"/><ColorAnimation Duration="0" To="Black" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock" d:IsOptimized="True"/></Storyboard></VisualState><VisualState x:Name="Pressed"/></VisualStateGroup></VisualStateManager.VisualStateGroups><Rectangle x:Name="rectangle" RadiusY="2" RadiusX="2"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF363941" Offset="0"/><GradientStop Color="#FF262729" Offset="0.183"/><GradientStop Color="#FF141416" Offset="0.839"/><GradientStop Color="#FF1D1F25" Offset="0.949"/></LinearGradientBrush></Rectangle.Fill></Rectangle><Rectangle Margin="3,1" RadiusY="2" RadiusX="2"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="White" Offset="0"/><GradientStop Color="#01FFFFFF" Offset="0.32"/></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock x:Name="textBlock" Margin="0,3,1,1" TextWrapping="Wrap" Text="{TemplateBinding Content}" d:LayoutOverrides="Height" TextAlignment="Center" Foreground="White"/></Grid></ControlTemplate></Setter.Value></Setter></Style></UserControl.Resources><Grid x:Name="LayoutRoot"><Rectangle x:Name="BG" RadiusX="2" RadiusY="2"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF5C606E" Offset="0"/><GradientStop Color="#FF3A3B3E" Offset="0.183"/><GradientStop Color="#FF2F3033" Offset="0.839"/><GradientStop Color="#FF3C3F49" Offset="0.949"/></LinearGradientBrush></Rectangle.Fill></Rectangle><Button x:Name="StartStopBtn" Content="Start Cam" Height="20" Margin="43,0,37,4" Style="{StaticResource GrayGradientStyle}" VerticalAlignment="Bottom" Cursor="Hand"/><Button x:Name="InstallBtn" Content="Install" Height="20" Margin="43,0,37,4" Style="{StaticResource GrayGradientStyle}" VerticalAlignment="Bottom" Cursor="Hand"/><local:CloseUserControl x:Name="CloseBtn" HorizontalAlignment="Right" Height="16" Margin="0,2,8,0" VerticalAlignment="Top" Width="13" Cursor="Hand"/><Rectangle x:Name="CameraCanvas" Margin="9,18,9,27" Fill="Black"/></Grid></UserControl>
关闭按钮 - CloseUserControl.xaml:
<UserControlxmlns="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"x:Class="TutorialCamDemo.CloseUserControl"d:DesignWidth="13" d:DesignHeight="16"><Grid x:Name="LayoutRoot"><Rectangle Margin="0,4,0,2"><Rectangle.Fill><LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF3C3D43" Offset="0.015"/><GradientStop Color="#FF151618" Offset="0.158"/><GradientStop Color="#FF1F2023" Offset="0.842"/><GradientStop Color="#FF373941" Offset="0.955"/></LinearGradientBrush></Rectangle.Fill></Rectangle><TextBlock TextWrapping="Wrap" Text="x" Margin="3,0" Foreground="White" d:LayoutOverrides="Height" IsHitTestVisible="False"/></Grid></UserControl>
MainPage.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;namespace TutorialCamDemo{ public partial class MainPage : UserControl { private VideoBrush MyBrush; private CaptureSource CapSrc = new CaptureSource(); private Boolean VideoIsPlaying = false; public MainPage() { // Required to initialize variables InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { this.MouseLeftButtonDown += new MouseButtonEventHandler(MainPage_MouseLeftButtonDown); CloseBtn.MouseLeftButtonDown += new MouseButtonEventHandler(CloseBtn_MouseLeftButtonDown); if (App.Current.InstallState == InstallState.Installed) { InstallBtn.Visibility = System.Windows.Visibility.Collapsed; } else { InstallBtn.Visibility = System.Windows.Visibility.Visible; } //判断是否有最新版本,如果进行更新 App.Current.CheckAndDownloadUpdateAsync(); App.Current.CheckAndDownloadUpdateCompleted += new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted); //添加安装当前应用的事件 InstallBtn.Click += new RoutedEventHandler(InstallBtn_Click); App.Current.MainWindow.TopMost = true; //处理视频的开始停止事件 StartStopBtn.Click += new RoutedEventHandler(StartStopBtn_Click); } void StartStopBtn_Click(object sender, RoutedEventArgs e) { if (VideoIsPlaying) { //停播工作 VideoIsPlaying = false; CapSrc.Stop(); //把当前画布制成黑背景色 CameraCanvas.Fill = new SolidColorBrush(Colors.Black); MyBrush = null; StartStopBtn.Content = "Start Cam"; } else { //开播工作 //判断是否可以介入摄像头 或者 摄像头没有被其他程序占用 if (CaptureDeviceConfiguration.AllowedDeviceAccess == true || CaptureDeviceConfiguration.RequestDeviceAccess()) { VideoIsPlaying = true; MyBrush = new VideoBrush(); MyBrush.SetSource(CapSrc); CameraCanvas.Fill = MyBrush; CapSrc.Start(); StartStopBtn.Content = "Stop Cam"; } } } void InstallBtn_Click(object sender, RoutedEventArgs e) { App.Current.Install(); } void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e) { if (e.UpdateAvailable) { MessageBox.Show("An update has been installed. Please stop and restart the application to see the updated version."); } } void CloseBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //自定义的控件X,可以关闭当前窗体 App.Current.MainWindow.Close(); } void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { //当前窗口可以被拖放 App.Current.MainWindow.DragMove(); } }}
源代码: http://download.csdn.net/detail/eric_k1m/5819605
- Silverlight OOB模式中显示摄像头画面
- Silverlight OOB 模式关闭窗口
- <二> 摄像头画面显示
- Silverlight 客戶端桌面模式(OOB 模式)
- Silverlight OOB 模式调用其它程序
- Silverlight OOB
- 如何在Silverlight OOB 中打开链接
- 在虚幻4中显示Kinect2.0摄像头的画面
- Silverlight判断当前模式是OOB还是Web
- Silverlight之OOB模式下的一些事
- Silverlight之OOB模式下的一些问题与解答
- Silverlight操作客户端注册表(优化)(OOB模式)
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- Unity3D调用摄像头显示当前拍摄画面
- MFC+OPENCV3.0显示摄像头画面
- Silverlight OOB应用程序详解
- 生活/工作常用技巧
- WebService中webmethod的六个属性
- 参加的活动---百度开放云·创客马拉松:跟手机联动的数字灯泡
- pstools psexec 执行文件
- 黑马程序员--java泛型
- Silverlight OOB模式中显示摄像头画面
- Oracle数据迁移方案
- [cocos2d-x]File文件的IO读写处理
- Android推送方案分析(MQTT/XMPP/GCM)
- java的HashMap和ArrayList比较器
- 细说智能卡加密狗
- 动态展示Google map
- maven初步
- 代理模式及其在spring与struts2中的体现