Google Cloud Vision在'requests [0] .image.content'获取无效值

时间:2016-03-10 19:06:58

标签: angularjs google-cloud-vision

我收到此错误:

Invalid value at 'requests[0].image.content' (TYPE_BYTES), "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/...

图像编码正确(它使用另一种需要相同base64编码的视觉搜索服务,metamind),这次调用出了什么问题?

这是来自AngularJS $http服务:

            $http({
                method: "POST",
                data: '{"requests":[{"image":{"content":"' + base64Img + '"},"features":[{"type":"LABEL_DETECTION","maxResults":1}]}]}',
                url: "https://vision.googleapis.com/v1/images:annotate?key=mykey27272772277227292992929"
            }).success(function (result) {
                console.log("SUCCESS");
                $scope.results = result;
                console.log(result);
            }).error(function (err) {
                console.log("FAIL");
                console.log(err);
            });

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

我刚刚意识到他们不想要"数据:image / jpeg; base64,"部分所以base64Img.slice(23)完成了这项工作

更新:由于切片在字符级别.slice(23)处于切断状态,因此适用于.png& .jpg(以及其他具有3个字符扩展名的类型),但如果用户上传类型' jpeg'额外的角色会破坏你的数据:

var headerless = "data:image/jpeg;base64" 
headerless.slice(23)

这将输出:"data:image/jpeg;base6",其余4将损坏您的数据。

我建议使用带有正则表达式语句的.replace代替此用例:

var image = <my image in b64 including header goes here>
var noHeader = image.replace(/^data:image\/(png|jpg|jpeg);base64,/, "")

答案 1 :(得分:0)

你可以直接在GCV上使用jpg格式。以下对我有用:

var source = 'http://www.abhishek.info/wp-content/uploads/2015/07/6-ID-Card-for-Downtown-Motors-Pvt-Limited.jpg';
  var apikey = 'yourkeyXXXXXXXXXXxxxxxx';
  $scope.param = '{"requests": [ { "image": { "source": { "imageUri": "' + source + '" } }, "features": [ { "type": "TEXT_DETECTION" } ] } ] }';
  $scope.url = 'https://vision.googleapis.com/v1/images:annotate?key=' + apikey;
  var url = $scope.url;
  var data = $scope.param;
  var config = {
    headers: {
      'Content-Type': 'application/json; charset=utf-8'
    }
  };
  $http.post(url, data,config).then(
    function(response) {
      console.log(response);
    },
    function(response) {
      console.log(response);
    });

  })