Javascript 如何将图像传递给函数Ionic 2

Javascript 如何将图像传递给函数Ionic 2,javascript,angular,typescript,ionic2,Javascript,Angular,Typescript,Ionic2,我通过这个代码从服务类获取图像 this.cameraService.getImage(this.width, this.height, this.quality).subscribe(data => this.image = data, error => 我想把这段代码传递给一个getVision函数,这样我就可以使用GoogleAPI了。我可以知道怎么做吗?我尝试声明一个字符串变量,并尝试将上面的代码放入变量中,但它不起作用 Camera.TS class expo

我通过这个代码从服务类获取图像

 this.cameraService.getImage(this.width, this.height, this.quality).subscribe(data => this.image = data, error =>
我想把这段代码传递给一个getVision函数,这样我就可以使用GoogleAPI了。我可以知道怎么做吗?我尝试声明一个字符串变量,并尝试将上面的代码放入变量中,但它不起作用

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);

  });
}

}

您需要了解,可观察服务返回的数据可以在.subscribe回调1中访问,而不能在回调2中访问。阅读更多的澄清

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);
  }
);

如果我理解正确的话,服务应该是.cameraService.getImage。。。返回图像。这张图片就是你想要传递给getVision的。如果服务中出现错误,请执行其他操作,对吗?是的,您是对的。在这种情况下,我认为您需要将getVision调用从error callback转换为subscribe。我将回答。getVision并且正在另一个服务类中。我将根据您的代码更新这个问题。getVision方法是在同一个类CameraPage中编写的。很明显,它可以工作,但我现在面临google API错误异常:响应状态:400 URL:OK:所以解决方案现在可以解决给定的问题了。您描述的错误与此无关。因此,尝试找到解决方案,如果找不到,请提出新问题。您可以通过接受答案来结束此问题:谢谢你先生的帮助。我真的很感激你,你的回答清晰明了。我会阅读你提供的文件。在发布问题之前,只需进行一次基本的搜索,并明确代码的确切概念行为。
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);
  }
);