【发布时间】:2015-07-30 17:57:27
【问题描述】:
谁能说出如何使用 C# 在 Windows Phone 8.1 中切换手电筒? Windows Phone 8.1 中似乎有很多 API 更改,并且 WP 8.0 中的大多数 API 都不受支持。非常感谢您的回答。
【问题讨论】:
标签: c# windows-phone-8.1
谁能说出如何使用 C# 在 Windows Phone 8.1 中切换手电筒? Windows Phone 8.1 中似乎有很多 API 更改,并且 WP 8.0 中的大多数 API 都不受支持。非常感谢您的回答。
【问题讨论】:
标签: c# windows-phone-8.1
我可以像这样在我的 Lumia 820 上使用TorchControl - 首先你必须指定你将使用哪个摄像头 - 默认是前置的(我认为这就是你可能会发现一些问题的原因)并且我们想要后置一 - 带闪光灯的。示例代码:
// edit - I forgot to show GetCameraID:
private static async Task<DeviceInformation> GetCameraID(Windows.Devices.Enumeration.Panel desiredCamera)
{
DeviceInformation deviceID = (await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture))
.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == desiredCamera);
if (deviceID != null) return deviceID;
else throw new Exception(string.Format("Camera of type {0} doesn't exist.", desiredCamera));
}
// init camera
async private void InitCameraBtn_Click(object sender, RoutedEventArgs e)
{
var cameraID = await GetCameraID(Windows.Devices.Enumeration.Panel.Back);
captureManager = new MediaCapture();
await captureManager.InitializeAsync(new MediaCaptureInitializationSettings
{
StreamingCaptureMode = StreamingCaptureMode.Video,
PhotoCaptureSource = PhotoCaptureSource.VideoPreview,
AudioDeviceId = string.Empty,
VideoDeviceId = cameraID.Id
});
}
// then to turn on/off camera
var torch = captureManager.VideoDeviceController.TorchControl;
if (torch.Supported) torch.Enabled = true;
// turn off
if (torch.Supported) torch.Enabled = false;
请注意,最好在完成后致电captureManager.Dispose()。
另请注意,在某些手机上要打开手电筒/手电筒,您需要先开始预览。
【讨论】:
Windows Phone 8.1 是第一个具有用于控制相机灯光的专用 API 的版本。此 API 源自 Windows 8.1,但可用于 Windows Phone 8.1 项目和 Windows Phone Silverlight 8.1 项目。
var mediaDev = new MediaCapture();
await mediaDev.InitializeAsync();
var videoDev = mediaDev.VideoDeviceController;
var tc = videoDev.TorchControl;
if (tc.Supported)
{
if (tc.PowerSupported)
tc.PowerPercent = 100;
tc.Enabled = true;
}
注意: 注意:TorchControl.Supported 在 WP8.1 开发者预览版中的大多数手机上返回 false。预计将在 WP 8.1 发布时通过固件更新修复。在撰写本文时测试过的手机:Lumia 620、822、1020:不工作,Lumia 1520:工作。
【讨论】:
在诺基亚 Lumia 1520 中,您使用 FlashControl 来切换闪光灯而不是 TorchControl。
//to switch OFF flash light
mediacapture.VideoDeviceController.FlashControl.Enabled = false;
//to switch ON flash light
mediacapture.VideoDeviceController.FlashControl.Enabled = true;
【讨论】:
不适用于我的 Lumia 1520。您需要开始视频录制才能让手电筒工作:
var videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
var videoStorageFile = await KnownFolders.VideosLibrary.CreateFileAsync("tempVideo.mp4", CreationCollisionOption.GenerateUniqueName);
await captureManager.StartRecordToStorageFileAsync(videoEncodingProperties, videoStorageFile);
【讨论】:
在我的 Lumia 1520 中。我需要开始视频录制和预览才能让手电筒工作:
await captureManager.StartPreviewAsync();
【讨论】: