×
Namespaces

Variants
Actions
Revision as of 05:29, 1 July 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

移植Android相机检测代码到WP7

From Nokia Developer Wiki
Jump to: navigation, search
WP Metro Icon Multimedia.png
WP Metro Icon Porting.png
SignpostIcon XAML 40.png
SignpostIcon WP7 70px.png
Article Metadata

兼容于
文章
翻译:
Hazel Xie
最后由 hamishwillee 在 01 Jul 2013 编辑

本文介绍如何移植Android相机检测代码到Windows Phone7

Contents

介绍

检测相机在设备中是否可用对要求启动相机的应用程序来说是很有帮助的。 如果我们没有进行检测其是否存在就打开相机,应用程序有很大的几率会崩溃。因此,提前检测设备中什么类型的相机(主相机,副相机或者两者都有)可用总是比较好的。

在Android中检测相机的存在

在Android中,我们使用PackageManager 类来检测手机是否支持相机。该PackageManager 类提供了一个函数 hasSystemFeature来检测在设备中一个特定的特性是否可用。这个函数接受一个表示该特性的一些我们需要的信息的字符串类型的参数。要检测相机的存在,我们将使用的参数字符串是FEATURE_CAMERA。 在Android中检测相机存在的代码:

  • 首先我们创建一个PackageManager的实例
PackageManager packagemanager = this.Application.getContext().getPackageManager();
  • 下一步,我们调用DETECT_CAMERA() 方法
if (packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA) && packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT))
{
Log.i("camera", "Both Primary and Secondary Camera's are Available.");
}
if (packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA))
{
Log.i("camera", "Only Primary Camera is available.");
}
else if (packagemanager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT))
{
Log.i("camera", "Only Secondary Camera is available.");
}
else
{
Log.i("camera", "No Camera available on this Device.");
}

在Windows Phone7中检测相机的存在

要在设备中检测相机的存在,我们使用PhotoCamera API。在Android中,PhotoCamera API 提供了一个接受表示相机类型的字符串类型变量的函数 IsCameraTypeSupported 在Windows Phone7中检测相机是否存在的代码:

  • 首先我们创建一个PhoneCamera的实例
private PhotoCamera _Camera;
  • 下一步,我们调用DETECT_CAMERA()方法
 public void DETECT_CAMERA()
{
if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing) && PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
MessageBox.Show("Both Primary and Secondary Camera's are available.");
}
else if (PhotoCamera.IsCameraTypeSupported(CameraType.FrontFacing))
{
MessageBox.Show("Only Secondary Camera is available.");
}
else if (PhotoCamera.IsCameraTypeSupported(CameraType.Primary))
{
MessageBox.Show("Only Primary Camera is available.");
}
else
{
MessageBox.Show("No Camera available on this Device.");
}
}

总结

现在我们已经成功移植Android的相机检测代码到Windows Phone 7

This page was last modified on 1 July 2013, at 05:29.
141 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×