Crear un proyecto de tipo blank:
ionic start PruebaCamara blank
Acceder a la carpeta del proyecto
cd PruebaCamara
Instalar el plugin de la cámara:
ionic cordova plugin add cordova-plugin-camera
npm install @ionic-native/camera
home.page.html
<ion-header>
<ion-toolbar>
<ion-title>
Prueba de la cámara
</ion-title>
</ion-toolbar>
</ion-header>
<ion-content padding>
<ion-button (click)="hacerFoto()">Hacer foto</ion-button>
<img [src]="foto" *ngIf="foto" />
</ion-content>
home.page.ts
import { Component } from '@angular/core';
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
foto: any;
constructor(private camera: Camera) { }
hacerFoto() {
const options: CameraOptions = {
destinationType: this.camera.DestinationType.DATA_URL
}
this.camera.getPicture(options).then((imageData) => {
this.foto = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
console.log(err);
});
}
}
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { Camera } from '@ionic-native/camera/ngx';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
StatusBar,
SplashScreen,
Camera,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}