Oh your service is making this a challenge… Our upload function doesn’t want to pass additional parameters to the host via the body structure and you have to use the body parameter to send in the API key and the language. I think you were more on the right track (since we can’t pass additional post parameters using the network.upload() call)
So lets go back to your original code (and if you’re on Android and need to test from the Resource directory, you will need to follow Josh’s steps above too.
You need to read the data in from the file, not just open it. Then you will need to base64 encode the data.
[lua]
local function _b64enc( data )
– character table string
local b=‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/’
return ( (data:gsub( ‘.’, function( x )
local r,b=’’, x:byte()
for i=8,1,-1 do r=r … ( b % 2 ^ i - b % 2 ^ ( i - 1 ) > 0 and ‘1’ or ‘0’ ) end
return r;
end ) …‘0000’ ):gsub( ‘%d%d%d?%d?%d?%d?’, function( x )
if ( #x < 6 ) then return ‘’ end
local c = 0
for i = 1, 6 do c = c + ( x:sub( i, i ) == ‘1’ and 2 ^ ( 6 - i ) or 0 ) end
return b:sub( c+1, c+1 )
end) … ( { ‘’, ‘==’, ‘=’ } )[#data %3 + 1] )
end
local path = system.pathForFile(“ocr1.jpg”, system.ResourceDirectory)
local file, errStr = io.open( path, “rb” )
if file then
local content = file:read( “*a” )
local img = _b64enc(content)
local headers = {}
headers[“Content-Type”] = “multipart/form-data”
local body = “image=” … img … “&language=” … language … “&apikey=” … ocrapiserviceApiKey
local params = {}
params.headers = headers
params.body = body
network.request(“http://api.ocrapiservice.com/1.0/rest/ocr”, “POST”, networkListener, params)
end
[/lua]