2017-04-10 64 views
0

程序引发此2个错误:谷歌API抛出400错误还显示网址OK

POST https://vision.googleapis.com/v1/images:annotate?key=APIKEY

例外:响应,状态:400 OK的网址:

我做了关于一些研究这个错误是,如果枚举回报是URL OK意味着该URL是OK和返回成功

来源:https://cloud.google.com/vision/docs/reference/rest/v1/Code

在我添加裁剪功能之前,API可以工作,但是它在这里抛出错误。我不是否我是否在app.components.ts中的提供者中声明了2个服务类是错误的?。该程序的逻辑是拍摄一个列表的图片,然后程序会将用户带到一个裁剪页面,其中用户可以裁剪图像,然后程序将图像传回到camera.ts页面,在那里它将图像传递给方法getVision(),该方法将图片发布到googleAPI和console.log()图片中的文本。下面是代码

@Component({ 
    templateUrl: 'app.html', 

    providers:[CameraService,TestService] 


}) 
export class MyApp { 
    rootPage = TabsPage; 

    constructor(platform: Platform) { 
    platform.ready().then(() => { 

     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
    }); 
    } 
} 

测试,service.ts

@Injectable() 
export class TestService { 

    apiKeys = { 
    cloudVision :'ApiKey', 

    }; 

    apiUrls = { 
    cloudVision : 'https://vision.googleapis.com/v1/images:annotate?key=', 

    }; 


    visionPostObj = { 
    requests : [ 
     { 
     image : { 
      content : '' 
     }, 
     features: { 
      type: 'TEXT_DETECTION', 
      maxResults: 1 
     } 
     } 
    ] 
    }; 

    static get parameters() { 
    return [[Http]]; 
    } 



    constructor(private http: Http) { 
    console.log('Hello CameraService Provider'); 

    } 




    getVisionLabels(image : string){ 

    let url = this.apiUrls.cloudVision + this.apiKeys.cloudVision; 
    let post = this.visionPostObj; 
    post.requests[0].image.content = image; 

    return this.http.post(url, post).map((res) => res.json()); 
    } 

camera.ts

constructor(public navCtrl: NavController, public navParams: NavParams,public testService: TestService,public cameraService: CameraService,public toastCtrl: ToastController) { 

    } 

    addPhoto(){ 


     this.cameraService.getImage(this.width,this.height,this.quality) 
      .subscribe((data) => { 
       this.image = data; 
       this.getVision(this.image); 
      //console.log(this.image); 
      },(error) => { 
       // 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) 
     .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); 

     }); 
    } 
    } 

回答

0

我已经解决了问题。由于base64字符串包含“data:image/jpeg; base64”,反过来会引发此错误。因此,我需要从base64字符串图像中删除该数据。

下面是代码

addPhoto(){ 


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


      this.imageConvert = this.image.replace(/^data:image\/[a-z]+;base64,/, ""); 

      this.getVision(this.imageConvert); 



     },(error) => { 
      // Toast errot and return DEFAULT_PHOTO from Constants 
      this.toast(error); 
     } 
    ); 
    }