前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ionic2实现扫描二维码功能

ionic2实现扫描二维码功能

作者头像
用户1437675
发布2018-08-20 11:18:31
1.3K0
发布2018-08-20 11:18:31
举报
文章被收录于专栏:Angular&服务Angular&服务

注意使用该插件不能使用

1. 安装插件

代码语言:javascript
复制
// 安装平台插件
ionic cordova plugin add cordova-plugin-qrscanner

// 安装ionic2插件
npm install --save @ionic-native/qr-scanner

2.使用插件

1.创建扫描二维码的页面
代码语言:javascript
复制
ionic generate page qrdcan
2.页面跳转到扫码页面

跳转方法

代码语言:javascript
复制
  //  跳转扫码页面
  goQrdcan(){
    this.navCtrl.push('QrdcanPage');
  }
3.扫码页面

// index.html

代码语言:javascript
复制
<ion-app style="background: none transparent;"></ion-app>

// qrdcan.html

代码语言:javascript
复制
<ion-header>
    <ion-navbar>
        <ion-title>扫码页面</ion-title>
    </ion-navbar>
</ion-header>

<ion-content no-scroll class="qrscanner">
    <div class="qrscanner-area">
    </div>
    <div class="through-line"></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>

// qrdcan.scss

代码语言:javascript
复制
page-qrdcan {
    .qrscanner {
        background: none;
        &-area {
        width: 100%;
        height: 85%;
        background: url(../assets/imgs/scanner.svg) no-repeat center center;
        background-size: contain;
        }
    }
    .through-line {
        left: 20%;
        width: 60%;
        height: 2px;
        background: red;
        position: absolute;
        animation: myfirst 5s linear infinite alternate;
    }
    @keyframes myfirst {
        0% {
            background: red;
            top: 180px;
        }
        25% {
            background: yellow;
            top: 220px;
        }
        50% {
            background: blue;
            top: 240px;
        }
        75% {
            background: green;
            top: 260px;
        }
        100% {
            background: red;
            top: 280px;
        }
    }
    .button-bottom {
        width: 128px;
        position: absolute;
        left: 50%;
        bottom: 80px;
        margin-left: -64px;
        .icon-camera {
            float: left;
        }
    }
}

// qrdcan.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { IonicPage, NavController, NavParams, ViewController } from 'ionic-angular';
import { QRScanner, QRScannerStatus } from '@ionic-native/qr-scanner';

/**
 * Generated class for the QrdcanPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-qrdcan',
  templateUrl: 'qrdcan.html',
})

export class QrdcanPage implements OnInit {

  // 控制闪光灯
  light: boolean = false;

  // 控制摄像头前后
  frontCamera: boolean = false;

  constructor(public navCtrl: NavController,
    public navParams: NavParams,
    private qrScanner: QRScanner,
    private viewCtrl: ViewController) {
  }

  ngOnInit() {
    this.qrScanner.prepare()
      .then((status: QRScannerStatus) => {
        if (status.authorized) {
          // camera permission was granted

          // start scanning
          let scanSub = this.qrScanner.scan().subscribe((text: string) => {
            alert(text);

            this.qrScanner.hide(); // hide camera preview
            scanSub.unsubscribe(); // stop scanning
          });

          // show camera preview
          this.qrScanner.show();

          // wait for user to scan something, then the observable callback will be called

        } else if (status.denied) {
          // 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.
        }
      })
      .catch((e: any) => console.log('Error is', e));
  }

  //页面加载完成事件
  ionViewDidLoad() {
    console.log('ionViewDidLoad QrdcanPage');
  }

  // 页面即将进来
  ionViewWillEnter() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'none';
      });
    }
  }

  // 页面即将离开
  ionViewWillLeave() {
    let elements = document.querySelectorAll(".tabbar");
    if (elements != null) {
      Object.keys(elements).map((key) => {
        elements[key].style.display = 'flex';
      });
    }
  }

  // 销毁当前页面
  dismiss(): void {
    this.viewCtrl.dismiss();
  }

  // 打开闪光灯功能
  toggleLight() {
    this.light = !this.light;
    if (this.light) {
      this.qrScanner.disableLight();
    } else {
      this.qrScanner.enableLight();

    }
  }

  // 切换摄像头功能
  toggleCamera() {
    this.frontCamera = !this.frontCamera;
    if (this.frontCamera) {
      this.qrScanner.useBackCamera();
    } else {
      this.qrScanner.useFrontCamera();
    }
  }


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.12.24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 安装插件
  • 2.使用插件
    • 1.创建扫描二维码的页面
      • 2.页面跳转到扫码页面
        • 3.扫码页面
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档