谷歌API抛出400错误但显示网址确定

时间:2017-04-10 03:37:40

标签: angular typescript ionic2 google-cloud-vision

该程序抛出了这两个错误:

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

EXCEPTION:状态响应:网址为400,确认

我对此错误进行了一些研究,如果枚举返回对于URL是正常的,则意味着URL正常并返回成功

来源: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();
    });
  }
}

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

      });
    }
    }

1 个答案:

答案 0 :(得分: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);
        }
      );
  }