How-to: 利用Web Camera模拟Windows Phone 7的重力加速度传感器

简介:

引言

        目前,对于我们国内大多数的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面板的板子,检验效果。

1

实现原理

        摄像头对捕捉到的图像进行分析,识别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


本文转自施炯博客园博客,原文链接:http://www.cnblogs.com/dearsj001/archive/2010/11/26/AccelerometerEmulatorForWP7.html,如需转载请自行联系原作者


相关文章
|
2月前
|
开发框架 监控 安全
Windows Defender 导致 Web IIS 服务异常停止排查
某日凌晨IIS服务异常停止,经查为Windows Defender安全补丁KB2267602触发引擎更新,导致系统资源波动,进而引发应用池回收。确认非人为操作,系统无重启。通过分析日志与监控,定位原因为Defender更新后扫描加重负载。解决方案:将IIS及.NET相关路径添加至Defender排除列表,避免业务影响。
340 116
|
2月前
|
Java 测试技术 网络安全
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
98 0
Burp Suite Professional 2025.10 for Windows x64 - 领先的 Web 渗透测试软件
|
5月前
|
安全 Linux iOS开发
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
499 0
Burp Suite Professional 2025.7 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
7月前
|
人工智能 安全 网络安全
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
354 4
Burp Suite Professional 2025.5 for Windows x64 - 领先的 Web 渗透测试软件
|
7月前
|
安全 Devops 测试技术
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
146 0
AppSpider 7.5.018 for Windows - Web 应用程序安全测试
|
10月前
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
454 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
10月前
|
安全 JavaScript Java
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
165 12
AppSpider Pro 7.5.015 for Windows - Web 应用程序安全测试
|
9月前
|
自然语言处理 安全 测试技术
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
568 0
HCL AppScan Standard 10.8.0 (Windows) - Web 应用程序安全测试
|
安全 网络协议 网络安全
Windows Server 2003 Web服务器搭建
Windows Server 2003 Web服务器搭建
175 1
|
网络协议 Windows
Windows Server 2019 Web服务器搭建
Windows Server 2019 Web服务器搭建
595 0

热门文章

最新文章