借鉴链接:https://blog.csdn.net/qq_33165549/article/details/89879435

1.扫描页面

<template>
<view>
</view>
</template>
<script>
var barcode = null;
export default {
data() {
return {
name: '', //要在扫码界面自定义的内容
title:'', //扫描标题
flash: false, //是否打开摄像头
isOngoingScan:"0", //是否连续扫描
type: ''
};
},
onLoad(d) {
// var n = d.text;
// this.type = d.type;
// if (n) {
// this.name = n;
// }
// this.name = d;
this.isOngoingScan = d.isOngoingScan;
this.title = d.title; var pages = getCurrentPages();
var page = pages[pages.length - 1];
// #ifdef APP-PLUS
plus.key.hideSoftKeybord();
plus.navigator.setFullscreen(true); //全屏
var currentWebview = page.$getAppWebview();
this.createBarcode(currentWebview); //创建二维码窗口
this.createView(currentWebview); //创建操作按钮及tips界面
// #endif },
methods: {
// 扫码成功回调
onmarked(type, result) {
plus.key.hideSoftKeybord();
// this.name = result;
const eventChannel = this.getOpenerEventChannel()
eventChannel.emit('acceptScanContent', { scanText: result }); this.tui.toast("扫描内容:" + result, 1000, false);
if(this.isOngoingScan === "1")
{
setTimeout(() =>
{
plus.navigator.setFullscreen(false);
barcode.close();
// #ifdef APP-PLUS
var pages = getCurrentPages();
var page = pages[pages.length - 1];
var currentWebview = page.$getAppWebview();
this.createBarcode(currentWebview); //创建二维码窗口
this.createView(currentWebview); //创建操作按钮及tips界面
// #endif
}, 1000);
}
else
{
plus.navigator.setFullscreen(false);
uni.navigateBack({
delta: 1
});
this.$eventHub.$emit(this.type, {
result: result
});
barcode.close();
}
},
// 创建二维码窗口
createBarcode(currentWebview)
{
plus.key.hideSoftKeybord();
barcode = plus.barcode.create('barcode',
[plus.barcode.QR,plus.barcode.EAN13,plus.barcode.EAN8
,plus.barcode.UPCA,plus.barcode.UPCE,plus.barcode.CODABAR
,plus.barcode.CODE39,plus.barcode.CODE93,plus.barcode.CODE128
,plus.barcode.ITF,plus.barcode.RSS14,plus.barcode.RSSEXPANDED
,plus.barcode.AZTEC,plus.barcode.DATAMATRIX,plus.barcode.MAXICODE
,plus.barcode.PDF417]
, {
top: '0',
left: '0',
width: '100%',
height: '100%',
scanbarColor: '#1DA7FF',
position: 'static',
frameColor: '#1DA7FF'
});
barcode.onmarked = this.onmarked;
barcode.setFlash(this.flash);
currentWebview.append(barcode);
barcode.start();
},
// 创建操作按钮及tips
createView(currentWebview) {
// 创建返回原生按钮
var backVew = new plus.nativeObj.View('backVew', {
top: '0px',
left: '0px',
height: '40px',
width: '100%'
},
[{
tag: 'img',
id: 'backBar',
src: 'static/backBar.png',
position: {
top: '2px',
left: '3px',
width: '35px',
height: '35px'
}
}]);
// 创建打开手电筒的按钮
var scanBarVew = new plus.nativeObj.View('scanBarVew', {
top: '60%',
left: '40%',
height: '10%',
width: '20%'
},
[{
tag: 'img',
id: 'scanBar',
src: 'static/scanBar.png',
position: {
width: '28%',
left: '36%',
height: '30%'
}
},
{
tag: 'font',
id: 'font',
text: '轻触照亮',
textStyles: {
size: '10px',
color: '#ffffff'
},
position: {
width: '80%',
left: '10%'
}
}
]);
// 创建展示类内容组件
var content = new plus.nativeObj.View('content', {
top: '0px',
left: '0px',
height: '100%',
width: '100%'
},
[
{
tag: 'font',
id: 'scanTitle',
text: this.title,
textStyles: {
size: '18px',
color: '#ffffff'
},
position: {
top: '0px',
left: '0px',
width: '100%',
height: '40px'
}
},
{
tag: 'font',
id: 'scanTips',
text: this.name,
textStyles: {
size: '14px',
color: '#ffffff',
whiteSpace: 'normal'
},
position: {
top: '90px',
left: '10%',
width: '80%',
height: 'wrap_content'
}
}
]);
backVew.interceptTouchEvent(true);
scanBarVew.interceptTouchEvent(true);
currentWebview.append(content);
currentWebview.append(scanBarVew);
currentWebview.append(backVew);
backVew.addEventListener("click", function(e) { //返回按钮
uni.navigateBack({
delta: 1
});
barcode.close();
plus.navigator.setFullscreen(false);
}, false);
var temp = this;
scanBarVew.addEventListener("click", function(e) { //点亮手电筒
temp.flash = !temp.flash;
if (temp.flash) {
scanBarVew.draw([{
tag: 'img',
id: 'scanBar',
src: 'static/yellow-scanBar.png',
position: {
width: '28%',
left: '36%',
height: '30%'
}
},
{
tag: 'font',
id: 'font',
text: '轻触照亮',
textStyles: {
size: '10px',
color: '#ffffff'
},
position: {
width: '80%',
left: '10%'
}
}
]);
} else {
scanBarVew.draw([{
tag: 'img',
id: 'scanBar',
src: 'static/scanBar.png',
position: {
width: '28%',
left: '36%',
height: '30%'
}
},
{
tag: 'font',
id: 'font',
text: '轻触照亮',
textStyles: {
size: '10px',
color: '#ffffff'
},
position: {
width: '80%',
left: '10%'
}
}
])
}
if (barcode) {
barcode.setFlash(temp.flash);
}
}, false)
}
},
onBackPress() {
// #ifdef APP-PLUS
// 返回时退出全屏
barcode.close();
plus.navigator.setFullscreen(false);
// #endif
},
onUnload() {
barcode.close();
plus.navigator.setFullscreen(false);
}
};
</script> <style scoped>
</style>

2.调用页面

<template>
<view class="container">
<!-- <uni-table>
<uni-tr>
<uni-td>扫描:</uni-td>
<uni-td><input type="text" class="remarkStyle" @longpress="scan" placeholder="长按启动扫描" @blur="inputBlur" /></uni-td>
</uni-tr>
</uni-table> -->
<view>扫描:<input type="text" class="remarkStyle" @longpress="scan" placeholder="长按启动扫描" @blur="inputBlur" /></view> <uni-table>
<uni-tr v-for="(row,idx) in Table" :key="idx">
<uni-td><view>{{row.ID}}</view></uni-td>
<uni-td class="tdWidth linefeed"><view>{{row.scanStr}}</view></uni-td>
</uni-tr>
</uni-table>
</view>
</template> <script>
export default {
data() {
return {
Table:[], //扫描的数据列表
Text:"", //扫描内容
tableIndex:0, //序号,自增id
}
},
methods:
{
//获取扫描栏输入内容
inputBlur(e)
{
this.Text = e.detail.value;
},
//扫描
scan()
{
// // 调起条码扫描,单个扫描
// uni.scanCode({
// success: (res) => {
// this.tui.toast(res.result,1000,false);
// var dataList =
// {
// "id":1,
// "scanText": res.result,
// };
// this.Table.push(dataList);
// this.scan();
// },
// fail: (res) => { }
// }) plus.key.hideSoftKeybord();//关闭软件盘
var Table = this.Table;
var index = this.tableIndex;
uni.navigateTo({
url: '/pages/scan/scan?isOngoingScan=1&title=扫描',
events:
{
// 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
acceptScanContent: function(data)
{
index++;
var row = {
"ID":index,
"scanStr":data.scanText,
};
Table.unshift(row);
//写入缓存
uni.setStorageSync('TableStore', Table);
uni.setStorageSync('TableIndex', index);
}
},
success: (res) => { },
fail: (res) => { }
});
}
}
,onLoad()
{
//获取当前最大的序号id,为空时取默认值
var index = uni.getStorageSync('TableIndex');
if(index)
{
this.tableIndex = index;
} //进入时加载之前的列表数据
var storage = uni.getStorageSync("TableStore");
for(var i = 0; i < storage.length; i++)
{
this.Table.push(storage[i]);
}
} }
</script> <style>
.container
{
position: relative;
padding-bottom: 100rpx;
}
.remarkStyle
{
border-style: none none solid;
font-size: 12px;
}
.tdWidth //列表固定宽度
{
width: 25%;
}
.linefeed //文本换行
{
white-space: normal; // 规定段落中的文本不进行换行
word-break: break-all; // 允许单词中换行,在容器的最右边进行断开不会浪费控件
word-wrap: break-word; // 防止长单词溢出,单词内部短句
}
</style>

pages.json 加上扫描页面地址:/pages/scan/scan

uniapp 扫描的更多相关文章

  1. 3000本IT书籍下载地址

    http://www.shouce.ren/post/d/id/112300    黑客攻防实战入门与提高.pdfhttp://www.shouce.ren/post/d/id/112299    黑 ...

  2. uni-app开发经验分享十三:实现手机扫描二维码并跳转全过程

    最近使用 uni-app 开发 app ,需要实现一个调起手机摄像头扫描二维码功能,官网API文档给出了这样一个demo: // 允许从相机和相册扫码 uni.scanCode({ success: ...

  3. 在UniApp的H5项目中,生成二维码和扫描二维码的操作处理

    在我们基于UniApp的H5项目中,需要生成一些二维码进行展示,另外也需要让用户可以扫码进行一定的快捷操作,本篇随笔介绍一下二维码的生成处理和基于H5的扫码进行操作.二维码的生成,使用了JS文件wea ...

  4. uni-app——想说爱你不容易之踩坑系列

    1.uni-app不支持动态组件,目前在用i-if判断,或者用scroll-view切换,没有想到什么其他的办法 2.uni-app不支持具名插槽,会导致页面塌陷 3.uni-app在做动态组件渲染的 ...

  5. uni-app 创建的第一个应用

    本人微信公众号:前端修炼之路,欢迎关注 背景介绍 经过上一篇文章uni-app官方教程学习手记的学习之后,我就着手做这个项目了. 目前已经初步搭出了整体的框架,秉着取之于社会,回馈于社会的原则,我将这 ...

  6. uni-app 自定义扫码界面

    二维码扫描,已经成为当下一款应用不可或缺,同时也是用户习以为常的功能了.uni-app 为我们提供了扫码 API ,直接调用即可. 需求场景 在实际开发中,平台提供的默认扫码界面,并不能满足一些自定义 ...

  7. uni-app 快速认识

    uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS.Android.H5.以及各种小程序(微信/阿里/百度/头条/QQ)等多个平台. 即使不跨端,un ...

  8. 微信小程序实现连续扫码功能(uniapp)

    注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...

  9. 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇)

    系列文章 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇) 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇) 前言 好久不见,很久没更新博客了,前段时间 ...

  10. 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇)

    系列文章 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇) 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇) 扫码体验,先睹为快 可以扫描下微信小程序的 ...

随机推荐

  1. Pdfjs第三方插件使用

    项目中时常会遇到在线预览PDF文件的需求,网上一找也能找到很多,但被使用率高的并不多,经过多次实验发现被应该最多的还属pdfjs插件. 首先介绍下:Pdf.js插件是由Mozilla 主导推出的 作用 ...

  2. computed与watch的区别

    1.computed表示的是计算属性,watch指的是监听属性,监听的值变化时执行回调函数 2.computed会使用缓存,而watch不使用缓存,每次监听都执行回调 3.computed需要retu ...

  3. [GKCTF2021]RRRRSA

    [GKCTF2021]RRRRSA 题目 from Crypto.Util.number import * from gmpy2 import gcd flag = b'xxxxxxxxxxxxx' ...

  4. ES6判断对象是否为空

    1.ES6判断对象是否为空{} let obj = {} if(Object.keys(obj).length == 0){ console.log("对象是空的") }else{ ...

  5. C++程序设计实验五 模板类与多态

    三.实验内容 2. 实验任务2 Person.hpp: #ifndef PERSON_TASK_HPP #define PERSON_TASK_HPP #include<iostream> ...

  6. 【剑指Offer】【字符串】字符串的排列

    题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入一个字符串,长 ...

  7. leetcode刷题(二)

    1.栈 逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分.给定逆波兰表 ...

  8. 还在拿flex进行布局吗?快来试试grid网格布局吧

    例: 遇到这种布局要求我们应该怎么办? 方法1:使用flex布局 <style> * { padding: 0; margin: 0; } .gird_Box { width: 100vw ...

  9. echarts地图上的坐标更换为自定义的图标

    ECharts 是一个开源的数据可视化库,支持多种图表类型,包括地图.在 ECharts 地图中,可以通过自定义的方式来更换坐标点的图标. 下面是一些实现自定义坐标图标的步骤: 首先,你需要准备自定义 ...

  10. cenots7 rpm 包升级ssh

    rpm下载地址 也可以自行官网下载 链接: https://pan.baidu.com/s/1S945MehpmZbIriKK6l7Sfw 提取码: y5ua centos7rpm包升级ssh 逻辑思 ...