鱼C论坛

 找回密码
 立即注册
查看: 2482|回复: 3

使用Flask后html5无法调用摄像头

[复制链接]
发表于 2023-1-7 08:28:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
HTML文件
  1. <!DOCTYPE html>
  2. <html lang="en">

  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>camera</title>
  8.     <style>
  9.         #camera {
  10.             float: left;
  11.             margin: 20px;
  12.         }
  13.         
  14.         #contentHolder {
  15.             width: 300px;
  16.             height: 300px;
  17.             margin-bottom: 10px;
  18.         }
  19.         
  20.         #btn_snap {
  21.             margin: 0 auto;
  22.             border: 1px solid #f0f0f0;
  23.             background: #5CACEE;
  24.             color: #FFF;
  25.             width: 100px;
  26.             height: 36px;
  27.             line-height: 36px;
  28.             border-radius: 8px;
  29.             text-align: center;
  30.             cursor: pointer;
  31.             cursor: pointer;
  32.             /*禁止选择*/
  33.             -webkit-touch-callout: none;
  34.             /* iOS Safari */
  35.             -webkit-user-select: none;
  36.             /* Chrome/Safari/Opera */
  37.             -khtml-user-select: none;
  38.             /* Konqueror */
  39.             -moz-user-select: none;
  40.             /* Firefox */
  41.             -ms-user-select: none;
  42.             /* Internet Explorer/Edge */
  43.             user-select: none;
  44.             /* Non-prefixed version, currently not supported by any browser */
  45.         }
  46.         
  47.         #imgBoxxx {
  48.             width: 200px;
  49.             margin: 60px 20px 20px;
  50.             /* border-radius: 50%; */
  51.         }
  52.     </style>
  53. </head>

  54. <body>
  55.     <div id="camera">
  56.         <div id="contentHolder">
  57.             <video id="video" width="300" height="300" autoplay></video>
  58.             <canvas style="display:none;" id="canvas" width="300" height="300"></canvas>
  59.         </div>

  60.         <div id="btn_snap" onclick="myfunction()">拍照</div>
  61.     </div>

  62.     <script>
  63.         var canvas = document.getElementById("canvas"),
  64.             pzBtn = document.getElementById("btn_snap"),
  65.             context = canvas.getContext("2d"),
  66.             video = document.getElementById("video");

  67.         alert('该页面会调用您的摄像头')

  68.         // 旧版本浏览器可能根本不支持mediaDevices,我们首先设置一个空对象
  69.         if (navigator.mediaDevices === undefined) {
  70.             navigator.mediaDevices = {};
  71.         }

  72.         // 一些浏览器实现了部分mediaDevices,我们不能只分配一个对象
  73.         // 使用getUserMedia,因为它会覆盖现有的属性。
  74.         // 这里,如果缺少getUserMedia属性,就添加它。
  75.         if (navigator.mediaDevices.getUserMedia === undefined) {
  76.             navigator.mediaDevices.getUserMedia = function(constraints) {
  77.                 // 首先获取现存的getUserMedia(如果存在)
  78.                 var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
  79.                 // 有些浏览器不支持,会返回错误信息
  80.                 // 保持接口一致
  81.                 if (!getUserMedia) {
  82.                     return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
  83.                 }
  84.                 //否则,使用Promise将调用包装到旧的navigator.getUserMedia
  85.                 return new Promise(function(resolve, reject) {
  86.                     getUserMedia.call(navigator, constraints, resolve, reject);
  87.                 });
  88.             }
  89.         }
  90.         var constraints = {
  91.             audio: false,
  92.             video: {
  93.                 width: 720,
  94.                 height: 720
  95.             }
  96.         }
  97.         navigator.mediaDevices.getUserMedia(constraints)
  98.             .then(function(stream) {
  99.                 var video = document.querySelector('video');
  100.                 // 旧的浏览器可能没有srcObject
  101.                 if ("srcObject" in video) {
  102.                     video.srcObject = stream;
  103.                 } else {
  104.                     //避免在新的浏览器中使用它,因为它正在被弃用。
  105.                     video.src = window.URL.createObjectURL(stream);
  106.                 }
  107.                 video.onloadedmetadata = function(e) {
  108.                     video.play();
  109.                 };
  110.             })
  111.             .catch(function(err) {
  112.                 console.log(err.name + ": " + err.message);
  113.             });

  114.         function myfunction() {
  115.             var date = new Date().getTime();
  116.             // 点击,canvas画图
  117.             context.drawImage(video, 0, 0, 300, 300);
  118.             // 获取图片base64链接
  119.             var image = canvas.toDataURL('image/png');
  120.             // 定义一个img
  121.             var img = new Image();
  122.             //设置属性和src
  123.             img.id = "imgBox";
  124.             img.src = image;
  125.             //将图片添加到页面中
  126.             // document.body.appendChild(img);

  127.             // base64转文件
  128.             function dataURLtoFile(dataurl, filename) {
  129.                 var arr = dataurl.split(','),
  130.                     mime = arr[0].match(/:(.*?);/)[1],
  131.                     bstr = atob(arr[1]),
  132.                     n = bstr.length,
  133.                     u8arr = new Uint8Array(n);
  134.                 while (n--) {
  135.                     u8arr[n] = bstr.charCodeAt(n);
  136.                 }
  137.                 return new File([u8arr], filename, {
  138.                     type: mime
  139.                 });
  140.             }
  141.             console.log(img);
  142.             // console.log(dataURLtoFile(image, date + '.png'));


  143.             
  144.         };
  145.     </script>
  146.     <script scr="jsQR.js"></script>
  147. </body>

  148. </html>
复制代码


直接打开的话是可以调用摄像头的

我加了一个flask后
  1. from flask import Flask,render_template, request, redirect, url_for, make_response

  2. app = Flask(__name__)

  3. # app.route("/index")

  4. name = ''
  5. password = ''

  6. @app.route("/index", methods=["POST", "GET"])
  7. def index():
  8.     global name, password
  9.     if request.method == "GET":
  10.         return render_template("index.html")
  11.     if request.method == "POST":
  12.         name = request.form.get("name")
  13.         password = request.form.get("password")
  14.         if name=="Admin" and password=="123456":
  15.             return redirect(url_for('sm'))
  16.             # return "a"
  17.         else:
  18.             return render_template("index.html")


  19. @app.route("/sm", methods=["POST", "GET"])
  20. def sm():
  21.     if (name=="Admin" and password=="123456"):
  22.         return render_template("sao_ma.html")
  23.     else:
  24.         return redirect(url_for('index'))



  25. if __name__ == "__main__":
  26.     app.run(host='0.0.0.0', port=80, debug = True)
复制代码


也就是
  1. @app.route("/sm", methods=["POST", "GET"])
  2. def sm():
  3.     if (name=="Admin" and password=="123456"):
  4.         return render_template("sao_ma.html")
  5.     else:
  6.         return redirect(url_for('index'))
复制代码


这段代码
再根据flask给的ip打开就无法调用摄像头了

求解
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-7 09:00:01 | 显示全部楼层
已解决,感谢我自己
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-7 09:02:19 | 显示全部楼层
Mike_python小 发表于 2023-1-7 09:00
已解决,感谢我自己

《求人不如求自己》
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-7 10:19:20 | 显示全部楼层
liuhongrun2022 发表于 2023-1-7 09:02
《求人不如求自己》

同感
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 06:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表