如何調整相機或麥克風設定 (HTML)
[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]
您可以調整相機或麥克風上的各種設定,例如亮度、對比、焦點 (相機) 或音量 (麥克風)。 在 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 物件。這個物件的屬性 (例如亮度、對比或焦點) 都會傳回 MediaDeviceControl 物件,含有可以傳回 MediaDeviceControlCapabilities 物件的 Capabilities 屬性。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 物件當作參數,啟動用來調整相機設定的對話方塊。