1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Net;
5: using System.Windows;
6: using System.Windows.Controls;
7: using System.Windows.Documents;
8: using System.Windows.Input;
9: using System.Windows.Media;
"> 10: using System.Windows.Media.Animation;
11: using System.Windows.Shapes;
12: using SLARToolKit;
13: using System.Windows.Media.Media3D;
14:
15: namespace SLRealidadeAumentada
16: {
17: public partial class MainPage : UserControl
18: {
19:
20: CaptureSource cpsVideo;
21: CaptureSourceMarkerDetector cmkDetectorPattern;
22:
23: public MainPage()
24: {
25: InitializeComponent();
26: }
27:
28: private void UserControl_Loaded(object sender, RoutedEventArgs e)
29: {
30: // Instancia o objeto que manipula a WebCam
31: cpsVideo = new CaptureSource();
32: // Define qual webcam será o alvo do objeto, no caso a câmera padrão do Sistema
33: cpsVideo.VideoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();
34:
35: //Cria o objeto que recebe os dados do video
36: var vbsVideo = new VideoBrush();
37: vbsVideo.SetSource(cpsVideo);
38: // Preenche o retangulo com os dados capturados do video
39: Viewport.Fill = vbsVideo;
40:
41: // Initialize the o detector
42: cmkDetectorPattern = new CaptureSourceMarkerDetector();
43: // Carrega o pattern que deve ser procurado, ele tem 64 segmentos e 80 milimetros de
44: var patPattern = Marker.LoadFromResource("makesys.pat", 64, 64, 80);
45: // Cria a parspectiva com planos de 1 a 4000
46: cmkDetectorPattern.Initialize(cpsVideo, 1, 4000, patPattern);
47:
48: // Vincula o handler de captura do pattern
49: // O evento é disparado se pelo menos um pattern é identificado na imagem
50: cmkDetectorPattern.MarkersDetected += (senderDelegate, eDelegate) =>
51: {
52: // Cria uma nova tread para manipular a projeção do objeto que está no canvas
53: Dispatcher.BeginInvoke(() =>
54: {
55: // Calcula a matrix de projeção
56: var dreResultado = eDelegate.DetectionResults;
57: if (dreResultado.HasResults)
58: {
59: // Centro do Objeto
60: Matrix3D mtdCentro = Matrix3DFactory.CreateTranslation(-imgMakesys.ActualWidth * 0.5, -imgMakesys.ActualHeight * 0.5, 0);
61: // Troca o eixo Y e diminui a escala pela metade
62: Matrix3D mtdEscala = Matrix3DFactory.CreateScale(0.5, -0.5, 0.5);
63: // Calcula a matriz final de transformação baseada no ponto centro original
64: Matrix3D mtdFinal = mtdCentro * mtdEscala * dreResultado[0].Transformation;
65:
66: // Calcula a projeção utilizando o SLARToolkit
67: Matrix3D mtdViewPort = Matrix3DFactory.CreateViewportTransformation(Viewport.ActualWidth, Viewport.ActualHeight);
68: Matrix3D mtdProjecao = Matrix3DFactory.CreateViewportProjection(mtdFinal, Matrix3D.Identity, cmkDetectorPattern.Projection, mtdViewPort);
69:
70: // Aplicaca a transformação 3D ao objeto final
71: imgMakesys.Projection = new Matrix3DProjection { ProjectionMatrix = mtdProjecao };
72: imgMakesys.Visibility = Visibility.Visible;
73: }
74: });
75: };
76: }
77:
78: private void Button_Click(object sender, RoutedEventArgs e)
79: {
80: // Efetua a requisição de acesso a WebCam
81: if (CaptureDeviceConfiguration.RequestDeviceAccess())
82: {
83: cpsVideo.Start();
84: }
85: }
86: }
87: }