分享好友 前端技术首页 频道列表

记录vue用 html5+做移动APP 用barcode做扫一扫功能时安卓 的bug(黑屏、错位等等)和解决方法

html教程  2023-03-08 20:130

最近做项目时,要用到扫一扫二维码的功能,在html5+里面有提供barcode功能,于是照过来用了,

写的代码如下 :

扫码页面:

<style lang="less" scoped>
#camera {
  height: 100%;
  width: 100%;
  .van-icon {
    top: -2px;
    font-size: 30px;
    color: #fff;
    &.back {
      left: 10px;
    }
    &.light {
      right: 10px;
    }
  }
  #scan {
    width: 100%;
    height: 100%;
    z-index: 2;
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 1);
  }
  .tips {
    text-align: center;
    position: absolute;
    width: 100%;
    top: 40%;
    color: #fff;
    z-index: 3;
    left: 0%;
  }
  .action {
    position: fixed;
    z-index: 777;
    width: 100%;
    left: 0;
    bottom: 0;
    .items {
      display: flex;
      justify-content: space-around;
      background: rgba(0, 0, 0, 0.35);
      width: 60%;
      padding: 4px;
      margin: 4px auto;
      .item {
        flex-basis: 50px;
        text-align: center;
        img {
          width: 27px;
        }
      }
    }
  }
}
</style>
<template>
  <div id="camera">
    <div id="scan"></div>
    <div class="tips">"加载中...</div>
    <div class="action">
      <div class="items">
        <div
          class="item"
          @click="openLight"
        ><img src="../assets/img/png-60@3x.png"></div>
        <div
          class="item"
          @click="getPicture()"
        ><img src="../assets/img/png-59@3x.png"></div>

        <div
          class="item"
          @click="cancelScan()"
        ><img src="../assets/img/png-61@3x.png"></div>
      </div>
    </div>
  </div>
</template>

<script  type='text/ecmascript-6'>
let scan = null;
export default {
  data() {
    return {
      codeUrl: "",
      isLight: false,
      showEnter: false,
      extra: null,
      type: ""
    };
  },

  mounted() {
    if (window.plus) {
      let s = plus.navigator.checkPermission("camera");
      if (s !== "notdeny") {
        plus.nativeUI.alert("相机权限未获取,请往设置应用程序里面开启权限!");
      }
    }
    this.startScan(); //`进入页面就调取扫一扫
  },
  beforeDestroy() {
    if (!window.plus) return;
    scan.cancel();
    scan.close();
  },
  methods: {
    // 打开闪光灯
    openLight() {
      this.isLight = !this.isLight;
      scan.setFlash(this.isLight);
    },

    //创建扫描控件
    startRecognize() {
      let that = this;
      if (!window.plus) return;

      scan = null;
      scan = new plus.barcode.Barcode(
        "scan",
        [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13],
        {
          frameColor: "#1294cb",
          scanbarColor: "#1294cb",
          top: "100px",
          left: "0px",
          width: "100%",
          height: "500px",
          position: "fixed"
        }
      );

      scan.onmarked = onmarked;
      function onmarked(type, result, file) {
        result = result.replace(/\n/g, "");
        that.storage.save("cameraData", result);
        that.codeUrl = result; //扫描后返回值
        that.$router.go(-1);
      }
    },
    // //开始扫描
    startScan() {
      if (!window.plus) return;
      this.startRecognize(); //创建控件
      setTimeout(() => {
        scan.start();
      }, 200);
    },
    // 取消扫描
    cancelScan() {
      let l = plus.webview.all().length;
      this.$toast(l);
      if (l > 1) {
        let ws = plus.webview.currentWebview();
        plus.webview.close(ws);
      } else {
        this.$router.go(-1);
      }
      // this.$router.go(-1);
      if (!window.plus) return;
      plus.navigator.setStatusBarStyle("dark");
      if (scan) {
        scan.cancel(); //关闭扫描
        scan.close(); //关闭条码识别控件
      }
    },
    getPicture() {
      plus.gallery.pick(src => {
        plus.barcode.scan(
          src,
          (type, result) => {
            scan.cancel(); //关闭扫描
            scan.close();
            this.storage.save("cameraData", result);
            this.$router.go(-1);
          },
          error => {
          this.$toast({
              position: "bottom",
              message: error.message
            });
            
          }
        );
      });
    }
  }
};
</script>

 

去扫一扫前的页面:

<template>
  <div id="workshop">
    <div id="scan">
      <div
        id="header"
        :style="{height:statubar}"
      >
        <div
          class="wrap"
          :style="{'padding-top':paddingTop}"
        >
          <div class="back">
            <van-icon
              name="arrow-left"
              @click="$router.go(-1)"
            />
          </div>
          <div class="title">扫一扫</div>
          <div class="history">
            <span>
              历史记录
            </span>
          </div>
        </div>
      </div>
      <div class="searchBox">
        <van-row>
          <van-col span="3">
            <van-button
              size="large"
              :square="true"
              @click="goCamera"
            >
              <img
                class="right"
                src="../../assets/img/png-73@3x.png"
              />
            </van-button>
          </van-col>
          <van-col
            span="21"
            class="van-hairline--surround"
          >
            <van-search
              v-model="value"
              :placeholder="pla"
              @keydown.stop="search"
              shape="round"
            >
              <span slot="left-icon"></span>
            </van-search>
          </van-col>
        </van-row>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: "orderScan",
  data() {
    return {
      result: {},
      value: "",
    };
  },
  methods: {
    // 去扫码
    goCamera() {
        this.$router.go("/camera")
    },
    // 手动输入时搜索
    search(e) {
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },
 // 搜索结果
    onSearch() {}
  },
  mounted() {
    let camera = sessionStorage.getItem("cameraData");
    if (camera && camera != "null") {
      this.value = sessionStorage.getItem("cameraData");
      sessionStorage.removeItem("cameraData");
    }
  }
};
</script>

两个页面运行的效果如下

记录vue用 html5+做移动APP  用barcode做扫一扫功能时安卓 的bug(黑屏、错位等等)和解决方法

 

当时以为很容易,结果做出后遇到各种BUG,有时黑屏,有时位置偏移,有时扫码框偏移等等

比如下图 这个扫码框已经偏移了

记录vue用 html5+做移动APP  用barcode做扫一扫功能时安卓 的bug(黑屏、错位等等)和解决方法

很奇怪,看官方提供的APP怎么扫都是没有问题。于是乎百度各种解决方法,看到 有的说是因为摄像头很耗资源,要用单独的webview来分配,用完即关,看看官方的案例,貌似确实也是新建一个webview来启动的。

心想应该就是这样吧,于是,把方法改了一下。去扫一扫前的页面的$router.go("/camera")时直接改成生成新一个webview,改后的JS代码如下;

  goCamera() {
      //新建一个webview来启动扫一扫,不再$router.push()的跳转
      let h = location.href;
      let n = h.indexOf("#");
      let r = h.substr(n);
      h = h.replace(r, "#/camera");
      let ws = plus.webview.create(h);
      ws.show();
    },

 

 在扫码页面,修改扫码成功后router.go(-1)改成关闭当前的webview,进入页面时设置一个定时器来加载摄像头的资源的JS更改如下

let scan = null;
export default {
  data() {
    return {
      codeUrl: "",
      isLight: false,
      showEnter: false,
      extra: null,
      scan: null,
      type: ""
    };
  },

  mounted() {
    setTimeout(() => {
      // 设置500毫秒等资源加载
      if (window.plus) {
        let s = plus.navigator.checkPermission("camera");
        if (s !== "notdeny") {
          plus.nativeUI.alert("相机权限未获取,请往设置应用程序里面开启权限!");
      return; }
this.startScan(); //`进入页面就调取扫一扫 } }, 500); }, beforeDestroy() { if (!window.plus) return; scan.cancel(); scan.close(); // scan = null; }, methods: { // 打开闪光灯 openLight() { this.isLight = !this.isLight; scan.setFlash(this.isLight); }, //创建扫描控件 startRecognize() { let that = this; if (!window.plus) return; scan = null; scan = new plus.barcode.Barcode( "scan", [plus.barcode.QR, plus.barcode.EAN8, plus.barcode.EAN13], { frameColor: "#1294cb", scanbarColor: "#1294cb", top: "100px", left: "0px", width: "100%", height: "500px", position: "fixed" } ); scan.onmarked = onmarked; function onmarked(type, result, file) { result = result.replace(/\n/g, ""); that.storage.save("cameraData", result); if (plus.webview.all().length > 1) { // 扫码成功后关闭当前的webview let ws = plus.webview.currentWebview(); plus.webview.close(ws); } } }, // //开始扫描 startScan() { if (!window.plus) return; this.startRecognize(); //创建控件 setTimeout(() => { scan.start(); }, 200); }, // 取消扫描 cancelScan() { let l = plus.webview.all().length;if (l > 1) { let ws = plus.webview.currentWebview(); plus.webview.close(ws); } else { this.$router.go(-1); } // this.$router.go(-1); if (!window.plus) return; plus.navigator.setStatusBarStyle("dark"); if (scan) { scan.cancel(); //关闭扫描 scan.close(); //关闭条码识别控件 } }, // 从相册选择图片扫码 getPicture() { plus.gallery.pick(src => { plus.barcode.scan( src, (type, result) => { scan.cancel(); scan.close(); this.storage.save("cameraData", result); if (plus.webview.all().length > 1) { // 扫码成功后关闭当前的webview let ws = plus.webview.currentWebview(); plus.webview.close(ws); } }, error => {      this.$toast({ position: "bottom", message: error.message }); } ); }); } } };

 至于扫码成功的数据切换,我是用localStorage来保存。

改成这样新生成一个webview方法后,我自己测试了N遍,没遇到过那些问题了,我也用我同事的手机测试过,也没有问题。但愿是解决了吧

查看更多关于【html教程】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
HTML中将背景颜色渐变 html设置背景颜色渐变
通过使用 css3 渐变可以让背景两个或多个指定的颜色之间显示平稳的过渡,由于用到css3所以需要考虑下浏览器兼容问题,例如:从左到右的线性渐变,且带有透明度的样式:#grad {background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*

0评论2023-03-08625

html5 Canvas 如何自适应屏幕大小
但是这样创建出的画布不能随着浏览器窗口大小的改变而动态的改变画布的大小。而这一点往往又非常重要, 因为我们会经常改变浏览器窗口大小,不会一直保持某个固定的大小。 html代码 canvas width="300" height="300" id="myCanvas"/canvas设置样式 * {

0评论2023-03-08811

HTML的video标签,不能下载视频代码
!-- 在线视频不能下载代码 --!DOCTYPE html html headscript src="../Demo/demo/book/JQuery/jQuery v2.2.0.js"/script/headbody div style="text-align:center;"video src="../images/PreviewVideo.mp4" width="820"controls="controls&

0评论2023-03-08596

HTML特殊字符、列表、表格总结 html特殊符号对照表
        HTML实体字符  在HTML中一些特殊的字符需要用特殊的方式才能显示出来,比如小于号、版权等,  在课堂上老师教了我们一个有点意思的:空格,在教材上字符实体是“nbsp”通过老师  的演示我们发现不同的浏览器他所显示的效果不同,有的比

0评论2023-03-08521

【JavaScript】使用document.write输出覆盖HTML问题
您只能在 HTML 输出中使用 document.write。如果您在文档加载后使用该方法,会覆盖整个文档。分析HTML输出流是指当前数据形式是HTML格式的数据,这部分数据正在被导出、传输或显示,所以称为“流”。通俗的来说就是HTML文档的加载过程,如果遇到document.writ

0评论2023-03-08815

ASP.Net MVC 控制@Html.DisplayFor日期显示格式
在做一個舊表的查詢頁時,遇到一個問題:字段在db里存儲的是DATETIME,但保存的值只有日期,沒有時間數據,比如2018/2/26 0:00:00,顯示出來比較難看,當然也可以做一個ViewModel,在字段上添加Attribute定義來更改名稱和顯示名稱,如下:[Display(Name = "建

0评论2023-03-08716

html 基础代码
title淄博汉企/title/headbody bgcolor="#00CC66" topmargin="200" leftmargin="200" bottommargin="200"a name="top"/a今天br /天气nbsp;nbsp;nbsp;nbsp;nbsp;不错br /font color="#CC0000"格式控制标签br /b 文字加粗方式1\bbr /str

0评论2023-03-08466

HTML-js 压缩上传的图片方法(默认上传的是file文件)
//压缩图片方法function compressImg(file,callback){var src;var fileSize = parseFloat(parseInt(file['size'])/1024/1024).toFixed(2);var read = new FileReader();read.readAsDataURL(file);read.onload = function (e) {var img = new Image(); img.src

0评论2023-03-08909

html中使table设置固定宽度的列不改变
在开发中发现,有时设置table列的宽度,但是当页面的宽度变时,列宽还是会变,解决方法就是设置table的table-layout属性,设置成table-layout="fixed"就可以了,关于它的其他值,大家可以查查

0评论2023-03-08732

HTML Table 拼凑表格数据并支持滚动条
1 !DOCTYPE html 2 html xmlns="http://www.w3.org/1999/xhtml" 3 head 4 meta http-equiv="Content-Type" content="text/html; charset=utf-8"/ 5 title/title 6 /head 7 body 8 div style="width:600px; height:60px; overflow:scroll;

0评论2023-03-08728

html5 中meta中 content=width=device-width注意
!DOCTYPE html        html        head        meta http-equiv="content-type" content="text/html; charset=UTF-8"        meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"        sty

0评论2023-03-08540

HTML a标签 target属性作用
特殊的目标有 4 个保留的目标名称用作特殊的文档重定向操作:_blank浏览器总在一个新打开、未命名的窗口中载入目标文档。_self这个目标的值对所有没有指定目标的 a 标签是默认目标,它使得目标文档载入并显示在相同的框架或者窗口中作为源文档。这个目标是多

0评论2023-03-08328

HTML5] html和css的使用方法以及样式
第一步: 清除默认样式第二步: 划分模块第三步: 设置模块的大小以及位置第四步: 划分下一级模块html和css引入网页头像link rel="shortcut icon" href="img/...ico"css样式表的引入方式css样式表的引入方式1、外链式link href="" rel="stylesheet"2、嵌入式

0评论2023-03-08936

转载:原文http://www.cnblogs.com/gao241/p/3522143.html
shell实例手册0说明{手册制作: 雪松更新日期: 2013-12-06欢迎系统运维加入Q群: 198173206请使用"notepad++"打开此文档,"alt+0"将函数折叠后方便查阅请勿删除信息,转载请说明出处,抵制不道德行为。错误在所难免,还望指正!# shell实例手册最新下载地址:http

0评论2023-03-08722

HTML里link meta全部标签的作用
做网站建设的时候很少用到太多的标签,但是在网站运营的时候,特别是像移动互联网发展、做SEO之后我们就特别容易看到很多不常见的link标签。那么这些标签都有什么作用呢?E维科技今天来为您整理一下,欢迎其他技术大牛共同分享。HTML里link 和 meta 标签详解m

0评论2023-03-08413

MVC 自定义标签,给Html.ActionLink加上支持图片链接的功能
转载:http://blog.csdn.net/niewq/article/details/17537873Html.ActionLink只支持文字链接,现在我们利用C#的扩展方法写一个Html.ActionLinkWithImage()让其支持图片链接的功能。一、首先新建一个静态类,将命名空间在改在System.Web.Mvc下,代码如下 usin

0评论2023-03-08587

更多推荐