Ionic开发文档

调用照相机拍照

发布时间 2018-5-23 15:58:08   浏览量()   收藏(1)

首先找到官方文档:https://ionicframework.com/docs/native/camera/

1、安装插件及模块

ionic cordova plugin add cordova-plugin-camera
npm install --save @ionic-native/camera

2、在app.modules.ts中引入并在providers中注册

import { Camera } from '@ionic-native/camera';

3、在用到的页面引用

import { Camera, CameraOptions } from '@ionic-native/camera';

constructor(private camera: Camera) { }

...


const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64:
 let base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});

31.png

例子:

  <button ion-button color="secondary" (click)="doCamera()">拍照</button>
  
  
  <img *ngIf="base64Image" [src]="base64Image" />
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { Device } from '@ionic-native/device';

import { Camera, CameraOptions } from '@ionic-native/camera';



@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  public uuid='';

  public model='';

  public platform='';

  public base64Image='';
  constructor(public navCtrl: NavController,private device: Device,private camera: Camera) {
    //  alert('Device UUID is: ' + this.device.uuid);

     this.uuid=this.device.uuid;

     this.model=this.device.model;

     this.platform=this.device.platform;

  }

  doCamera(){
    const options: CameraOptions = {
      quality: 100, //图片质量
      destinationType: this.camera.DestinationType.FILE_URI,  /*返回的类型*/
      encodingType: this.camera.EncodingType.JPEG,


      sourceType:this.camera.PictureSourceType.CAMERA,
      // mediaType: this.camera.MediaType.PICTURE,
      allowEdit:true,
      targetWidth:300,  /*宽度高度要设置*/
      targetHeight:300
    }
    
    this.camera.getPicture(options).then((imageData) => {
     // 返回拍照的地址     
        /*
        base64的地址

         this.camera.DestinationType.DATA_URL
         this.base64Image = 'data:image/jpeg;base64,' + imageData;
        */
        this.base64Image=imageData;

    }, (err) => {
      // Handle error
    });


  }

}


广告会让浏览体验不好,可这是网站的唯一收入,请点击下面的百度广告,支持老高的开源行动吧!