使用 focusMode 约束获取所需的媒体设备

Use the focusMode constraint to get required media device

大家好!

目前,我正在为我的网络应用程序实施二维码扫描器。在多个设备上进行一些测试后,我注意到我必须设置 focusMode 约束才能获得能够自动对焦环境的相机。

我可以通过直接在约束中设置 deviceId 来直接 select 我的调试设备上的相机:

let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        deviceId: "332d34c91861f97ba8f0e11f446da4566a1803539764dd67c1dfe036ef32fd97"
    }
});

我可以调用 stream.getVideoTracks()[0].getCapabilities() 来获取功能。

{
    aspectRatio: {max: 4000, min: 0.0003333333333333333},
    colorTemperature: {max: 7000, min: 2850, step: 50},
    deviceId: "332d34c91861f97ba8f0e11f446da4566a1803539764dd67c1dfe036ef32fd97",
    exposureCompensation: {max: 2, min: -2, step: 0.10000000149011612},
    exposureMode: (2) ["continuous", "manual"],
    exposureTime: {max: 1250, min: 0, step: 0},
    facingMode: ["environment"],
    focusMode: (3) ["manual", "single-shot", "continuous"],
    frameRate: {max: 30, min: 0},
    groupId: "40f2953f5fae495c7471348c844e919762a3213019b271664d220d0aa617313c",
    height: {max: 3000, min: 1},
    iso: {max: 4000, min: 20, step: 1},
    resizeMode: (2) ["none", "crop-and-scale"],
    torch: true,
    whiteBalanceMode: (2) ["continuous", "manual"],
    width: {max: 4000, min: 1}
}

Copied from the Chromium console log.

所以我在我的三星 Galaxy A51 上通过 Brave(基于 Chromium)远程调试尝试了以下限制,其中 none 有效:

let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        focusMode: {exact: ["continuous"]}
    }
});


let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        focusMode: "continuous"
    }
});


let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        focusMode: ["continuous"]
    }
});


let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        advanced: [{focusMode: "continuous"}]
    }
});


let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        advanced: [{focusMode: ["continuous"]}]
    }
});


let stream = await navigator.mediaDevices.getUserMedia({
    video: {
        advanced: [{focusMode: {exact: "continuous"}}]
    }
});

我不知道结构是否正确,我不太确定如何找到它。

有人知道如何使用 focusMode 约束来获得具有连续对焦模式的设备吗?

来自:documentation

The process works like this (using MediaStreamTrack as an example):

  1. If needed, call MediaDevices.getSupportedConstraints() to get the list of supported constraints, which tells you what constrainable properties the browser knows about. This isn't always necessary, since any that aren't known will be ignored when you specify them—but if you have any that you can't get by without, you can start by checking to be sure they're on the list.

  2. Once the script knows whether the property or properties it wishes to use are supported, it can then check the capabilities of the API and its implementation by examining the object returned the track's getCapabilities() method; this object lists each supported constraint and the values or range of values which are supported.

  3. Finally, the track's applyConstraints() method is called to configure the API as desired by specifying the values or ranges of values it wishes to use for any of the constrainable properties about which it has a preference.

  4. The track's getConstraints() method returns the set of constraints passed into the most recent call to applyConstraints().This may not represent the actual current state of the track, due to properties whose requested values had to be adjusted and because platform default values aren't represented. For a complete representation of the track's current configuration, use getSettings().

In the Media Stream API, both MediaStream and MediaStreamTrack have constrainable properties.

可能您的 focusMode 是默认设置,您的 phone 会自己计算出来。所以你必须使用 getSettings() 来 return 实际值。

什么是 .getSettings() returning?