javascript - How do i pass image to a function Ionic 2 -
i image service class via code
this.cameraservice.getimage(this.width, this.height, this.quality).subscribe(data => this.image = data, error =>
i want pass code 1 of getvision() function able use google api.may know how do it? tried declaring string variable , try put above code inside variable not work.below codes
camera.ts class export class camerapage { width: number; height: number; cropper: cropper;*/ image:string; width:number = 500; height:number = 500; quality:number = 90; picture:string; labels: array<any> = []; //translation scanning: array<any> = []; choselang: boolean = false; loading: boolean = false; constructor(public navctrl: navcontroller, public navparams: navparams,public testservice: testservice,public cameraservice: cameraservice,public toastctrl: toastcontroller) { } addphoto(){ //take picture & return image *** this.cameraservice.getimage(this.width, this.height, this.quality).subscribe(data => this.image = data, error => { this.getvision(this.image); // toast errot , return default_photo constants this.toast(error); }); } toast(message: string) { let toast = this.toastctrl.create({ message: message, duration: 2500, showclosebutton: false }); toast.present(); } getvision(image64:string) { this.testservice.getvisionlabels(image64:string) .subscribe((sub) => { this.labels = sub.responses[0].textannotations; this.gettext(); }); } gettext() { this.labels.foreach((label) => { let translation = {search: label.description, result: ''}; console.log(label.description); }); } }
camera service class
export class cameraservice { public base64image: string; constructor(public platform: platform, public alertctrl: alertcontroller, public modalctrl: modalcontroller, private http: http) { } getimage(width: number, height: number, quality: number) { return observable.create(observer => { //set default options taking image camera let imageoptions: = { quality: quality, destinationtype: camera.destinationtype.data_url, sourcetype: camera.picturesourcetype.camera, encodingtype: camera.encodingtype.jpeg, correctorientation: 1, savetophotoalbum: false, mediatype: camera.mediatype.picture, cameradirection: 1 }; let selectalert = this.alertctrl.create({ title: 'let\'s add picture!', message: "select how add picture", enablebackdropdismiss: false, buttons: [{ text: 'albums', handler: data => { //change sourcetype photolibrary imageoptions.sourcetype = camera.picturesourcetype.photolibrary; selectalert.dismiss(); } }, { text: 'camera', handler: data => { selectalert.dismiss(); } }] }); selectalert.ondiddismiss(() => { this.getcameraimage(imageoptions).subscribe(image => { //image options either album or camera** let cropmodal = this.modalctrl.create(scannerpage, { "imagebase64": image, "width": 500, "height": 500 }); cropmodal.ondiddismiss((croppedimage: any) => { if (!croppedimage) observer.error("canceled while cropping.") else { observer.next(croppedimage); observer.complete(); } }); cropmodal.present(); }, error => observer.error(error)); }); selectalert.present(); }); } getcameraimage(options: any) { //get base64 image return observable.create(observer => { this.platform.ready().then(() => { camera.getpicture(options).then((imagedata: any) => { // imagedata base64 encoded string per options set above let base64image: string = "data:image/jpeg;base64," + imagedata; observer.next(base64image); observer.complete(); }, error => { observer.error(error); }); }); }); } }
you need understand data returned observable service can accessed in .subscribe()
callback 1 , not in callback 2. read this more clarification.
this.cameraservice.getimage(this.width,this.height,this.quality) .subscribe( (data) => { this.image = data; this.getvision(this.image); },(error) => { // toast errot , return default_photo constants this.toast(error); } );
Comments
Post a Comment