admin管理员组

文章数量:1379408

Im running out of ideas. i have been trying to get the getUserMedia dynamically to select macro camera or atleast focus. Im making barcode(ean) reader and if i put barcode close to camera then I don't get any focusing.

async function startCamera() {
    try {
        if (videoStream) {
            videoStream.getTracks().forEach(track => track.stop());
        }

        videoStream = await navigator.mediaDevices.getUserMedia({
            video: {
                facingMode: {exact: "environment"},
                focusMode: {ideal: "continuous"}
            }
        });

        const $videoElement = $("#camera-feed");
        $videoElement[0].srcObject = videoStream;

        $videoElement.on("loadedmetadata", function () {
            this.play();
            $(".scanning-line").fadeIn(300).addClass("scan-active");
            $videoElement.css({ opacity: 1, transform: "scale(1)" }).fadeIn();
            startScanner();
        });

    } catch (err) {
        $(".scanning-line").fadeOut(300);
    }
}

I tried using AI help but It does not give any good ideas to try, most of them I have tried and failed.

Im running out of ideas. i have been trying to get the getUserMedia dynamically to select macro camera or atleast focus. Im making barcode(ean) reader and if i put barcode close to camera then I don't get any focusing.

async function startCamera() {
    try {
        if (videoStream) {
            videoStream.getTracks().forEach(track => track.stop());
        }

        videoStream = await navigator.mediaDevices.getUserMedia({
            video: {
                facingMode: {exact: "environment"},
                focusMode: {ideal: "continuous"}
            }
        });

        const $videoElement = $("#camera-feed");
        $videoElement[0].srcObject = videoStream;

        $videoElement.on("loadedmetadata", function () {
            this.play();
            $(".scanning-line").fadeIn(300).addClass("scan-active");
            $videoElement.css({ opacity: 1, transform: "scale(1)" }).fadeIn();
            startScanner();
        });

    } catch (err) {
        $(".scanning-line").fadeOut(300);
    }
}

I tried using AI help but It does not give any good ideas to try, most of them I have tried and failed.

Share Improve this question asked Mar 19 at 18:56 DexxDexx 196 bronze badges 1
  • I can't imagine there's a consistent API that allows you to control the camera on such a low-level (to change focus). – Spectric Commented Mar 19 at 23:33
Add a comment  | 

1 Answer 1

Reset to default 0

There is no way to get camera to focus or use macro camera, but what did work was setting the zoom value to 3. It uses the main camera but "seems" like a macro camera.

本文标签: javascriptnavigatormediaDevicesgetUserMedia impossible to get the camera to focusStack Overflow