[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
你可以调整相机或麦克风上的各种设置,如亮度、对比度、对焦(位于相机上)或音量(位于麦克风上)。 在 Windows.Media.Capture API 中,通过检索 Windows.Media.Devices.VideoDeviceController 或 Windows.Media.Devices.AudioDeviceController 对象并设置该对象的属性来完成该操作。
先决条件
- 你应当熟悉 JavaScript。
- 你正使用的电脑配备相机。
说明
步骤 1: 检索视频或音频设备控制器
Windows.Media.Capture.MediaCapture 对象包含 VideoDeviceController 和 AudioDeviceController 属性,这些属性允许你检索 Windows.Media.Devices.VideoDeviceController 或 Windows.Media.Devices.AudioDeviceController 对象以控制视频或音频设备的设置。
// Create the media capture object.
var oMediaCapture = new Windows.Media.Capture.MediaCapture();
oMediaCapture.initializeAsync();
// Retrieve a video device controller.
var videoDeviceController = oMediaCapture.videoDeviceController;
// Retrieve an audio device controller.
var audioDeviceController = oMediaCapture.audioDeviceController;
步骤 2: 设置视频设备控制器的属性
VideoDeviceController 属性返回 Windows.Media.Devices.VideoDeviceController 对象。该对象的属性,如亮度、对比度或焦距,各自返回带 Capabilities 属性的 MediaDeviceControl 对象,该属性可返回 MediaDeviceControlCapabilities 对象。MediaDeviceControlCapabilities 对象拥有允许你确定相机是否支持某属性的属性和方法以及属性的最小值和最大值,并允许你获取和设置属性值。
以下示例检索称为 brightnessCapabilities 的 MediaDeviceControlCapabilities 对象,可进行视频相机的亮度,并用于增加亮度级别。
// Retrieve the brightness capabilites of the video camera
var brightnessCapabilities = videoDeviceController.brightness.capabilities;
//
// Determine if the video camera supports adjustment of the brightness setting.
//
if (brightnessCapabilities.supported)
{
var brightness;
//
// Retrieve the current brightness value.
//
if (videoDeviceController.brightness.tryGetValue( brightness ))
{
//
// Get the minimum, maximum and step size for the brightness value.
//
var min = brightnessCapabilities.min;
var max = brightnessCapabilities.max;
var step = brightnessCapabilities.step;
//
// Increase the brightness value by one step as long as the new value is less than or equal to the maximum.
//
if( (brightness + step) <= max )
{
if( brightnessCapabilities.trySetValue( brightness + step ) )
{
// The brightness was successfully increased by one step.
}
else
{
// The brightness value couldn't be increased.
}
}
else
{
// The brightness value is greater than the maximum.
}
}
else
{
// The brightness value couldn't be retrieved.
}
}
else
{
// Setting the brightness value is not supported on this camera.
}
步骤 3: 设置音频设备控制器的属性
AudioDeviceController 属性返回 Windows.Media.Devices.AudioDeviceController 对象。此对象的属性,如 Muted 和 VolumePercent,可用于直接调整麦克风的设置。
以下示例显示了如何使用 AudioDeviceController 对象对麦克风设置静音/取消静音,以及增加麦克风音量。
// Mute the microphone.
audioDeviceController.muted = true;
// Un-mute the microphone.
audioDeviceController.muted = false;
// Get the current volume setting.
var currentVolume = audioDeviceController.volumePercent;
// Increase the volume by 10 percent, if possible.
if (currentVolume <= 90) {
audioDeviceController.volumePercent = (currentVolume + 10);
}
步骤 4: 完整的示例
以下示例显示了在将视频捕获到文件中时,如何调整相机和麦克风设置。此示例中的入口点为 StartMediaCaptureSession 函数。那么,此功能可调用 SetDevices 功能,其中,在捕获会话开始之前可调整视频相机亮度和麦克风音量。有关如何创建捕获会话的示例,请参阅 快速入门:使用 MediaCapture API 捕获视频。
//
// Initialize MediaCapture global object
//
var oMediaCapture;
function startMediaCaptureSession() {
oMediaCapture = new Windows.Media.Capture.MediaCapture();
oMediaCapture.initializeAsync().then (function (result) {
// Set the audio and video.
setDevices();
}, errorHandler);
}
function setDevices()
{
//
// Obtain Video and Audio device controllers.
//
var videoDeviceController = oMediaCapture.videoDeviceController;
var audioDeviceController = oMediaCapture.audioDeviceController;
//
// Adjust Video and Audio device settings.
//
//
// Increase the brightness value by one step as long as the new value is less than or equal to the maximum.
//
var brightness;
var brightnessCapabilities = videoDeviceController.brightness.capabilities;
brightness = videoDeviceController.brightness.tryGetValue();
if( (brightness.value + brightnessCapabilities.step) <= brightnessCapabilities.max )
{
if (videoDeviceController.brightness.trySetValue(brightness + brightnessCapabilities.step))
{
// The brightness value was successfully increased by one step.
}
else
{
// The brightness value could not be increased.
}
}
else
{
// The new brightness value would be greater than the maximum value.
}
//
// Increase the microphone volume by 10 percent if possible.
//
var increase = 10;
var currentVolume = audioDeviceController.volumePercent;
if (currentVolume + increase <= 100) {
audioDeviceController.volumePercent += increase;
}
}
备注
你还可通过调用 Windows.Media.Capture.CameraOptionsUI.Show 并传递 MediaCapture 对象作为参数来启动调整相机设置的对话框。