uniapp 扫描
借鉴链接: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 扫描的更多相关文章
- 3000本IT书籍下载地址
http://www.shouce.ren/post/d/id/112300 黑客攻防实战入门与提高.pdfhttp://www.shouce.ren/post/d/id/112299 黑 ...
- uni-app开发经验分享十三:实现手机扫描二维码并跳转全过程
最近使用 uni-app 开发 app ,需要实现一个调起手机摄像头扫描二维码功能,官网API文档给出了这样一个demo: // 允许从相机和相册扫码 uni.scanCode({ success: ...
- 在UniApp的H5项目中,生成二维码和扫描二维码的操作处理
在我们基于UniApp的H5项目中,需要生成一些二维码进行展示,另外也需要让用户可以扫码进行一定的快捷操作,本篇随笔介绍一下二维码的生成处理和基于H5的扫码进行操作.二维码的生成,使用了JS文件wea ...
- uni-app——想说爱你不容易之踩坑系列
1.uni-app不支持动态组件,目前在用i-if判断,或者用scroll-view切换,没有想到什么其他的办法 2.uni-app不支持具名插槽,会导致页面塌陷 3.uni-app在做动态组件渲染的 ...
- uni-app 创建的第一个应用
本人微信公众号:前端修炼之路,欢迎关注 背景介绍 经过上一篇文章uni-app官方教程学习手记的学习之后,我就着手做这个项目了. 目前已经初步搭出了整体的框架,秉着取之于社会,回馈于社会的原则,我将这 ...
- uni-app 自定义扫码界面
二维码扫描,已经成为当下一款应用不可或缺,同时也是用户习以为常的功能了.uni-app 为我们提供了扫码 API ,直接调用即可. 需求场景 在实际开发中,平台提供的默认扫码界面,并不能满足一些自定义 ...
- uni-app 快速认识
uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS.Android.H5.以及各种小程序(微信/阿里/百度/头条/QQ)等多个平台. 即使不跨端,un ...
- 微信小程序实现连续扫码功能(uniapp)
注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...
- 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇)
系列文章 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇) 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇) 前言 好久不见,很久没更新博客了,前段时间 ...
- 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇)
系列文章 手牵手,使用uni-app从零开发一款视频小程序 (系列上 准备工作篇) 手牵手,使用uni-app从零开发一款视频小程序 (系列下 开发实战篇) 扫码体验,先睹为快 可以扫描下微信小程序的 ...
随机推荐
- PostgreSQL性能优化综合案例 - 2
[调优阶段8] 1. 压力测试 pgbench -M prepared -r -c 1 -f /home/postgres/test/login0.sql -j 1 -n -T 180 -h 172. ...
- npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
VScode报错:npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree 不能解析依赖树 ,需要先修复上面 ...
- 需要登陆,请求数据 session
requests中的session模块思路:# 1. 登录 --> 等到cookie# 2.带着cookie 请求到书架的url-->书架上的内容#注意:# 两个操作要连续起来操作# 我们 ...
- js-解决安卓手机软键盘弹出后,固定定位布局被顶上移问题
分析:软键盘弹出后,导致页面高度变化 解决方案:软键盘弹出后,修复页面高度 // 监听窗口变化 resizeScreen(){ if (!this.state.isIOS && thi ...
- python笔记--在文件进行输出
将print的内容输出到文件中 1 #将数据输出到文件中 2 fp=open('E:/text1.txt','a+') 3 print('hello word',file=fp) 4 fp.close ...
- Python读取保存图像文件
Python处理图像数据时通常需要对图像文件进行读取.保存等操作,因此将现有的方法归纳了一下. 1. PIL 依赖包:Pillow 安装:pip install Pillow 源码: 1 import ...
- Cisco ASA防火墙恢复密码和基本配置
Cisco ASA密码恢复 一.思路 Cisco ASA防火墙密码恢复,与路由器相似 修改寄存器的值,绕过startup-config配置文件 重新修改密码 恢复修改寄存器的值,保存配置 二.操作步骤 ...
- FastAPI中声明参数为必需的三种方式
前提 有时候我们定义一些参数的时候,需要声明这个参数为必需,请求者必须传递该参数.FatstAPI中声明参数为必需的方式有三种,分别为:不设默认值. "..." 和 " ...
- Python爬取亚马逊商品页面
仍然利用Requests库来实现 1 import requests 2 r=requests.get('https://www.amazon.cn/gp/product/B01M8L5Z3Y') 3 ...
- 使用源码编译安装PHP7
使用源码编译安装PHP7 2015年6月11日,PHP官网发布消息,正式公开发布PHP7第一版的alpha版本. PHP7特性: PHP 7.0.0 Alpha 1使用新版的ZendEngine引擎, ...