This commit is contained in:
tiago.kayaya
2021-11-09 15:28:40 +01:00
parent f7c8942444
commit 3a46b77e36
6 changed files with 69 additions and 63 deletions
+60 -59
View File
@@ -2,14 +2,14 @@ import { Injectable } from '@angular/core';
import { FileLoaderService } from '../file/file-loader.service';
import { FileToBase64Service } from '../file/file-to-base64.service';
import { InAppBrowser } from '@ionic-native/in-app-browser/ngx';
//Cordova
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { ChatService } from '../chat.service';
import { ModalController } from '@ionic/angular';
import { SearchPage } from 'src/app/pages/search/search.page';
import { SearchList } from 'src/app/models/search-document';
import { ProcessesService } from '../processes.service';
import { ToastService } from '../toast.service';
import { Camera, CameraResultType, CameraSource, Photo} from '@capacitor/camera';
@Injectable({
providedIn: 'root'
@@ -22,9 +22,9 @@ export class FileService {
showLoader: boolean;
files: Set<File>;
photos: any[] = [];
constructor(
private camera: Camera,
private fileLoaderService: FileLoaderService,
private fileToBase64Service: FileToBase64Service,
private iab: InAppBrowser,
@@ -34,26 +34,25 @@ export class FileService {
private toastService: ToastService,
) { }
takePicture() {
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
async takePicture() {
const capturedImage = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Camera
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL): m
//let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = 'data:image/png;base64,'+imageData;
this.capturedImageTitle = new Date().getTime() + '.jpeg';
}, (err) => {
/* console.log(err); */
});
const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.photos.unshift({
filepath: "soon...",
webviewPath: capturedImage.webPath
});
this.capturedImage = await this.convertBlobToBase64(blob);
this.capturedImageTitle = new Date().getTime() + '.jpeg';
let data = {
image:this.capturedImage,
name: this.capturedImageTitle
@@ -62,6 +61,15 @@ export class FileService {
return data;
}
convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
const reader = new FileReader;
reader.onerror = reject;
reader.onload = () => {
resolve(reader.result);
};
reader.readAsDataURL(blob);
});
loadPicture() {
const input = this.fileLoaderService.createInput({
accept: ['image/apng', 'image/jpeg', 'image/png']
@@ -85,19 +93,24 @@ export class FileService {
};
}
addCameraPictureToChat(roomId){
async addCameraPictureToChat(roomId){
const options: CameraOptions = {
quality: 50,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
}
const capturedImage = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Camera
this.camera.getPicture(options).then((imageData) => {
this.capturedImage = 'data:image/png;base64,'+imageData;
});
const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.photos.unshift({
filepath: "soon...",
webviewPath: capturedImage.webPath
});
this.capturedImage = await this.convertBlobToBase64(blob);
this.capturedImageTitle = new Date().getTime() + '.jpeg';
let body = {
@@ -121,29 +134,26 @@ export class FileService {
this.toastService.badRequest("Não foi possível adicionar a fotografia!");
});
}, (err) => {
this.toastService.badRequest("Não foi possível adicionar a fotografia!");
});
}
addPictureToChatMobile(roomId) {
alert('Here')
async addPictureToChatMobile(roomId) {
const options: CameraOptions = {
const capturedImage = await Camera.getPhoto({
quality: 90,
sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
destinationType: this.camera.DestinationType.DATA_URL,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE,
targetWidth: 720,
targetHeight: 720,
correctOrientation: true
}
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Camera
this.camera.getPicture(options).then((imageData) => {
let base64Image = 'data:image/jpeg;base64,' + imageData;
this.capturedImage = imageData;
});
const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.photos.unshift({
filepath: "soon...",
webviewPath: capturedImage.webPath
});
this.capturedImage = await this.convertBlobToBase64(blob);
this.capturedImageTitle = new Date().getTime() + '.jpeg';
//const loader = this.toastService.loading();
@@ -161,21 +171,12 @@ export class FileService {
}]
}
}
console.log(this.capturedImage)
this.chatService.sendMessage(body).subscribe(res=> {
//loader.remove();
//console.log(res);
},(error) => {
//loader.remove();
});
}, (err) => {
//console.log(err);
});
}
addPictureToChat(roomId) {