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

标签 javascript android camera chromium brave

大家好!

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

我可以通过在约束中直接设置 deviceId 来直接在我的调试设备上选择摄像头,如下所示:

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.

因此,我通过 Brave(基于 Chromium)在我的三星 Galaxy A51 上进行远程调试尝试了以下约束,但均无效:

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 是默认设置,您的手机会自行计算。所以你必须使用 getSettings() 来返回实际值。

.getSettings() 返回什么?

关于javascript - 使用 focusMode 约束获取所需的媒体设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68912208/

相关文章:

android - 带有 Phonegap Build 和 PushPlugin 的 Android 的白色通知图标

ios - 如何在 iOS 应用程序的相机中添加当前时间戳

android - Android 中的 setPreviewSize() 错误?

javascript - 具有属性的深层复制 Javascript 数组

javascript - 两个 JavaScript 类之间的区别以及为什么原型(prototype)不适用于 Emp 类

javascript - 如何将样式传递给子组件并将其用作 Vue 中的作用域样式?

javascript - ExtJS Controller 问题。未捕获的类型错误 : Object [object Object] has no method 'setSortState'

java - 后台线程中的 Progress bar.setProgress(variable)

android - 资源覆盖

android - 如何识别手指在相机镜头上或不在android中?