Hey Everyone, I want to build custom camera plugin for my organisation!
can anyone help me:
Here is my code but i can not use in project.
My plugin src/index.ts file
import { Injectable } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { File } from '@ionic-native/file/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { UUID } from 'angular2-uuid';
import { ActionSheetController, Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class CameraProvider {
protected options: CameraOptions;
protected translations = {
cancel: 'Cancel',
picture_library: '',
picture_options : '',
take_picture: '',
};
constructor (private file: File, public platform: Platform, public actionSheetCtrl: ActionSheetController,
public camera: Camera, public sanitizer: DomSanitizer,
private filePath: FilePath) {
console.log('run CameraProvider');
}
// Displays action sheet with options to take a picture or choose one from the photo library
public async savePhoto() {
return new Promise(async (resolve, reject) => {
const sheet = await this.actionSheetCtrl.create({
buttons: [
{
handler: () => {
sheet.onDidDismiss().then(() => {
this.savePicture(0).then((photoUrl) => {
resolve(photoUrl);
});
});
},
text: this.translations.take_picture,
},
{
handler: () => {
sheet.onDidDismiss().then(() => {
this.savePicture(1).then((photoUrl) => {
resolve(photoUrl);
});
});
},
text: this.translations.picture_library,
},
{ text: this.translations.cancel, role: 'cancel' },
],
header: this.translations.picture_options,
});
(await sheet).present();
});
}
// Stores a picture from the camera or photo library in the app data directory. Returns the url of the picture.
private async savePicture(type: number) {
return new Promise((resolve, reject) => {
this.camera.getPicture(this.optionsForType(type)).then((imageUrlToBeNormalized) => {
// Handle cases for different URLs
this.normalizeUrl(imageUrlToBeNormalized, type).then((imageUrl) => {
// Get file name and path
const name = imageUrl.substr(parseInt(imageUrl.lastIndexOf('/'), 10) + 1);
const namePath = imageUrl.substr(0, parseInt(imageUrl.lastIndexOf('/'), 10) + 1);
// Copy file
this.file.copyFile(namePath, name, this.file.dataDirectory, UUID.UUID()).then((file) => {
console.log('copy',JSON.stringify(file));
console.log('file.nativeURL',JSON.stringify(file.nativeURL));
resolve(file.nativeURL);
})
.catch((e) => {
console.log('copyFile',e);
reject(e);
});
});
})
.catch(e => {
console.log('getPicture',e);
});
});
}
// Fixes file paths when the Android Photo Library is used
private async normalizeUrl(uri: string, type: number): Promise<any> {
return new Promise((resolve, reject) => {
if(this.platform.is('android') && type === 1) {
this.filePath.resolveNativePath(uri).then(filePath => {
console.log('normalizeUrl filePath',filePath)
resolve(filePath);
})
.catch(err => console.log('normalizeUrl',err));
} else {
console.log('normalizeUrl filePath uri',uri)
resolve(uri);
}
});
}
// Camera plugin options
private optionsForType(type) {
const defaultOptions = {
allowEdit: this.platform.is('ios') ? true : false,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
saveToPhotoAlbum: false,
sourceType: type === 0 ? this.camera.PictureSourceType.CAMERA : this.camera.PictureSourceType.SAVEDPHOTOALBUM,
};
if (this.options) {
return {...defaultOptions, ...this.options} as any;
} else {
return defaultOptions;
}
}
// Sanitizes profile pictures for display in templates
public getProfileImage(image): SafeStyle {
return this.sanitizer.bypassSecurityTrustStyle(`url(${image ? image : 'assets/default-img'})`);
}
}
when i was trying to import in my ionic cordova project get some error
import { CameraProvider } from ‘@org/camera’;
Get some error like No suitable injection token for parameter ‘CameraProvider’ of class ‘HomePage’.
- also tried
@Inject(‘CameraProvider’) private CameraProvider: any // not work
HomePage.ts
import { Component,Inject } from '@angular/core';
import { CameraProvider } from '@org/camera';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
@Inject('CameraProvider') private CameraProvider: any
constructor() {
console.log(this.CameraProvider);
}
async addImage() {
const url = await this.CameraProvider.savePhoto(); // not working
}
}