如何在Ionic 2中使用原生智能手机相机?

Alw*_*ing 6 mobile camera cordova cordova-plugins ionic

Ionic 1似乎有一些cordova插件可以让你这样做.我真的需要逐步了解如何使用Ionic 2进行此操作.网上似乎没有资源可用.

谢谢!

Mar*_*und 2

在项目文件夹中,运行:

$ ionic 插件添加 cordova-plugin-camera --save

然后你就可以全局使用 navigator.camera 了。

import {Page, Platform, NavParams} from 'ionic/ionic'; import {NgZone} from 'angular2/core';






constructor(platform:Platform, navParams: NavParams, _zone : NgZone) {
this._zone = _zone;
this.platform = platform;
this.images = [];}



takePhoto() {
this.platform.ready().then(() => {
  let options = {
    quality: 80,
    destinationType: Camera.DestinationType.DATA_URL,
    sourceType: Camera.PictureSourceType.CAMERA,
    allowEdit: false,
    encodingType: Camera.EncodingType.JPEG,
    saveToPhotoAlbum: false
  };
  // https://github.com/apache/cordova-plugin-camera#module_camera.getPicture
  navigator.camera.getPicture(
    (data) => {
      let image = "data:image/jpeg;base64," + data;
       this._zone.run(()=> this.images.unshift({
         src: image
       }))
    }, (error) => {
      alert(error);
    }, options
  );
});}
Run Code Online (Sandbox Code Playgroud)

在这里做了一个演示项目;仅在Android上测试,请尝试。

https://github.com/marcusasplund/ionic2-camera-demo/