接收到无效的JSON有效负载。无法解析数字(Google Vision API)

时间:2019-09-25 03:01:13

标签: json api cordova vision

我遇到了错误:

Invalid JSON payload received. Unable to parse number. INVALID_ARGUMENT

我知道了密钥和所有内容,但仍然有相同的错误返回。一些建议是首先调整图像大小,以使其不适合4MB图像限制。

我还尝试模拟来自其文档的相同JSON请求。但是仍然有同样的问题。 https://cloud.google.com/vision/docs/drag-and-drop

这是我使用的代码:(我从Akshay Kadam获得了示例)

        me.detection_types = {
        LABEL_DETECTION: 'label',
        DOCUMENT_TEXT_DETECTION: 'text',
        LOGO_DETECTION: 'logo',
        LANDMARK_DETECTION: 'landmark'
        };

        var api_key = '';
        $scope.takePicture = function(){

            var options = {
            destinationType: Camera.DestinationType.DATA_URL,
            sourceType: Camera.PictureSourceType.CAMERA,
            targetWidth: 150,
            targetHeight: 150,
            correctOrientation: true,
            cameraDirection: 0,
            allowEdit: true,
            encodingType: Camera.EncodingType.JPEG,
            quality: 25
            };

            $cordovaCamera.getPicture(options).then(function(imagedata){

                me.current_image = "data:image/jpeg;base64," + imagedata;
                me.image_description = '';
                me.locale = '';

                var vision_api_json = {
                  "requests":[
                    {
                      "image":{
                        "content": imagedata
                      },
                      "imageContext": {
                        "cropHintsParams": {
                          "aspectRatios": [
                            0.8,
                            1,
                            1.2
                          ]
                        }
                      },
                      "features":[
                        {
                          "type": me.detection_type,
                          "maxResults": 1
                        }
                      ]
                    }
                  ]
                }

                var file_contents = JSON.stringify(vision_api_json);                

                $cordovaFile.writeFile(
                    cordova.file.applicationStorageDirectory,
                    'file.json',
                    file_contents,
                    true
                ).then(function(result){                    
                    var headers = {
                        'Content-Type': 'application/json'                                      
                    };

                    options.headers = headers;

                    var server = 'https://vision.googleapis.com/v1/images:annotate?key=' + api_key;
                    var filePath = cordova.file.applicationStorageDirectory + 'file.json';                                      
                    $cordovaFileTransfer.upload(server, filePath, options, true)
                        .then(function(result){
                            alert(JSON.stringify(result))
                            var res = JSON.parse(result.response);
                            var key = me.detection_types[me.detection_type] + 'Annotations';

                            me.image_description = res.responses[0][key][0].description;
                      }




需要帮助。谢谢!

更新: 我尝试添加Content-Length,但仍然遇到相同的问题。

0 个答案:

没有答案