阅读 26

html 拍照实现

为了使用html实时拍照,代替选取图片提高使用效率,开发此功能,开发过程记录以作后期方便使用

流程

image.png

摄像头拍照.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<div>
  <video id="video"></video>
  <canvas id="canvas"></canvas>
  <!--  <canvas id="canvas" style="display: none"></canvas>-->
  <button onclick="getPhoto()">拍照</button>
  <script>
      const width = 480;
      const height = 320;
      const video = document.getElementById('video');

      //拍照
      function getPhoto(){
        //访问摄像头
        if (navigator.mediaDevices.getUserMedia || navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia) {
            //调用用户媒体设备, 访问摄像头
            getUserMedia({video: {width: width, height: height}}, success, error);
        } else {
            alert('不支持访问用户媒体');
        }
      }
      
 
      //访问用户媒体设备的兼容方法
      function getUserMedia(constraints, success, error) {
          if (navigator.mediaDevices.getUserMedia) {
              //最新的标准API
              navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
          } else if (navigator.webkitGetUserMedia) {
              //webkit核心浏览器
              navigator.webkitGetUserMedia(constraints, success, error)
          } else if (navigator.mozGetUserMedia) {
              //firfox浏览器
              navigator.mozGetUserMedia(constraints, success, error);
          } else if (navigator.getUserMedia) {
              //旧版API
              navigator.getUserMedia(constraints, success, error);
          }
      }
 
      //成功回调
      function success(stream) {
          
          console.log('成功');
          //兼容webkit核心浏览器
          // const CompatibleURL = window.URL || window.webkitURL;
          //将视频流设置为video元素的源
          // video.src = CompatibleURL.createObjectURL(stream);
          video.srcObject = stream;
          video.play();
          setInterval(drawCanvasImage, 10)
      }
 
      //失败回调
      function error(error) {
          console.log('失败');
          console.log("访问用户媒体设备失败", error);
      }
 
      function drawCanvasImage() {
          const canvas = document.getElementById('canvas');
          canvas.width = width;
          canvas.height = height;
          const context = canvas.getContext('2d');
          context.drawImage(video, 0, 0, width, height, 0, 0, width, height);
          //获取图片,数据格式为base64
          const imageData = canvas.toDataURL("image/png");
          console.log(imageData +'\n')
      }
  </script>
 
</div>
</body>
</html>
复制代码

根据图片url 获取图片文件.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>

<body>
<img/>
</body>

<script>
function getImageFileFromUrl(url, imageName,callback) {
  // imageName一定要带上后缀
    var blob = null;
    var xhr = new XMLHttpRequest(); 
    xhr.open("GET", url);
    xhr.setRequestHeader('Accept', 'image/jpeg');
    xhr.responseType = "blob";
    xhr.onload = () => {
	if (xhr.status == 200) {
      blob = xhr.response;
      let imgFile = new File([blob], imageName, {type: 'image/jpeg'});
	  console.log(imgFile)
      callback.call(this,imgFile);
    }};
    xhr.send();
}



  $(function () {
	getImageFileFromUrl('https://dss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4085579248,162891217&fm=15&gp=0.jpg','testFile.jpg',function(file){
        debugger
		console.log(file)
		var r = new FileReader()
        r.readAsDataURL(file)
        r.onload = function (e) {
          var base64 = e.target.result;
          $("img").attr('src', base64);         
        }

	});

  })
</script>

</html>


复制代码
文章分类
前端
文章标签