Hello,
I am trying to POST a local image (photo2.jpg) to Google Vision API, but I am stuck with error message:
Invalid value at 'requests[0].image.content' (TYPE\_BYTES), Base64 decoding failed for \"..base64\""
The code is:
-- loading the binary file local path2 = system.pathForFile( "photo2.jpg", system.DocumentsDirectory ) local file2 = io.open(path2,"rb") local temp = file2:read("\*a") io.close(file2) file2 = nil -- converting the binary stored in temp to base64 local function base64encode(data) local len = data:len() local t = {} for i=1,len,384 do local n = math.min(384, len+1-i) if n \> 0 then local s = data:sub(i, i+n-1) local enc, \_ = mime.b64(s) t[#t+1] = enc end end return table.concat(t) end local base64 = base64encode(temp) local function networkListener( event ) local res = json.prettify( event.response ) local decoded = json.decode( res ) if ( event.isError ) then print( "Network error: ", event.response ) else print( "Result: " .. ( res ) ) --This seems to display Base64 properly print(base64) end end --Somehow Base64 decoding failed! local headers = {} headers["Content-Type"] = "application/json" local body = '{"requests": [{"image": {"content": "..base64"},"features": [{"type": "TEXT\_DETECTION"}]}]}' local params = {} params.headers = headers params.body = body network.request( "https://vision.googleapis.com/v1/images:annotate?key="..apikey, "POST", networkListener, params )
I also tried the following simpler function (in place of base64encode function above):
local mime = require("mime") local result = mime.b64(temp)
It returns the same error.
In both cases, the Corona Simulator console seems to output Base64 data properly…
I look at documentation of the Google API and check Base64 related websites, but run out of ideas how to solve this. Do you have similar experience? Any hint?