2010-11-24 146 views

回答

13

這現在可以在Windows Phone OS 7.1 SDK中使用。

這裏的鏈接到MSDN文章:Access Camera API on WP7

PhotoCamera cam = new Microsoft.Devices.PhotoCamera(CameraType.Primary); 
cam.FlashMode = FlashMode.On; 
2

這不能在當前的SDK中以編程方式完成。

用戶在手機上獨佔控制此功能。

+1

儘管像HTC原始設備製造商能夠爲他們被允許在手機更多的訪問要做到這一點,他們的手電筒應用程序使用這種能力,其他人不能(雖然這將是很好)。 – RoguePlanetoid 2010-11-25 13:03:25

0

1)在主頁XAML文件MainPage.xaml中,添加以下代碼中的StackPanel元素,名爲ShutterButton按鈕元素下面。此代碼是相機閃光燈的按鈕。

<Button Name="FlashButton" Content="Fl:TBD" Click="changeFlash_Clicked" FontSize="26" FontWeight="ExtraBold" Height="75"/> 

2)打開代碼隱藏文件的主網頁,MainPage.xaml.cs中,並添加MainPage類構造函數上面的以下變量聲明:

//保存當前閃光模式。

private string currentFlashMode; 

3)在MainPage.xaml.cs中,將下面的代碼添加到OnNavigatedTo方法中,在Disable UI註釋的下方。

FlashButton.IsEnabled = false; 

4)在MainPage.xaml.cs中,下面的代碼添加到cam_Initialized方法,只是txtDebug聲明如下:

//設置閃光燈按鈕上的文字。

FlashButton.Content = "Fl:" + cam.FlashMode.ToString(); 

此代碼顯示FlashButton按鈕上的當前Flash模式。

5)在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; 
    }); 
}