vue 实现扫描二维码和相册识别二维码

25 篇文章 2 订阅
2 篇文章 0 订阅

vue实现扫描二维码和相册识别二维码,纯手打造

可自行写样式

<template>
  <div class="scan">
    <header>
      <p @click="closeScan">返回</p>
      <span>请扫码二维码</span>
      <p @click="scanImg">相册</p>
    </header>
    <div id="bcid">
      <div style="height:40%"></div>
      <p class="tip">...载入中...</p>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
let scan = null;
export default {
  data() {
    return {
      codeUrl: ""
    };
  },
  mounted() {
    //跳转时自动开启
    this.startRecognize();
  },
  methods: {
    // 创建扫描控件
    startRecognize() {
      let that = this;
      if (!window.plus) return;
      scan = new plus.barcode.Barcode(
        "bcid",
        [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
        {
          frameColor: "#009DE2",
          scanbarColor: "#009DE2"
        }
      );
      //scan.onmarked = onmarked;

      // 开始扫描
      scan.start();
      function onmarked(type, result, file) {
        switch (type) {
          case plus.barcode.QR:
            type = "QR";
            break;
          case plus.barcode.EAN13:
            type = "EAN13";
            break;
          case plus.barcode.EAN8:
            type = "EAN8";
            break;
          default:
            type = "其它" + type;
            break;
        }
        result = result.replace(/\n/g, "");
        that.codeUrl = result;
        window.localStorage.codeUrl = result;
       // alert(result);
        that.closeScan();
      }
    },
    // 关闭返回
    closeScan() {
      if (!window.plus) return;
      scan.close();
      this.$router.push({ path: "/home" });
    },
    scanImg() {
      // 从系统相册选择文件
      if (!window.plus) return;
      plus.gallery.pick(
        function(path) {
         // alert(path);
          plus.barcode.scan(
            path,
            function(type, result) {
             // alert("Scan success:(" + type + ")" + result);
              window.localStorage.codeUrl2 = result;
            },
            function(e) {
              console.log(e);
              window.localStorage.codeUrl2 = e;
              plus.nativeUI.alert("如果图片无法识别,请用扫码上传");
            }
          );
        },
        function(e) {
          alert("取消选择图片");
        },
        { filter: "image" }
      );
    }
  }
};
</script>
<style lang="scss">
.scan {
  height: 100%;
  #bcid {
    width: 100%;
    position: absolute;
    left: 0;
    right: 0;
    top: 1rem;
    bottom: 0;
    text-align: center;
    color: #fff;
    background: #ccc;
  }
  header {
    position: absolute;
    display: flex;
    justify-content: space-between;
    font-size: 16px;
    color:#009DE2;
    left: 0.3rem;
    top: 0;
    right: 0.3rem;
    height: 1rem;
    line-height: 1rem;
    z-index: 2;
    span{
      color: #000;
    }
  }
}
</style>

barcode链接:
http://www.html5plus.org/doc/zh_cn/barcode.html

觉得有用就关注点赞一下吧

  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力,加油,奋斗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值