using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Diagnostics;
using Microsoft.Surface.Core;
using System.Windows.Media;
namespace HugeFlow.Surface
{
///
/// 화면을 적외선 스캔하는 클래스
///
public class Scanner
{
#region 프로퍼티
///
/// 스캔 중인지 여부
///
public bool IsScanning { get; private set; }
///
/// 스캔 어플리케이션 윈도우
///
public Window ApplicationWindow { get; private set; }
#endregion 프로퍼티
#region 멤버
private IntPtr _windowHandle;
private TouchTarget _touchTarget;
private Rect _scanArea;
#endregion 멤버
///
/// Application Window를 인자로 소비하는 생성자
///
///
public Scanner(Window window)
{
Debug.Assert(window != null);
ApplicationWindow = window;
WindowInteropHelper wih = new WindowInteropHelper(window);
_windowHandle = wih.Handle;
}
///
/// 스캔 비동기 호출
///
///
public void ScanAsync(Rect scanArea)
{
if (IsScanning == true)
throw new InvalidOperationException("Is scanning...");
_scanArea = scanArea;
IsScanning = true;
// Create a target for surface input.
_touchTarget = new TouchTarget(_windowHandle, EventThreadChoice.OnBackgroundThread);
_touchTarget.EnableInput();
// Enable the normalized raw-image.
_touchTarget.EnableImage(ImageType.Normalized);
// Hook up a callback to get notified when there is a new frame available
_touchTarget.FrameReceived += new EventHandler(_touchTarget_FrameReceived);
}
///
/// 스캔 비동기 호출
///
///
public void ScanAsync(FrameworkElement fe)
{
Rect boundInHostCoordinates = (fe.TransformToVisual(ApplicationWindow).TransformBounds(new Rect()
{
X = 0,
Y = 0,
Width = fe.ActualWidth,
Height = fe.ActualHeight
}));
ScanAsync(boundInHostCoordinates);
}
void _touchTarget_FrameReceived(object sender, FrameReceivedEventArgs e)
{
_touchTarget.FrameReceived -= _touchTarget_FrameReceived;
byte[] normalizedImage = null;
e.UpdateRawImage(
ImageType.Normalized,
normalizedImage,
(int)_scanArea.Left,
(int)_scanArea.Top,
(int)_scanArea.Width,
(int)_scanArea.Height);
OnScanCompleted(normalizedImage);
IsScanning = false;
}
#region 이벤트들
#region ScanCompleted
public event RoutedPropertyChangedEventHandler ScanCompleted;
private void OnScanCompleted(byte[] imageData)
{
if (ScanCompleted == null)
return;
ScanCompleted(this, new RoutedPropertyChangedEventArgs(null, imageData));
}
#endregion ScanCompleted
#endregion 이벤트들
}
}
서피스2 SDK의 샘플 중 XNA로 되어 있는 RawImageVisualizer 소스를 참고해서 WPF용으로 만들어 봤습니다.
잘못된 부분, 보완할 부분 발견하시면 지적을 부탁드립니다.
'NUI > Microsoft PixelSense(Old Surface)' 카테고리의 다른 글
| 서피스 2 : 애플리케이션 수동으로 배포하는 방법 (0) | 2011.12.22 |
|---|---|
| 포스코 IF2011 서피스 2 전시 현장스케치, 포항 (1) | 2011.12.15 |
| 서피스2 : 마이크로소프트-삼성 서피스2 사전주문이 시작되었습니다. (0) | 2011.11.18 |
| 서피스2 :: SurfaceEnvironment 객체 소개 (0) | 2011.09.19 |
| 서피스2 :: WPF앱 ControlBox 아이콘들 (0) | 2011.09.19 |
| 서피스2 :: 앱 쇼케이스 (0) | 2011.09.19 |
| 드디어 마이크로소프트 서피스 2 SDK 공개! (6) | 2011.07.13 |