2017-04-10 58 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); 
     } 
    ); 
    } 
相關問題