如何在 Windows Phone 应用程序中使用相机闪光灯
2012/2/9
使用 Windows Phone OS 7.1,您可以采用编程方式访问设备相机。本主题介绍如何采用编程方式设置相机闪光灯模式。本主题是如何为 Windows Phone 创建基本相机应用程序的继续,并且假定您已在该主题中创建了基本相机项目。
提示: |
|---|
本主题与基本相机示例相对应。若要下载完整的项目,请参阅 Windows Phone 的代码示例。 |
Windows Phone 支持 FlashMode 枚举中的下列闪光灯模式。
闪光灯模式 | 说明 |
|---|---|
关 | 禁用相机闪光灯。 |
开 | 启用相机闪光灯。 |
RedEyeReduction | 启用红眼消除。 |
Auto | 在自动模式下使用相机闪光灯。 |
重要说明: |
|---|
并非所有设备都支持所有闪光灯模式;重要的是在使用之前,调用 IsFlashModeSupported(FlashMode) 方法来确认可用的模式。 |
本节提供在您的应用程序中实现闪光灯模式配置所需的代码。
注意: |
|---|
| 本主题基于 C# 开发;但也提供 Visual Basic 代码。 |
实现相机闪光灯的步骤
打开在如何为 Windows Phone 创建基本相机应用程序中创建的基本相机项目。
在主页 XAML 文件 MainPage.xaml 中,在 StackPanel 元素中添加以下代码,放在名为 ShutterButton 的 Button 元素下面。该代码为相机闪光灯的按钮。
<Button Name="FlashButton" Content="Fl:TBD" Click="changeFlash_Clicked" FontSize="26" FontWeight="ExtraBold" Height="75"/>
打开主页的代码隐藏文件 MainPage.xaml.cs,在 MainPage 类构造函数上面添加以下变量声明:
在 MainPage.xaml.cs 中,向 OnNavigatedTo 方法中添加以下代码,放在 Disable UI 注释下面。
该代码禁用闪光灯按钮。当设备上的相机不可用时使用。
在 MainPage.xaml.cs 中,向 cam_Initialized 方法中添加以下代码,放在 txtDebug 语句下面。
该代码显示 FlashButton 按钮上的当前闪光灯模式。
在 MainPage.xaml.cs 中,向 MainPage 类中添加以下代码。该代码通过每次按按钮时切换到另一种闪光灯模式来实现 changeFlash_Clicked 的事件处理程序。
// Activate a flash mode. // Cycle through flash mode options when the flash button is pressed. private void changeFlash_Clicked(object sender, RoutedEventArgs e) { switch (cam.FlashMode) { case FlashMode.Off: if (cam.IsFlashModeSupported(FlashMode.On)) { // Specify that flash should be used. cam.FlashMode = FlashMode.On; FlashButton.Content = "Fl:On"; currentFlashMode = "Flash mode: On"; } break; case FlashMode.On: if (cam.IsFlashModeSupported(FlashMode.RedEyeReduction)) { // Specify that the red-eye reduction flash should be used. cam.FlashMode = FlashMode.RedEyeReduction; FlashButton.Content = "Fl:RER"; currentFlashMode = "Flash mode: RedEyeReduction"; } else if (cam.IsFlashModeSupported(FlashMode.Auto)) { // If red-eye reduction is not supported, specify automatic mode. cam.FlashMode = FlashMode.Auto; FlashButton.Content = "Fl:Auto"; currentFlashMode = "Flash mode: Auto"; } else { // If automatic is not supported, specify that no flash should be used. cam.FlashMode = FlashMode.Off; FlashButton.Content = "Fl:Off"; currentFlashMode = "Flash mode: Off"; } break; case FlashMode.RedEyeReduction: if (cam.IsFlashModeSupported(FlashMode.Auto)) { // Specify that the flash should be used in the automatic mode. cam.FlashMode = FlashMode.Auto; FlashButton.Content = "Fl:Auto"; currentFlashMode = "Flash mode: Auto"; } else { // If automatic is not supported, specify that no flash should be used. cam.FlashMode = FlashMode.Off; FlashButton.Content = "Fl:Off"; currentFlashMode = "Flash mode: Off"; } break; case FlashMode.Auto: if (cam.IsFlashModeSupported(FlashMode.Off)) { // Specify that no flash should be used. cam.FlashMode = FlashMode.Off; FlashButton.Content = "Fl:Off"; currentFlashMode = "Flash mode: Off"; } break; } // Display current flash mode. this.Dispatcher.BeginInvoke(delegate() { txtDebug.Text = currentFlashMode; }); }在设备上,通过选择“调试 | 启动调试”菜单命令来运行应用程序。
提示:
重要说明: