2017-04-07 56 views
0

我通過這段代碼得到的服務類圖像我如何通過圖像的功能離子2

this.cameraService.getImage(this.width, this.height, this.quality).subscribe(data => this.image = data, error => 

我會想這個代碼傳遞給getVision之一()函數使我能夠使用Google API.May我知道我該怎麼做?我試圖聲明一個字符串變量,並嘗試把上面的代碼的變量中但它不work.Below是代碼

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 and return DEFAULT_PHOTO from 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); 

    }); 
} 

} 

相機服務類

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 for taking an image with the camera 
     let imageOptions: any = { 
     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 a picture!', 
     message: "Select how you would like to add the picture", 
     enableBackdropDismiss: false, 
     buttons: [{ 
      text: 'Albums', 
      handler: data => { 
      //Change sourceType to PHOTOLIBRARY 
      imageOptions.sourceType = Camera.PictureSourceType.PHOTOLIBRARY; 
      selectAlert.dismiss(); 
      } 
     }, { 
      text: 'Camera', 
      handler: data => { 
      selectAlert.dismiss(); 
      } 
     }] 
     }); 

     selectAlert.onDidDismiss(() => { 
     this.getCameraImage(imageOptions).subscribe(image => { //image options are 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 is a base64 encoded string as per options set above 
      let base64Image: string = "data:image/jpeg;base64," + imageData; 
      observer.next(base64Image); 
      observer.complete(); 
     }, error => { 
      observer.error(error); 
     }); 
     }); 
    }); 
    } 
} 
+0

如果我理解正確,服務'this.cameraService.getImage(...)'將返回圖像。而這張圖片就是你想傳遞給'getVision()'的東西。如果在服務中出現錯誤,請執行其他操作,對嗎? –

+0

是的,你是對的 –

+0

在這種情況下,我認爲你需要將'getVision()'調用從錯誤回調轉換爲'subscribe()'。我會回答。 –

回答

1

您需要了解的數據通過Observable服務返回的信息可以在.subscribe()回調1中訪問,不在回調2中。請閱讀this以獲取更多說明。

this.cameraService.getImage(this.width,this.height,this.quality) 
    .subscribe((data) => { 
    this.image = data; 
    this.getVision(this.image); 
    },(error) => { 
    // Toast errot and return DEFAULT_PHOTO from Constants 
    this.toast(error); 
    } 
); 
+0

它可以工作,但我現在面臨的谷歌API錯誤EXCEPTION:狀態響應:400確定的網址: –

+0

因此,該解決方案適用於給定的問題現在。您所描述的錯誤與此無關。因此,請嘗試找到解決方案,如果找不到,請提出一個新問題。您可以通過接受答案來關閉此問題。 :) –

+0

謝謝你的主席的幫助! 。我真的很感激它,你的回答是清晰和簡潔的。將閱讀你提供的文件 –