ionic 扫描二维码/条形码功能
一、安装插件@ionic-native/qr-scanne
二、page
scan.ts
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';/*** Generated class for the ScanPage page.** See https://ionicframework.com/docs/components/#navigation for more info on* Ionic pages and navigation.*/
@IonicPage()
@Component({selector: 'page-scan',templateUrl: 'scan.html',
})
export class ScanPage {light: boolean;//判断闪光灯frontCamera: boolean;//判断摄像头isShow: boolean = false;//控制显示背景,避免切换页面卡顿error = '';devicetype = '';constructor(private navCtrl: NavController,private qrScanner: QRScanner,public navParams: NavParams, ) {//默认为falsethis.light = false;this.frontCamera = false;}ionViewDidLoad() {this.devicetype = this.navParams.get('devicetype') || '';this.qrScanner.prepare().then((status: QRScannerStatus) => {if (status.authorized) {// camera permission was granted// start scanninglet scanSub = this.qrScanner.scan().subscribe((text: string) => {// alert(text);this.qrScanner.hide(); // hide camera previewscanSub.unsubscribe(); // stop scanningif (this.devicetype == 'parent') {this.navCtrl.getPrevious().data.scantextParent = text;} else if (this.devicetype == 'sub') {this.navCtrl.getPrevious().data.scantextSub = text;} else {this.navCtrl.getPrevious().data.scantext = text;}this.navCtrl.pop();});// show camera previewthis.qrScanner.show();// wait for user to scan something, then the observable callback will be called} else if (status.denied) {// camera permission was permanently deniedthis.error = 'camera permission was permanently denied';// you must use QRScanner.openSettings() method to guide the user to the settings page// then they can grant the permission from there} else {// permission was denied, but not permanently. You can ask for permission again at a later time.this.error = 'permission was denied, but not permanently. You can ask for permission again at a later time.';}}).catch((e: any) => {console.log('Error is', e);this.error = e;});}ionViewDidEnter() {//页面可见时才执行this.devicetype = this.navParams.get('devicetype') || '';this.showCamera();this.isShow = true;//显示背景}/*** 闪光灯控制,默认关闭*/toggleLight() {if (this.light) {this.qrScanner.disableLight();} else {this.qrScanner.enableLight();}this.light = !this.light;}/*** 前后摄像头互换*/toggleCamera() {if (this.frontCamera) {this.qrScanner.useBackCamera();} else {this.qrScanner.useFrontCamera();}this.frontCamera = !this.frontCamera;}showCamera() {(window.document.querySelector('ion-app') as HTMLElement).classList.add('cameraView');}hideCamera() {(window.document.querySelector('ion-app') as HTMLElement).classList.remove('cameraView');this.qrScanner.hide();//需要关闭扫描,否则相机一直开着this.qrScanner.destroy();//关闭}ionViewWillLeave() {this.hideCamera();}
}
scan.html
<!--Generated template for the ScanPage page.See http://ionicframework.com/docs/components/#navigation for more info onIonic pages and navigation.
-->
<ion-header><ion-navbar><ion-title>扫描中……{{error}}</ion-title></ion-navbar>
</ion-header>
<ion-content no-scroll [ngClass]="{'qrscanner':isShow}"><div [ngClass]="{'qrscanner-area':isShow}"></div><div [ngClass]="{'through-line':isShow}"></div><div class="button-bottom"><button (click)="toggleLight()" ion-fab class="icon-camera" margin-right><ion-icon name="flash"></ion-icon></button><button (click)="toggleCamera()" ion-fab class="icon-camera"><ion-icon name="reverse-camera"></ion-icon></button></div>
</ion-content>
scan.scss
page-scan {
.qrscanner {background: none !important;&-area {width: 100%;height: 86%;background: url(../assets/imgs/scanner.svg) no-repeat center center;background-size: contain;}}.through-line {left: 25%;width: 50%;height: 2px;background: red;position: absolute;animation: myfirst 2s linear infinite alternate;}@keyframes myfirst {0% {background: red;top: 30%;}25% {background: yellow;top: 35%;}50% {background: blue;top: 40%;}75% {background: green;top: 45%;}100% {background: red;top: 50%;}}.button-bottom {width: 128px;position: absolute;left: 50%;bottom: 80px;margin-left: -64px;.icon-camera {float: left;}
}
}
scan.module.ts
import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ScanPage } from './scan';@NgModule({declarations: [ScanPage,],imports: [IonicPageModule.forChild(ScanPage),],
})
export class ScanPageModule {}
三、使用
a.ts
ionViewDidEnter() {this.scantext = this.navParams.get('scantext') || '';}// 二维码qrScanners() {this.navCtrl.push('ScanPage');}
四、Ionic二维码扫码插件QR Scanner不能扫描一维码问题
解决:
在qr-scanner插件中的QRScanner.java的setupCamera中添加如下代码:
formatList.add(BarcodeFormat.UPC_A); // UPC标准码(通用商品)
formatList.add(BarcodeFormat.UPC_E); // UPC缩短码(商品短码)
formatList.add(BarcodeFormat.EAN_13);
formatList.add(BarcodeFormat.EAN_8);
formatList.add(BarcodeFormat.CODE_39);
formatList.add(BarcodeFormat.CODE_93);
formatList.add(BarcodeFormat.CODE_128);
formatList.add(BarcodeFormat.ITF);
formatList.add(BarcodeFormat.DATA_MATRIX);
五、源码参考
https://github.com/xif3681/job2-8-smart-city-install-app/tree/master/src/pages/scan
ionic 扫描二维码/条形码功能相关推荐
- IONIC扫描二维码和一维码(条形码)
IONIC扫码目前有三个插件 : https://segmentfault.com/a/1190000012164809 该链接介绍的比较详细 ,下面采用的是 QR Scann ...
- 【Android】实现生成二维码、条形码和扫描二维码的功能
目录 一.添加依赖 二.布局文件 三.实现生成二维码的功能 四.效果图 一.添加依赖 要先添加一个第三方库来实现二维码的生成以及扫描二维码的功能,开源库如下: 在build.grade(Moudle) ...
- iOS 扫描二维码/条形码
级别:★★☆☆☆ 标签:「iOS 原生扫描」「AVCaptureSession」「AVCaptureDevice」「rectOfInterest」 作者: Xs·H 审校: QiShare团队 最近做 ...
- Android使用ZBar扫描二维码/条形码(实例)+常见问题汇总
写在前面:因项目需求,需要实现二维码扫码功能,笔者测试过多种开源扫码工具,但因不跨平台.扫描速度慢等问题逐个放弃,最后选用ZBar实现功能,笔者发现ZBar扫码在跨主流手机平台.扫码速度等方面有较明显 ...
- Android PAD扫描枪扫描二维码条形码
Android PAD扫描枪扫描二维码条形码 1,目前扫描条码只有通过按键触发,按下按键会发送F12的键值,可以通过监听F12键判断是否触发扫描 2,扫到的条码我们会在当前光标处显示出来,同时也发了一 ...
- 【微信小程序】使用 wx.scanCode方法实现扫描二维码的功能
在微信小程序中,你可以使用 wx.scanCode 方法实现扫描二维码的功能.以下是一个简单的示例代码: // 在某个页面或组件中调用扫描二维码功能 wx.scanCode({success(res) ...
- 微信小程序扫描二维码条形码 (wx.scanCode)
前言 在业务中遇到需要获取商品的二维码的信息返回商品的二维码信息,在调用后台的接口,首先想到用小程序的Api,wx.scanCode(Object object)获取二维码信息. 支持相机扫描或者相册 ...
- 微信小程序扫描二维码条形码
wxml代码 <button class='deaBtn' bindtap='scancode'>扫描二维码</button> js代码 scancode: function( ...
- python扫描二维码输出内容_通过python扫描二维码/条形码并打印数据
需提前安装好pyzbar和opencv-python库(博主的电脑安装opencv-python库比较麻烦,但大部分都不会出现该问题) 安装方法:打开命令框输入 pip install pyzbar/ ...
最新文章
- 多维数组与指针之间的关系详解
- Level/levelup-2-API
- 类和对象—对象特性—深拷贝与浅拷贝
- 20天吃透 PyTorch 中文版(附下载)
- Java黑皮书课后题第8章:*8.2(求矩阵对角线元素的和)使用下面的方法头编写一个方法,求n*n的double类型矩阵中主对角线上所有数字的和。编写一个程序,读取一个4*4的矩阵,显示主对角线和
- wifi芯片_全新蜕变!康希通信第四代WIFI 6 FEM芯片将于2020年Q1量产
- 【Flink】Flink AM container is launched, waiting for AM container to Register with RM
- oralce insert 时,出现的两个错误,以及解决方法
- 研究生们都在推荐哪些好用的论文在线翻译软件?
- 解决 你的组织策略阻止我们完成此操作
- 哄女朋友玩的c语言编程,哄女朋友开心的小套路 逗女朋友开心的话套路
- 读《我的身体里早已落叶纷飞》
- MP4文件格式的相关内容
- 面试官问:除了ArrayList你还看过哪些List源码
- 数组中有两种数出现奇数次,其他数出现偶数次,打印奇数次的数
- Spring Boot系列 - 3. SpringBoot项目学习汇总
- 六派巨量转移技术概述
- 计算机课 - 计算机科学导论
- 3GP/FLV/AVI 格式整理
- 2019春第二次课程实际实验报告