|
1 | 1 | import { Injectable } from '@angular/core'; |
2 | 2 | import { Platform } from 'ionic-angular'; |
3 | | -import { ImagePicker } from '@ionic-native/image-picker'; |
4 | 3 | import { UploadFS } from 'meteor/jalik:ufs'; |
5 | 4 | import { PicturesStore } from 'api/collections'; |
6 | 5 | import * as _ from 'lodash'; |
7 | 6 | import { DEFAULT_PICTURE_URL } from 'api/models'; |
| 7 | +import { Camera, CameraOptions } from '@ionic-native/camera'; |
| 8 | +import { Crop } from '@ionic-native/crop'; |
8 | 9 |
|
9 | 10 | @Injectable() |
10 | 11 | export class PictureService { |
11 | 12 | constructor(private platform: Platform, |
12 | | - private imagePicker: ImagePicker) { |
| 13 | + private camera: Camera, |
| 14 | + private crop: Crop) { |
13 | 15 | } |
14 | 16 |
|
15 | | - select(): Promise<File> { |
16 | | - if (!this.platform.is('cordova') || !this.platform.is('mobile')) { |
| 17 | + getPicture(camera: boolean, crop: boolean): Promise<File> { |
| 18 | + if (!this.platform.is('cordova')) { |
17 | 19 | return new Promise((resolve, reject) => { |
18 | | - try { |
19 | | - UploadFS.selectFile((file: File) => { |
20 | | - resolve(file); |
21 | | - }); |
22 | | - } |
23 | | - catch (e) { |
24 | | - reject(e); |
| 20 | + //TODO: add javascript image crop |
| 21 | + if (camera === true) { |
| 22 | + reject(new Error("Can't access the camera on Browser")); |
| 23 | + } else { |
| 24 | + try { |
| 25 | + UploadFS.selectFile((file: File) => { |
| 26 | + resolve(file); |
| 27 | + }); |
| 28 | + } catch (e) { |
| 29 | + reject(e); |
| 30 | + } |
25 | 31 | } |
26 | 32 | }); |
27 | 33 | } |
28 | 34 |
|
29 | | - return this.imagePicker.getPictures({maximumImagesCount: 1}).then((URL: string) => { |
30 | | - return this.convertURLtoBlob(URL); |
31 | | - }); |
| 35 | + return this.camera.getPicture(<CameraOptions>{ |
| 36 | + destinationType: 1, |
| 37 | + quality: 50, |
| 38 | + correctOrientation: true, |
| 39 | + saveToPhotoAlbum: false, |
| 40 | + sourceType: camera ? 1 : 0 |
| 41 | + }) |
| 42 | + .then((fileURI) => { |
| 43 | + return crop ? this.crop.crop(fileURI, {quality: 50}) : fileURI; |
| 44 | + }) |
| 45 | + .then((croppedFileURI) => { |
| 46 | + return this.convertURLtoBlob(croppedFileURI); |
| 47 | + }); |
32 | 48 | } |
33 | 49 |
|
34 | 50 | upload(blob: File): Promise<any> { |
|
0 commit comments