+
96
-

js中自签名ssl证书能否调用摄像头?

 if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
                   navigator.mediaDevices.getUserMedia({
                       video: true,
                       audio: true
                   }).then(function (stream) {
                       console.log(stream);
                       MediaStreamTrack=typeof stream.stop==='function'?stream:stream.getTracks()[1];
                       video.src=(window.URL).createObjectURL(stream);
                       video.play();
                   }).catch(function(err){
                       console.log(err);
                   });

最近想通过浏览器调用摄像头,发现一直失败,后来查了说要在https下面才可以,买一个签名证书又要钱,舍不得,请问自签名证书有用吗?

网友回复

+
16
-

可以,自建证书构建https也是可以的

+
16
-

可以

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>HTML5 GetUserMedia Demo</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

</head>

<body>

    <input type="button" title="开启摄像头" value="开启摄像头"BfwOnclick="getMedia();" /><br />

    <video height="120px" autoplay="autoplay"></video><hr />

    <input type="button" title="拍照" value="拍照"BfwOnclick="getPhoto();" /><br />

    <canvas id="canvas1" height="120px"></canvas><hr />

    <input type="button" title="视频" value="视频"BfwOnclick="getVedio();" /><br />

    <canvas id="canvas2" height="120px"></canvas>


    <script type="text/javascript">

        var video = document.querySelector('video');

        var audio, audioType;


        var canvas1 = document.getElementById('canvas1');

        var context1 = canvas1.getContext('2d');


        var canvas2 = document.getElementById('canvas2');

        var context2 = canvas2.getContext('2d');


        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;

        window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;


        var exArray = []; //存储设备源ID

        MediaStreamTrack.getSources(function (sourceInfos) {

            for (var i = 0; i != sourceInfos.length; ++i) {

                var sourceInfo = sourceInfos[i];

                //这里会遍历audio,video,所以要加以区分

      ...

点击查看剩余70%

我知道答案,我要回答