没有相机供应商! injectionError

时间:2017-09-05 07:07:51

标签: cordova ionic3

我是Ionic 2的初学者。我想在Ionic中使用相机。我按照https://ionicframework.com/docs/native/camera/教程。 我已经安装了cordova-plugin-camera插件并使用cli代码安装了ionic-native / camera。

我在为项目提供服务时显示运行时错误:

  

未捕获(承诺):错误:没有相机提供商! injectionError

我发送了app.module.ts,html页面,并输入了脚本页面。请看看。

app.module.ts

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HttpModule } from '@angular/http';

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CameraExamplePage } from "../pages/camara-example/camara-example";

@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CameraExamplePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CameraExamplePage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

相机拍摄Html页面

<ion-header>


  <ion-navbar>

   <ion-title>camaraExample</ion-title>
  </ion-navbar>

</ion-header>


<ion-content padding>

   <button ion-button color="dark" (click)="takePhoto()" > Take Photo 
   </button>
   <img [src]="imageURL" *ngIf="imageURL">

 </ion-content>

** TypeScript文件**

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Camera, CameraOptions } from '@ionic-native/camera'

@IonicPage()
 @Component({
             selector: 'page-camara-example',
             templateUrl: 'camara-example.html',
            })
 export class CameraExamplePage {
 imageURL

 constructor(public navCtrl: NavController, public navParams: NavParams, public camera: Camera) {
 }
 ionViewDidLoad() {
    console.log('ionViewDidLoad CameraExamplePage');
 }

  takePhoto()
  {
    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;

  this.imageURL = imageData


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

 }


}

4 个答案:

答案 0 :(得分:17)

您需要在 app.module.ts

中将相机设置为提供者
 import { Camera } from '@ionic-native/camera';//import in app.module.ts

 //...

 providers: [
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler},
    Camera //here
  ]
  

我服务项目时显示运行时错误

注意:Cordova插件在离子服务中不起作用..您需要使用仿真器/设备。 另外,在this.platform.ready()中包含您的插件代码,并使用this.platform.is('cordova')检查cordova是否可用

import { Platform } from 'ionic-angular'; //import Platform
//...
constructor(public platform:Platform){}
//...
takePhoto() {
    this.platform.ready().then(() => {
        if(this.platform.is('cordova')){
            this.camera.getPicture(this.options).then((imageData) => {
                // imageData is either a base64 encoded string or a file URI
                // If it's base64 (DATA_URL):
                let base64Image = 'data:image/jpeg;base64,' + imageData;
            }, (err) => {
                // Handle error
            });
        }
    })
}

答案 1 :(得分:5)

您需要先使用两个命令安装相机插件

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

$ npm install --save @ ionic-native / camera

在您的 app.module.ts 之后,您需要import plugins并更改provider

import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HttpModule } from '@angular/http';

import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { CamaraExampalePage } from "../pages/camara-exampale/camara-exampale";
import { Camera, CameraOptions } from '@ionic-native/camera';
@NgModule({
  declarations: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CamaraExampalePage
  ],
  imports: [
    BrowserModule,
    HttpModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    AboutPage,
    ContactPage,
    HomePage,
    TabsPage,
    CamaraExampalePage
  ],
  providers: [
    Camera, CameraOptions,
    StatusBar,
    SplashScreen,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}

答案 2 :(得分:1)

自动生成的import语句存在问题。您需要:`从'@ ionic-native / camera / ngx'导入{Camera,CameraOptions};

答案 3 :(得分:0)

只需在app.module.ts中将Camera设置为提供者,还将其导入您的组件中……就像魔术一样工作!