2017-04-07 24 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

谢谢你的主席的帮助! 。我真的很感激它,你的回答是清晰和简洁的。将阅读你提供的文件 –