flash jquery 调用摄像头 vue chrome49浏览器

flash jquery 调用摄像头 vue chrome49浏览器

这个摄像头,不能一个页面加载多个,只能一个页面显示一次,所以 调用的时候,记得加v-if 把组件销毁,然后从新加载新的

<!--
* @description 摄像头vue版实例
* @fileName cameraObject.vue
* @author 彭成刚
* @date 2019/03/18 10:46:55
* @version V1.0.0
!-->
<template>
  <div>

    <div id="webcam"></div>
    <!--<button @click="jieping">截屏</button>-->
    <div style="color:red; margin: 10px;">点击即可启用 Adobe Flash Player(拍照功能将启用Flash,第一次启用将会刷新页面,导致数据丢失,建议返回表单页面保存数据后开启拍照功能。)</div>

    <Button type='primary'
            @click="handleCamera"
            style="margin-bottom:5px; margin-right:5px;">拍照</Button><!--icon="md-camera"-->
    <Button type='warning'
            @click="leftRotate"
            :disabled="buttonDisabled"
            style="margin-bottom:5px; margin-right:5px;">逆旋转</Button>
    <Button type='warning'
            @click="rightRotate"
            :disabled="buttonDisabled"
            style="margin-bottom:5px;">正旋转</Button>
    <!--<canvas id="canvas" width="320" height="240"></canvas>-->
    <div>
    <img ref="img" id="base64image" src='' />
    </div>
  </div>
</template>

<script>
import jQuery from '@/../public/components/webcam/jquery.webcam'
export default {
  data () {
    return {
      buttonDisabled: true,
      timer: new Date().getTime().toString(),
      picIsHave: false
    }
  },

  components: {},

  computed: {},

  mounted () {
    let _this = this
    var pos = 0, ctx = null, saveCB, image = [];

    var canvas = document.createElement("canvas");
    canvas.setAttribute('width', 320);
    canvas.setAttribute('height', 240);

    if (canvas.toDataURL) {

      ctx = canvas.getContext("2d");

      image = ctx.getImageData(0, 0, 320, 240);

      saveCB = function(data) {
        // console.info('data', data)

        var col = data.split(";");
        var img = image;

        for(var i = 0; i < 320; i++) {
          var tmp = parseInt(col[i]);
          img.data[pos + 0] = (tmp >> 16) & 0xff;
          img.data[pos + 1] = (tmp >> 8) & 0xff;
          img.data[pos + 2] = tmp & 0xff;
          img.data[pos + 3] = 0xff;
          pos+= 4;
        }


        if (pos >= 4 * 320 * 240) {
          ctx.putImageData(img, 0, 0)
          var base64 = canvas.toDataURL("image/png")
          // console.info('canvas.toDataURL("image/png")',)
          // document.getElementById('base64image').attributes('src',base64)
          console.info('_this',_this)
          jQuery('#base64image').attr('src',base64)
          _this.buttonDisabled = false
          // $.post("/upload.php", {type: "data", image: canvas.toDataURL("image/png")});
          pos = 0;
        }
      };

    } else {

      saveCB = function(data) {
        console.info('data2',data)
        image.push(data);

        pos+= 4 * 320;

        if (pos >= 4 * 320 * 240) {
          console.info('data2 ok')
          // $.post("/upload.php", {type: "pixel", image: image.join('|')});
          pos = 0;
        }
      };
    }

    jQuery("#webcam").webcam({

      width: 320,
      height: 240,
      mode: "callback",
      swffile: "/familyMajor_web/components/webcam/jscam_canvas_only.swf",

      onSave: saveCB,

      onCapture: function () {
        webcam.save();
      },

      debug: function (type, string) {
        console.log(type + ": " + string);
      }
    });
  },

  methods: {
    // 外层调用
    getPicBase64 () {
      let base64 = this.$refs.img.src
      let ret = {
        picIsHave: this.picIsHave,
        base64: base64
      }
      return ret
    },
    handleCamera () {
      this.picIsHave = true
      webcam.capture()
    },
    leftRotate () {
      let src = this.$refs.img.src
      let edg = -90
      this.rotateBase64Img(src, edg, (_) => { this.$refs.img.src = _ })
    },
    rightRotate () {
      let src = this.$refs.img.src
      let edg = 90
      this.rotateBase64Img(src, edg, (_) => { this.$refs.img.src = _ })
    },
    rotateBase64Img (src, edg, callback) {
      var canvas = document.createElement('canvas')
      var ctx = canvas.getContext('2d')

      var imgW // 图片宽度
      var imgH // 图片高度
      var size // canvas初始大小

      if (edg % 90 !== 0) {
        console.error('旋转角度必须是90的倍数!')
        // throw '旋转角度必须是90的倍数!'
      }
      (edg < 0) && (edg = (edg % 360) + 360)
      const quadrant = (edg / 90) % 4 // 旋转象限
      const cutCoor = { sx: 0, sy: 0, ex: 0, ey: 0 } // 裁剪坐标

      var image = new Image()
      image.crossOrigin = 'anonymous'
      image.src = src

      image.onload = function () {
        imgW = image.width
        imgH = image.height
        size = imgW > imgH ? imgW : imgH

        canvas.width = size * 2
        canvas.height = size * 2
        switch (quadrant) {
          case 0:
            cutCoor.sx = size
            cutCoor.sy = size
            cutCoor.ex = size + imgW
            cutCoor.ey = size + imgH
            break
          case 1:
            cutCoor.sx = size - imgH
            cutCoor.sy = size
            cutCoor.ex = size
            cutCoor.ey = size + imgW
            break
          case 2:
            cutCoor.sx = size - imgW
            cutCoor.sy = size - imgH
            cutCoor.ex = size
            cutCoor.ey = size
            break
          case 3:
            cutCoor.sx = size
            cutCoor.sy = size - imgW
            cutCoor.ex = size + imgH
            cutCoor.ey = size + imgW
            break
        }

        ctx.translate(size, size)
        ctx.rotate(edg * Math.PI / 180)
        ctx.drawImage(image, 0, 0)

        var imgData = ctx.getImageData(cutCoor.sx, cutCoor.sy, cutCoor.ex, cutCoor.ey)
        if (quadrant % 2 === 0) {
          canvas.width = imgW
          canvas.height = imgH
        } else {
          canvas.width = imgH
          canvas.height = imgW
        }
        ctx.putImageData(imgData, 0, 0)
        callback(canvas.toDataURL())
      }
    }


  }
}

</script>
<style lang='less'>

</style>

 

posted @ 2019-06-25 17:32  彭成刚  阅读(701)  评论(0编辑  收藏  举报