ты пропустил самый важный шаг,
Убедитесь, что вы ввели camera
в провайдерах в app.module.ts
Вы должны создать файл PictureProvider, подобный этому
import { Injectable } from '@angular/core';
import { Defer } from '../../common/Defer';
import { Camera } from '@ionic-native/camera';
export enum PictureSource {
PhotoLibrary,
Camera,
Local,
Remote,
}
@Injectable()
export class PictureProvider {
constructor(private camera: Camera, ) {
console.log('Hello PictureProvider Provider');
}
fromCamera(source: PictureSource, options?, ) {
let mergedOtions = this.getOptions(source, options);
if (options)
for (let k in options)
mergedOtions[k] = options[k];
let defer = Defer.create();
navigator['camera'].getPicture(
imageUri => defer.resolve(imageUri),
(message: string) => {
console.log(message);
defer.reject();
},
mergedOtions,
);
return defer.promise;
}
private getOptions(source, options?) {
return {
sourceType: source == PictureSource.PhotoLibrary ? this.camera.PictureSourceType.PHOTOLIBRARY : this.camera.PictureSourceType.CAMERA,
destinationType: this.camera.DestinationType.NATIVE_URI,
quality: 50,
mediaType: this.camera.MediaType.PICTURE,
allowEdit: options.allowEdit,
correctOrientation: true,
targetWidth: options.targetWidth,
targetHeight: options.targetHeight
}
}
}
Выполнить по мере необходимости
this.picture.fromCamera(1, {
allowEdit: true,
targetWidth: 256,
targetHeight: 256,
destinationType: this.camera.DestinationType.DATA_URL, //直接返回base64
}).then(base64Img => {
this.headImg = this.encodeBase64Img(base64Img);
}).catch(Defer.NoOP);