Hola a todos, aqui el ejemplo de como activar la webcam a traves de Silverlight.
Yo como no poseo webcam he descargado SplitCam para simular una webcam y poder realizar este ejemplo.
Bien vamos a el ejemplo:
Bien crea un nuevo proyecto de silverlight5 con framework 4.5.
y en el mainpage, dibuja un rectangulo y añade un control videobrush.
de la manera que quede algo asi.
1 2 3 4 5 |
<Rectangle Height="470" VerticalAlignment="Top" Margin="93,49,146,0" Grid.Column="2"> <Rectangle.Fill> <VideoBrush x:Name="VideoCam" Stretch="Uniform"/> </Rectangle.Fill> </Rectangle> |
Luego añade un boton y en content escribe algo asi como “activar webcam” y a el boton name yo lo he llamado “bb”
Crea una variable capturesouce como publico y llamala webcam
1 |
Public WebCam As New CaptureSource |
Bien ahora en el evento clic del boton añade esto:
1 2 3 4 5 6 7 8 9 10 11 12 |
Private Sub bb_Click(sender As Object, e As RoutedEventArgs) Handles bb.Click If CaptureDeviceConfiguration.AllowedDeviceAccess = False Then CaptureDeviceConfiguration.RequestDeviceAccess() WebCam.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice VideoCam.SetSource(WebCam) WebCam.Start() Else WebCam.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice VideoCam.SetSource(WebCam) WebCam.Start() End If End Sub |
Bien como ves en el codigo una vez que pulsemos el boton si no tenemos acceso a la webcam nos sacara el mensaje de permitir el acceso de silverlight,
una vez le digamos si nos activa la webcam
Como siempre todo el codigo seguido.
Codigo XAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<UserControl 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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="WDOC4.MainPage" mc:Ignorable="d" d:DesignHeight="800.955" d:DesignWidth="600.06"> <Grid x:Name="LayoutRoot" Background="White" Margin="0,0,-337,0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="0*"/> <ColumnDefinition Width="9*"/> <ColumnDefinition Width="373*"/> </Grid.ColumnDefinitions> <Rectangle Height="470" VerticalAlignment="Top" Margin="93,49,146,0" Grid.Column="2"> <Rectangle.Fill> <VideoBrush x:Name="VideoCam" Stretch="Uniform"/> </Rectangle.Fill> </Rectangle> <Button x:Name="bb" Content="Activar WebCam" HorizontalAlignment="Left" Margin="93,22,0,0" VerticalAlignment="Top" Width="282" Height="22" Grid.Column="2"/> </Grid> </UserControl> |
Codigo VB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Partial Public Class MainPage Inherits UserControl Public Sub New() InitializeComponent() End Sub Public WebCam As New CaptureSource Private Sub bb_Click(sender As Object, e As RoutedEventArgs) Handles bb.Click If CaptureDeviceConfiguration.AllowedDeviceAccess = False Then CaptureDeviceConfiguration.RequestDeviceAccess() WebCam.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice VideoCam.SetSource(WebCam) WebCam.Start() Else WebCam.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice VideoCam.SetSource(WebCam) WebCam.Start() End If End Sub End Class |
Un saludo a todos, y ya saben comenten y siguenos en twitter Facebook y Google+