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从零开发一款视频小程序 (系列下 开发实战篇) 扫码体验,先睹为快 可以扫描下微信小程序的 ...
随机推荐
- Word 段落首行缩进
描述 使 Word 段落第一行左突出两个字符的位置,即段落首行缩进的方式有两种. "视图"选项卡--"显示"组--标尺 "开始"选项卡--& ...
- linux系统下,新加硬盘并把现有的/home目录扩容。
我的机器是centos7.3的系统.使用了一段时间后,我的/home 目录的可用空间没有了.于是我新加了一张硬盘.(如果是加了多张硬盘,就先做好raid,使用起来是一样的).我需要把新加的硬盘空间都划 ...
- CMSIS
百度百科-CMSIS CMSIS 编辑 ARM Cortex 微控制器软件接口标准(CMSIS:Cortex Microcontroller Software Interface Standard) ...
- 马哥教育第一周作业N67044-张铭扬
1. 图文并茂解释开源许可证 GPL.BSD.MIT.Mozilla.Apache和LGPL的区别? 1)MIT许可证:MIT是六种开源许可证中最自由宽容,它允许使用者自由修改后无需放置版权说明并且可 ...
- Linux驱动、应用调试技巧
原文地址:https://hceng.cn/2019/04/08/Linux%E9%A9%B1%E5%8A%A8%E3%80%81%E5%BA%94%E7%94%A8%E8%B0%83%E8%AF%9 ...
- C++程序设计实验四 继承
程序源码: #include <iostream> #include <typeinfo> // definitation of Graph class Graph { pub ...
- nginx 更改配置client_max_body_size nginx.conf 修改默认限制上传附件大小
Nginx 上传大文件超时解决办法 情况如下:用nginx作代理服务器,上传大文件时(测试上传50m的文件),提示上传超时或文件过大. 原因是nginx对上传文件大小有限制,而且默认是1M.另外,若上 ...
- 回归分析-2.X 简单线性回归
2.1 简单线性回归模型 y与x之间的关系假设 \(y=\beta_0+\beta_1x+\varepsilon\) \(E(\varepsilon|x)=0\) \(Var(\varepsilon| ...
- gitlab 搭建代理踩坑
公司的外网服务被黑客攻击,所以需要重置服务器 -- 此为前提 gitlab 的安装 网上找吧. 迁移前置条件 1. 迁移备份 gitlab 时,需要确保双方服务器的gitlab 版本相同 cat /o ...
- linux下yum安装时出现Loaded plugins: fastestmirror
linux使用yum安装软件时出现报错Loaded plugins:fastestmirror,是提示这个插件不能使用了,fastestmirror是yum的一个加速插件, 解决的办法是:将这个插件禁 ...