【问题标题】:Unable to access the WebCam无法访问网络摄像头
【发布时间】:2018-06-28 08:02:01
【问题描述】:
(function ($) {

    var webcam = {

    "extern": null, // external select token to support jQuery dialogs
    "append": true, // append object instead of overwriting

    "width": 320,
    "height": 240,

    "mode": "callback", // callback | save | stream

    "swffile": "../Webcam_Plugin/jscam_canvas_only.swf",
    "quality": 85,

    "debug":    function () {},
    "onCapture":    function () {},
    "onTick":   function () {},
    "onSave":   function () {},
    "onLoad":   function () {}
    };

    window["webcam"] = webcam;

    $["fn"]["webcam"] = function(options) {

    if (typeof options === "object") {
        for (var ndx in webcam) {
        if (options[ndx] !== undefined) {
            webcam[ndx] = options[ndx];
        }
        }
    }

    var source = '<object id="XwebcamXobjectX" type="application/x-shockwave-flash" data="'+webcam["swffile"]+'" width="'+webcam["width"]+'" height="'+webcam["height"]+'"><param name="movie" value="'+webcam["swffile"]+'" /><param name="FlashVars" value="mode='+webcam["mode"]+'&amp;quality='+webcam["quality"]+'" /><param name="allowScriptAccess" value="always" /></object>';

    if (null !== webcam["extern"]) {
        $(webcam["extern"])[webcam["append"] ? "append" : "html"](source);
    } else {
        this[webcam["append"] ? "append" : "html"](source);
    }

    var run = 3;
    (_register = function() {
        var cam = document.getElementById('XwebcamXobjectX');

        if (cam && cam["capture"] !== undefined) {

        /* Simple callback methods are not allowed :-/ */
        webcam["capture"] = function(x) {
            try {
            return cam["capture"](x);
            } catch(e) {}
        }
        webcam["save"] = function(x) {
            try {
            return cam["save"](x);
            } catch(e) {}
        }
        webcam["setCamera"] = function(x) {
            try {
            return cam["setCamera"](x);
            } catch(e) {}
        }
        webcam["getCameraList"] = function() {
            try {
            return cam["getCameraList"]();
            } catch(e) {}
        }
        webcam["pauseCamera"] = function() {
            try {
            return cam["pauseCamera"]();
            } catch(e) {}
        }       
        webcam["resumeCamera"] = function() {
            try {
            return cam["resumeCamera"]();
            } catch(e) {}
        }
        webcam["onLoad"]();
        } else if (0 == run) {
        webcam["debug"]("error", "Flash movie not yet registered!");
        } else {
        /* Flash interface not ready yet */
        run--;
        window.setTimeout(_register, 1000 * (4 - run));
        }
    })();
    }

})(jQuery);

以上是我用来访问系统网络摄像头的功能。当我在 localhost 上使用它时它工作正常,但是当将它放在服务器上然后通过 Intranet 访问时就会出现问题。 我找不到修复它的原因。请帮助我。

前两个图像是我在 localhost 上使用我的代码时的图像。它工作正常,因为我能够访问网络摄像头。

问题陈述: 最后一张图片是我尝试通过服务器执行相同操作的图片。由图像打开的网络摄像头完全消失了。只出现白屏,即使我通过网络摄像头拍摄照片,白色图像也只会保存在服务器上,而不是正确的原始图像。

【问题讨论】:

    标签: asp.net .net webcam webcam-capture jquery-webcam-plugin


    【解决方案1】:

    出于安全原因,您必须通过 Https 为网站提供服务才能访问相机。

    【讨论】:

    • 您能否详细说明可能涉及哪种类型的安全原因?这将有很大帮助。 @Maettu
    • Chrome 中的一项政策不允许通过 http 访问麦克风和摄像头。您的网站可能可以在不同的浏览器中运行
    • 是的,以上信息属实。当在 Firefox 上尝试 http 时,它确实有效。 Google 对 Intranet 站点有一些限制。所以它不允许访问内部硬件组件。 @maettu
    【解决方案2】:
    var webcam = {
    
        "extern": null, // external select token to support jQuery dialogs
        "append": true, // append object instead of overwriting
    
        "width": 320,
        "height": 240,
    
        "mode": "callback", // callback | save | stream
    
        "swffile": "../Webcam_Plugin/jscam_canvas_only.swf",
        "quality": 85,
    
        "debug":    function () {},
        "onCapture":    function () {},
        "onTick":   function () {},
        "onSave":   function () {},
        "onLoad":   function () {}
        };
    
        window["webcam"] = webcam;
    
        $["fn"]["webcam"] = function(options) {
    
        if (typeof options === "object") {
            for (var ndx in webcam) {
            if (options[ndx] !== undefined) {
                webcam[ndx] = options[ndx];
            }
            }
        }
    
        var source = '<object id="XwebcamXobjectX" type="application/x-shockwave-flash" data="'+webcam["swffile"]+'" width="'+webcam["width"]+'" height="'+webcam["height"]+'"><param name="movie" value="'+webcam["swffile"]+'" /><param name="FlashVars" value="mode='+webcam["mode"]+'&amp;quality='+webcam["quality"]+'" /><param name="allowScriptAccess" value="always" /></object>';
    
        if (null !== webcam["extern"]) {
            $(webcam["extern"])[webcam["append"] ? "append" : "html"](source);
        } else {
            this[webcam["append"] ? "append" : "html"](source);
        }
    
        var run = 3;
        (_register = function() {
            var cam = document.getElementById('XwebcamXobjectX');
    
            if (cam && cam["capture"] !== undefined) {
    
            /* Simple callback methods are not allowed :-/ */
            webcam["capture"] = function(x) {
                try {
                return cam["capture"](x);
                } catch(e) {}
            }
            webcam["save"] = function(x) {
                try {
                return cam["save"](x);
                } catch(e) {}
            }
            webcam["setCamera"] = function(x) {
                try {
                return cam["setCamera"](x);
                } catch(e) {}
            }
            webcam["getCameraList"] = function() {
                try {
                return cam["getCameraList"]();
                } catch(e) {}
            }
            webcam["pauseCamera"] = function() {
                try {
                return cam["pauseCamera"]();
                } catch(e) {}
            }       
            webcam["resumeCamera"] = function() {
                try {
                return cam["resumeCamera"]();
                } catch(e) {}
            }
            webcam["onLoad"]();
            } else if (30 == run) {
            webcam["debug"]("error", "Flash movie not yet registered!");
            } else {
            /* Flash interface not ready yet */
            run--;
            window.setTimeout(_register, 1000 * (4 - run));
            }
        })();
        }
    
    })(jQuery);
    

    【讨论】:

    • 请解释这段代码与 OP 的代码有何不同,以及为什么它可以解决问题。
    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多