Ionic Cordova calls native hardware API implementation camera photo

Official website: https://ionicframework.com/docs/native/camera

Chinese: http://www.ionic.wang/native_doc-index-id-121.html< /p>

1. Install cordava plugin

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

2. Introduce and register the corresponding module in app.module.ts

import {Camera} from'@ionic-native/camera/ngx';
    ...
@NgModule({
    ...
    providers: [
        ...
        Camera
        ...
    ]
    ...
})
export class AppModule {}

3. Import and read the document in the used page

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

constructor(private camera: Camera) {}

...


const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.FILE_URI,
  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 (DATA_URL):
 let base64Image = ‘data:image/jpeg;base64,‘ + imageData;
}, (err) => {
 // Handle error
});

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

import {Camera} from ‘@ionic-native/camera/ngx’;
    ...
@NgModule({
    ...
    providers: [
        ...
        Camera
        ...
    ]
    ...
})
export class AppModule {}

import {Camera, CameraOptions} from ‘@ionic-native/camera/ngx’;

constructor(private camera: Camera) {}

...


const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.FILE_URI,
  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 (DATA_URL):
 let base64Image = ‘data:image/jpeg;base64,‘ + imageData;
}, (err) => {
 // Handle error
});

Leave a Comment

Your email address will not be published.