2013-06-23 25 views
6

OK, sự cố của tôi khá đơn giản.Bật/Tắt Flash

Tôi đã bật đèn flash Bật (và giữ nút Bật).

Tuy nhiên, tôi vẫn không chắc chắn cách tắt (lol).

Dưới đây là mã của tôi:

var sensorLocation = CameraSensorLocation.Back; 

try 
{ 
    // get the AudioViceoCaptureDevice 
    var avDevice = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, 
     AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First()); 

    // turn flashlight on 
    var supportedCameraModes = AudioVideoCaptureDevice 
     .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode); 
    if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On)) 
    { 
     avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); 

     // set flash power to maxinum 
     avDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, 
      AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max); 
    } 
    else 
    { 
     turnWhiteScreen(true); 
    } 

} 
catch (Exception ex) 
{ 
    // Flashlight isn't supported on this device, instead show a White Screen as the flash light 
    turnWhiteScreen(true); 
} 

Bất kỳ ý tưởng?


P.S.

  • tôi tưởng tượng rằng chuyển đổi .on s để .off s có thể làm việc, nhưng nó không.
  • này đã được thử nghiệm trên HTC 8S và Lumia 820.

Trả lời

10

Dường như bạn không thể lấy lại thiết bị thu thập hai lần (tôi không chắc chắn lý do tại sao), vì vậy bạn nên lưu nó trong một tài sản :

protected AudioVideoCaptureDevice Device { get; set; } 

private async void ButtonTurnOn_Click(object sender, RoutedEventArgs e) 
{ 
    var sensorLocation = CameraSensorLocation.Back; 

    try 
    { 
     if (this.Device == null) 
     { 
      // get the AudioViceoCaptureDevice 
      this.Device = await AudioVideoCaptureDevice.OpenAsync(sensorLocation, 
      AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensorLocation).First()); 
     } 

     // turn flashlight on 
     var supportedCameraModes = AudioVideoCaptureDevice 
      .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode); 
     if (supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.On)) 
     { 
      this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); 

      // set flash power to maxinum 
      this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchPower, 
       AudioVideoCaptureDevice.GetSupportedPropertyRange(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchPower).Max); 
     } 
     else 
     { 
      turnWhiteScreen(true); 
     } 

    } 
    catch (Exception ex) 
    { 
     // Flashlight isn't supported on this device, instead show a White Screen as the flash light 
     turnWhiteScreen(true); 
    } 
} 

Sau đó, để tắt nó đi:

private void ButtonTurnOff_Click(object sender, RoutedEventArgs e) 
{ 
    var sensorLocation = CameraSensorLocation.Back; 

    try 
    { 
     // turn flashlight on 
     var supportedCameraModes = AudioVideoCaptureDevice 
      .GetSupportedPropertyValues(sensorLocation, KnownCameraAudioVideoProperties.VideoTorchMode); 
     if (this.Device != null && supportedCameraModes.ToList().Contains((UInt32)VideoTorchMode.Off)) 
     { 
      this.Device.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off); 
     } 
     else 
     { 
      turnWhiteScreen(false); 
     } 
    } 
    catch (Exception ex) 
    { 
     // Flashlight isn't supported on this device, instead show a White Screen as the flash light 
     turnWhiteScreen(false); 
    } 
} 
+0

Vâng, đó là tuyệt vời - và đơn giản như vậy! ** Nó hoạt động **! Cảm ơn rất nhiều, bạn thân! ;-) –

0

Hãy thử điều này một

private static VideoTorchMode _videoTorchMode = VideoTorchMode.Off; 
private AudioVideoCaptureDevice _videoRecordingDevice; 

Kiểm tra đèn pin có trong thiết bị.

private async void CheckTorch() { 
    if(AudioVideoCaptureDevice.AvailableSensorLocations.Contains(CameraSensorLocation.Back) && 
     AudioVideoCaptureDevice.GetSupportedPropertyValues(CameraSensorLocation.Back, KnownCameraAudioVideoProperties.VideoTorchMode).ToList().Contains((UInt32)VideoTorchMode.On)) { 
    var temp = AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back)[0]; 
    var resolution = new Windows.Foundation.Size(temp .Width, temp .Height); 
    _videoRecordingDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back, resolution); 
    } 
    else 
    MessageBox.Show("Your device does not support torch"); 
} 

Để thay đổi trạng thái ngọn đuốc

private void SetTorchMode(){ 
    try { 
    if (BackgroundHandler.Instance.IsBackTorchExist) { 
     if (_videoTorchMode == VideoTorchMode.Off) { 
      _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.On); 
      _videoTorchMode = VideoTorchMode.On; 
     } 
     else { 
      _videoRecordingDevice.SetProperty(KnownCameraAudioVideoProperties.VideoTorchMode, VideoTorchMode.Off); 
      _videoTorchMode = VideoTorchMode.Off;          
     } 
     } 
    } 
    catch (Exception ex){ } 
}