How-to: 利用Web Camera模拟Windows Phone 7的重力加速度传感器
来源:互联网 发布:部署网站先要买域名 编辑:程序博客网 时间:2024/05/01 19:28
引言
目前,对于我们国内大多数的Windows Phone 7开发者来说,模拟器是调试程序的唯一选择。因此,和硬件相关的一些功能就没有办法进行测试。加速度传感器是Windows Phone 7中一个基本的传感器,很多应用软件和游戏软件都需要用到它,因此,使用其他手段来模拟重力加速度传感器是一个十分有用的尝试。
线索
在今年8月,Windows Phone 7开发工具还是Beta版本的时候,山羊胡大叔的Blog《给你的Windows Phone 7模拟器加入GPS和加速度传感器模拟功能 》中,已经给出了一种途径,其方法是:使用一台真实的HD2来获取加速度传感器的数据,然后把这些数据再传回WCF服务;同时Windows Phone 7的应用程序通过这个WCF服务来获取加速度传感器的值。由于手头没有HD2设备,所以就无法去尝试和验证这个方法。
两个月之后,距离Windows Phone 7正式版本的开发工具发布后不久,Charles Petzold给出了他的电子书《Programming Windows Phone 7》,提供广大开发者们免费下载。他在前言中就提到了如何在Windows Phone 7模拟器中实现加速度传感器的方法。本文根据Charles Petzold给出的提示信息进行了验证,试想一下,用一个摄像头就可以模拟加速度传感器,真是挺有趣的。
准备工作
1. 安装Windows Phone 7的开发工具,这个我就不再啰嗦了。
2. 网络摄像头一个,普通的就可以。
3. 去Codeplex网站下载开源项目AccelKit,网址为:http://accelkit.codeplex.com/ 。
4. 解压AccelKit以后,里面包括EmuClasses、Executable、src这三个文件夹和Print_This_Cutout这个pdf文件。EmuClasses文件夹中的文件可以引入到我们的应用程序中,实现加速度传感器数值的读取;Executable文件夹中的文件用来运行在桌面PC上,控制摄像头,并保存数据;src中的文件是对应的控制摄像头程序的源程序。
5. 将Print_This_Cutout.pdf文件打印,并用剪刀剪裁其中一个Windows Phone 7的面板,将其固定于一块板子上。
开始测试
1. 启动Executable文件夹中的accelKit.exe文件,开启摄像头。
2. 开启Windows Phone 7模拟器,运行带有加速度传感器的应用程序。
3. 在摄像头前晃动带有Windows Phone 7面板的板子,检验效果。
实现原理
摄像头对捕捉到的图像进行分析,识别Windows Phone 7面板的移动加速度,按照一定的时间间隔发送给本地的端口:http://127.0.0.1:88/。
我们在写Windows Phone 7的应用程序时,利用WebClient来获取88端口的数据:
..
WebClient wc;
wc = new WebClient();
wc.AllowReadStreamBuffering = false;
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.DownloadStringAsync(new Uri(http://127.0.0.1:88/));
..
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null) { timer.Begin(); return; }
if (e.Result == null) { timer.Begin(); return; }
string[] vc = e.Result.Split(new Char[] {',', ' '});
double x = Convert.ToDouble(vc[0]);
double y = Convert.ToDouble(vc[1]);
double z = Convert.ToDouble(vc[2]);
}
具体可以参考EmuClasses文件中的文件。
注意
1. 目前该方法只实现了重力加速度,没有实现运动方向的加速度,即给出的X/Y/Z这三个值是在垂直方向上的加速度。
2. PC机上运行的摄像头程序会比较耗费CPU资源,建议分辨率不用调太高,可以识别就行。
参考链接:
给你的Windows Phone 7模拟器加入GPS和加速度传感器模拟功能
Programming Windows Phone 7
http://accelkit.codeplex.com/
http://www.tainicom.net/devTools/accelKit/
PS:
这里再附上自己捕捉的桌面视频,左边是摄像头的窗口,右边是Windows Phone 7模拟器:Accelerometer Emulator Sample on Windows Phone 7
- How-to: 利用Web Camera模拟Windows Phone 7的重力加速度传感器
- How-to:利用MultiTouchVista模拟Windows Phone 7的多点触摸
- 重力加速度传感器的启示
- 利用MultiTouchVista模拟Windows Phone 7的多点触摸
- How-to: 创建Windows Phone 7自定义控件
- Windows Phone 传感器使用
- 重力加速度传感器角度输出
- Windows Phone 7 Quick Tip: How to use Bing Maps in XNA games
- 重力加速度陀螺仪传感器MPU-6050
- unity3d 重力加速度传感器控制摄像头
- 重力加速度陀螺仪传感器MPU-…
- how to telnet a phone?
- Windows Phone开发之Accelerometer-加速度传感器
- How to convert from the color camera space to the depth camera space in Kinect For Windows
- 帮同学做了一个Android重力加速度传感器蓝牙控制小车的程序
- Android重力加速度传感器数据去噪方法
- STM32中使用MMA7660重力加速度传感器
- 重力加速度陀螺仪传感器MPU-6050(一)
- Android笔记--环境搭建
- QQ漂流瓶,有点意思
- 好用的正则表达式
- 对话框程序设计之显示对话框
- 奋斗!!!!!
- How-to: 利用Web Camera模拟Windows Phone 7的重力加速度传感器
- windows 和 Ubuntu双系统,每次开机差8个小时。
- stringgrid 使用技巧
- ORACLE物化视图
- MyEclipse8.0下安装org.eclipse.equinox.http插件的方法
- Android下用Properties保存程序配置
- today
- ASP.NET防止重复提交
- 马尔可夫过程