Ionic4 Cordova 调用原生硬件 Api 实现扫码功能
QR Scanner 速度快,样式随心所欲,默认只能扫二维码 https://ionicframework.com/docs/native/qr-scanner/
安装插件
ionic cordova plugin add cordova-plugin-qrscanner
npm install @ionic-native/qr-scanne
app.module.ts 引入依赖注入
import { QRScanner } from '@ionic-native/qr-scanner/ngx';
providers: [
Camera,
StatusBar,
SplashScreen,
QRScanner,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
]
新建页面
ionic g page scan
使用
下载扫码框背景图 scanner.svg 放在 src/assets 目录里面
下载地址:http://www.ionic.wang/scanner_svg.zip
scan.html
<ion-header>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
</ion-buttons>
<ion-title>扫码中..</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [ngClass]="{'qrscanner':isShow}">
<div [ngClass]="{'qrscanner-area':isShow}">
</div>
<div [ngClass]="{'through-line':isShow}"></div>
<div class="button-bottom">
<ion-button (click)="toggleLight()" class="icon-camera">
<ion-icon name="flash"></ion-icon>
</ion-button>
<ion-button (click)="toggleCamera()" class="icon-camera">
<ion-icon name="reverse-camera"></ion-icon>
</ion-button>
</div>
</ion-content>
css 样式:
.ion-page {
background: transparent none !important;
}
ion-content {
--background: transparent none !important;
}
.qrscanner {
background: none;
&-area {
width: 100%;
height: 86%;
background: url(../../assets/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;
}
}
ts
import { Component, OnInit } from '@angular/core';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner/ngx';
import { NavController } from '@ionic/angular';
@Component({
selector: 'app-scan',
templateUrl: './scan.page.html',
styleUrls: ['./scan.page.scss'],
})
export class ScanPage implements OnInit {
public light: boolean;//判断闪光灯
public frontCamera: boolean;//判断摄像头
public isShow: boolean = false;//控制显示背景,避免切换页面卡顿
constructor(private qrScanner: QRScanner, public navController: NavController) { }
ngOnInit() {
// this.ionViewWillEnter() ;
}
//刚进来的时候执行扫码
ionViewWillEnter() {
this.qrScanner.prepare()
.then((status: QRScannerStatus) => {
if (status.authorized) {
// start scanning
let scanSub = this.qrScanner.scan().subscribe((text: string) => {
console.log('Scanned something', text);
alert(text);
this.qrScanner.hide(); // hide camera preview
scanSub.unsubscribe(); // stop scanning
this.navController.back();
});
// 打开摄像头
this.qrScanner.show();
} else if (status.denied) {
console.log('没有摄像头权限,请前往设置中开启');
} else {
// permission was denied, but not permanently. You can ask for permission
console.log('没有摄像头权限,请前往设置中开启');
}
})
.catch((e: any) => console.log('Error is', e));
}
/*页面可见后执行 */
ionViewDidEnter() {
this.isShow = true;//显示背景
console.log("ionViewDidEnter")
}
/**
* 闪光灯控制,默认关闭
*/
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;
}
//组件销毁
ionViewWillLeave() {
this.qrScanner.hide();//需要关闭扫描,否则相机一直开着
this.qrScanner.destroy();//关闭
}
}
Ionic QR Scanner Android 扫描条形码配置
找到 QRScanner.java ArrayList<BarcodeFormat> formatList = new ArrayList<BarcodeFormat>();
formatList.add(BarcodeFormat.QR_CODE);
然后在上面代码后面新增下面代码
formatList.add(BarcodeFormat.UPC_A);
formatList.add(BarcodeFormat.UPC_E);
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);
真机测试效果:

出现扫描框,但是没有打开相机,白屏,同时Cordova.js 找不到报错
找到index.html中,手动引入,重新编译运行即可
<script src="cordova.js"></script>
Ionic4 Cordova 调用原生硬件 Api 实现扫码功能的更多相关文章
- 调用原生硬件 Api 实现照相机 拍照和相册选择 以及拍照上传
一.Flutter image_picker 实现相机拍照和相册选择 https://pub.dev/packages/image_picker 二.Flutter 上传图片到服务器 ht ...
- 配置微信api调扫码功能
var url = encodeURIComponent(location.href.split('#')[0]); $.get(iapi+'/htweb/wx/getJsSdkSign?url='+ ...
- ionic3 实现扫码功能
ionic3 通过插件phonegap-plugin-barcodescanner,调用机器硬件摄像头实现扫码功能. 首先当然先了解下 phonegap-plugin-barcodescanner,这 ...
- 微信小程序实现连续扫码功能(uniapp)
注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...
- vue移动app扫码功能
第一步: 上面这段代码写在index.html里面,我也不知道为什么,可能是全局的关系: 第二步: 定义一个按钮,点击启动扫码功能,另外再定义一个盒子来当做扫码的容器:我给这个盒子定义了一个id类名: ...
- Ionic Cordova 调用原生 Api 实现拍照上传 图片到服务器功能
Ionic 调用 Device 设备 Api 获取手机的设备信息 1. 找到对应的Api: https://ionicframework.com/docs/native/device/ 2. 安装相关 ...
- PHP--------微信网页开发实现微信扫码功能
今天说说微商城项目中用到的扫一扫这个功能,分享一下,希望对各位有所帮助. 前提:要有公众号,和通过微信认证,绑定域名,得到相应信息,appid,appsecret等. 微信开发文档:https://m ...
- vue项目中实现扫码功能
项目地址:https://github.com/wkl007/vue-scan-demo.git 项目主要是做的一个扫码的功能 核心代码为 <div class="scan" ...
- Android | 带你零代码实现安卓扫码功能
目录 小序 背景介绍 前期准备 开始搬运 结语 小序 这是一篇纯新手教学,本人之前没有任何安卓开发经验(尴尬),本文也不涉及任何代码就可以使用一个扫码demo,华为scankit真是新手的福音-- ...
随机推荐
- 用chrome浏览器进行前端debug和停止debug
首先F12打开控制台: 选择"source","Ctrl+Shift+F"搜索需要debug的代码关键词(Ctrl+O根据文件名搜索): 打开需要debug的文 ...
- 51nod 1305 Pairwise Sum and Divide
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = ...
- 软件测试过程中如何区分什么是功能bug,什么是需求bug,什么是设计bug?
问题描述: 测试过程中如何区分什么是功能bug,什么是需求bug,什么是设计bug? 精彩答案: 会员 土土的豆豆: 本期问题其实主要是针对不同方面或纬度上对于bug的一个归类和定位. 个人认为,从软 ...
- Mac上django 报错 [Errno 13] Permission denied: '/static'
将setting文件中的 改成:
- MongoDB 主从复制及 自动故障转移
1.MongoDB 主从复制 MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从 ...
- ES基本搜索(1)
1.空搜索 GET <写路径>/_search 返回的结果: eg: GET propdict/doc/_search { , "timed_out": false, ...
- Java - 框架之 SpringBoot 攻略day01
Spring-Boot 攻略 day01 spring-boot 一. 基本配置加运行 1. 导入配置文件(pom.xml 文件中) <parent> <gr ...
- UWB DWM1000 开源项目框架 之 温度采集
在之前博文开源一套uwb 框架,后面几篇博文会基于这个开源框架进行简单开发. 让uwb使用者更清楚了解基于这个basecode 开发工作. 这里所做内容是,采集dwm1000 温度,并发送到另一个节点 ...
- 入门node.js
我们现在要做一个简单的h5应用:包含登录.注册.修改密码.个人中心主页面.个人中心内页修改名称.个人中心修改手机号码. 第一步:工具安装,我选择了能够辅助我们快速开发的light开发工具 1. lig ...
- [TypeScript ] Using the Null Coalescing operator with TypeScript 3.7
The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set def ...