获取图片图片识别常用的LUA

----------------------------------------------------------------------
--请关注 接口函数区 三个函数 (1)查询信息 GetScore, (2)识别图片PostPic, (3)报错返分ReportError
----------------------------------------------------------------------
-------------------------------------------功能函数区-------------------------------------------
-- 读取文件
function ReadFile(path, isdel)
  local iRet, sRet = pcall(function()
    local f = io.open(path, "r")
    if f == null then
      return ""
    end
    local ret = f:read("*all")
    f:close()
    if isdel == true then
      os.remove(path)
    end
    return ret
  end)
  if iRet == true then
    return sRet
  else
    print(sRet)
    return ""
  end
end

function print(str)
  LuaAuxLib.TracePrint(str)
end
--读取文件二进制
function ReadFileByte(file)
  local f = io.open(file,"rb")
  local retbyte = f:read("*all")
  f:close()
  return retbyte
end
--读取文件转换成base64编码
function ReadFileBase(path)
  local iRet, sRet = pcall(function()
    f = io.open(path,"rb")
    if f == nil then 
      return nil
    end 
    bytes = f:read("*all")
    f:close()
    local key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
    return ((bytes:gsub('.', function(x) 
      local r,key='',x:byte()
      for i=8,1,-1 do r=r..(key%2^i-key%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 key:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#bytes%3+1])
  end)
  if iRet == true then
    return sRet
  else
    return ""
  end
end 

--
  ---------------------------------- json解析 ----------------------------------
--

local json = {}
-- JSON 编码数据
function json.encode (v)
  if v==nil then return "null" end
  local vtype = type(v)
  -- 字符串
  if vtype=='string' then return '"' .. json_encodeString(v) .. '"' end
  -- 数学、逻辑型
  if vtype=='number' or vtype=='boolean' then return tostring(v) end
  -- 表
  if vtype=='table' then
    local rval = {}
    -- 判断表是否为数组
    local bArray, maxCount = json_isArray(v)
    if bArray then
      for i = 1, maxCount do table.insert(rval, json.encode(v[i])) end
    else
      for i, j in pairs(v) do
        if json_isEncodable(i) and json_isEncodable(j) then table.insert(rval, '"' .. json_encodeString(i) .. '":' .. json.encode(j)) end
      end
    end
    if bArray then
      return '[' .. table.concat(rval,',') ..']'
    else
      return '{' .. table.concat(rval,',') .. '}'
    end
  end
  -- 函数型(不支持)
  if vtype=='function' then return "null" end
  -- 类型断言
  assert(false, "JSON编码时发现了不支持的数据类型 " .. vtype .. ":" .. tostring(v))
end

-- JSON 解码数据
function json.decode(s, startPos)
  startPos = startPos and startPos or 1
  startPos = decode_scanWhitespace(s, startPos)
  assert(startPos <= string.len(s), "数据遍历位置超过结尾 [" .. s .. "]")
  local curChar = string.sub(s, startPos, startPos)
  -- 对象
  if curChar=='{' then return decode_scanObject(s, startPos) end
  -- 数组
  if curChar=='[' then return decode_scanArray(s, startPos) end
  -- 数字
  if string.find("+-0123456789.e", curChar, 1, true) then return decode_scanNumber(s, startPos) end
  -- 字符串
  if curChar==[["]] or curChar==[[']] then return decode_scanString(s, startPos) end
  -- 注释
  if string.sub(s,startPos,startPos+1)=='/*' then return decode(s, decode_scanComment(s, startPos)) end
  -- 常数
  return decode_scanConstant(s, startPos)
end

-- 扫描数组 [s:JSON串, startPos:扫描位置] [1:还原的表, 2:新的扫描位置]
function decode_scanArray(s, startPos)
  local array = {}
  local stringLen = string.len(s)
  assert(string.sub(s, startPos, startPos)=='[','扫描的位置不是数组数据的起点,位置:' .. startPos .. '\nJSON字符串:'..s)
  startPos = startPos + 1
  repeat
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到数组结尾。')
    local curChar = string.sub(s, startPos, startPos)
    if (curChar==']') then return array, startPos + 1 end
    if (curChar==',') then startPos = decode_scanWhitespace(s,startPos+1) end
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到数组结尾。')
    object, startPos = json.decode(s,startPos)
    table.insert(array,object)
  until false
end

-- 扫描注释 [s:JSON串, startPos:扫描位置] [新的扫描位置]
function decode_scanComment(s, startPos)
  assert(string.sub(s, startPos, startPos + 1)=='/*', "扫描的位置不是注释数据的起点,位置:" .. startPos .. '\nJSON字符串:'..s)
  local endPos = string.find(s, '*/', startPos + 2)
  assert(endPos ~= nil, "JSON字符串扫描已结束,但没有找到注释结尾。")
  return endPos + 2
end

-- 扫描常数 [s:JSON串, startPos:扫描位置] [1:还原的数据, 2:新的扫描位置]
function decode_scanConstant(s, startPos)
  local consts = { ["true"] = true, ["false"] = false, ["null"] = nil }
  local constNames = {"true","false","null"}
  for i, k in pairs(constNames) do
    if string.sub(s,startPos, startPos + string.len(k) -1 ) == k then return consts[k], startPos + string.len(k) end
  end
  assert(nil, '无法找到标识符对应的值:' .. s .. ',位置:' .. startPos)
end

-- 扫描数字 [s:JSON串, startPos:扫描位置] [1:还原的数字, 2:新的扫描位置]
function decode_scanNumber(s, startPos)
  local endPos = startPos+1
  local stringLen = string.len(s)
  local acceptableChars = "+-0123456789.e"
  while (string.find(acceptableChars, string.sub(s,endPos,endPos), 1, true) and endPos<=stringLen) do endPos = endPos + 1 end
  local stringValue = 'return ' .. string.sub(s,startPos, endPos-1)
  local stringEval = load(stringValue)
  assert(stringEval, '扫描数字失败,' .. stringValue .. '在JSON字符串的位置:' .. startPos .. ' : ' .. endPos)
  return stringEval(), endPos
end

-- 扫描对象 [s:JSON串, startPos:扫描位置] [1:还原的表, 2:新的扫描位置]
function decode_scanObject(s, startPos)
  local object = {}
  local stringLen = string.len(s)
  local key, value
  assert(string.sub(s, startPos, startPos) == '{', '扫描的位置不是对象数据的起点,位置:' .. startPos .. '\nJSON字符串:'..s)
  startPos = startPos + 1
  repeat
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到对象结尾。')
    local curChar = string.sub(s,startPos,startPos)
    if (curChar=='}') then return object,startPos+1 end
    if (curChar==',') then startPos = decode_scanWhitespace(s,startPos+1) end
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到对象结尾。')
    key, startPos = json.decode(s,startPos)
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到键值:' .. key)
    startPos = decode_scanWhitespace(s,startPos)
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到键值:' .. key)
    assert(string.sub(s,startPos,startPos)==':','JSON对象键值格式异常,错误位置:' .. startPos)
    startPos = decode_scanWhitespace(s,startPos+1)
    assert(startPos<=stringLen, 'JSON字符串扫描已结束,但没有找到键值:' .. key)
    value, startPos = json.decode(s,startPos)
    object[key]=value
  until false
end

-- 扫描字符串 [s:JSON串, startPos:扫描位置] [1:还原的字符串, 2:新的扫描位置]
local escapeSequences = {["\\t"] = "\t", ["\\f"] = "\f", ["\\r"] = "\r", ["\\n"] = "\n", ["\\b"] = "\b"}
setmetatable(escapeSequences, {__index = function(t, k) return string.sub(k,2) end})
function decode_scanString(s, startPos)
  assert(startPos, '扫描字符串时没有填写起始位置。')
  local startChar = string.sub(s, startPos, startPos)
  assert(startChar == [["]] or startChar == [[']], '非法的字符串格式。')
  local t = {}
  local i, j = startPos, startPos
  while string.find(s, startChar, j+1) ~= j+1 do
    local oldj = j
    i, j = string.find(s, "\\.", j+1)
    local x, y = string.find(s, startChar, oldj+1)
    if not i or x < i then i, j = x, y-1 end
    table.insert(t, string.sub(s, oldj+1, i-1))
    if string.sub(s, i, j) == "\\u" then
      local a = string.sub(s, j+1, j+4)
      j = j + 4
      local n = tonumber(a, 16)
      assert(n, "字符串UNICODE转译失败," .. a .. " 无法转换为数字,出错位置:" .. i .. " : " .. j)
      local x
      if n < 0x80 then
        x = string.char(n % 0x80)
      elseif n < 0x800 then	-- [110x xxxx] [10xx xxxx]
        x = string.char(0xC0 + (math.floor(n/64) % 0x20), 0x80 + (n % 0x40))
      else					-- [1110 xxxx] [10xx xxxx] [10xx xxxx]
        x = string.char(0xE0 + (math.floor(n/4096) % 0x10), 0x80 + (math.floor(n/64) % 0x40), 0x80 + (n % 0x40))
      end
      table.insert(t, x)
    else
      table.insert(t, escapeSequences[string.sub(s, i, j)])
    end
  end
  table.insert(t,string.sub(j, j+1))
  assert(string.find(s, startChar, j+1), "字符串扫描失败,找不到关闭符号。出错位置:开始(" .. startChar .. ") 结束(" .. startPos .. ")")
  return table.concat(t,""), j+2
end

-- 扫描字符串返回下一个非白字符 [空格、Tab、换行、回车]
function decode_scanWhitespace(s,startPos)
  local whitespace=" \n\r\t"
  local stringLen = string.len(s)
  while ( string.find(whitespace, string.sub(s,startPos,startPos), 1, true)  and startPos <= stringLen) do startPos = startPos + 1 end
  return startPos
end

-- 返回可以编码的字符串
local json_escapeList = {['"']  = '\\"', ['\\'] = '\\\\', ['/']  = '\\/',  ['\b'] = '\\b', ['\f'] = '\\f', ['\n'] = '\\n', ['\r'] = '\\r', ['\t'] = '\\t'}
function json_encodeString(s)
  local s = tostring(s)
  return s:gsub(".", function(c) return json_escapeList[c] end)
end

-- 判断一个表是否为数组
function json_isArray(t)
  local maxIndex = 0
  for k, v in pairs(t) do
    if (type(k)=='number' and math.floor(k)==k and 1<=k) then
      if (not json_isEncodable(v)) then return false end
      maxIndex = math.max(maxIndex,k)
    else
      if (k=='n') then
        if v ~= table.getn(t) then return false end
      else
        if json_isEncodable(v) then return false end
      end
    end
  end
  return true, maxIndex
end

-- 判断数据是否可编码
function json_isEncodable(o)
  local t = type(o)
  return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null)
end

function PostHttp(url,heard)
  return LuaAuxLib.URL_OperationPost(url,heard,60)
end



-------------------------------------------超级鹰接口函数区-------------------------------------------

--查询信息
function QMPlugin.GetScore(user,pass)
  local PostHost = "http://www.98lm.com/Upload/GetScore.php"
  
  --添加数据到table
  local formtable = {}
  formtable["user"] = user
  formtable["pass2"] = pass
  
  --table转json
  local formjson = json.encode(formtable)
  
  --提交信息
  local retjson = PostHttp(PostHost,formjson)	
  return retjson
end


--识别图片
function QMPlugin.PostPic(user,pass,softid,codetype,len_min,pathfile)
  local PostHost = "http://www.98lm.com/Upload/Processing.php"
  
  local tempRet = ReadFile(pathfile)
  if tempRet == "" then
    return "file null"
  end
  
  --添加数据到table
  local formtable = {}
  formtable["user"] = user
  formtable["pass2"] = pass
  formtable["softid"] = softid
  formtable["codetype"] = codetype
  formtable["len_min"] = len_min
  formtable["file_base64"] = ReadFileBase(pathfile)
  
  --table转json
  local formjson = json.encode(formtable)
  
  --提交信息
  local retjson = PostHttp(PostHost,formjson)	
  return retjson
end


--报错返分
function QMPlugin.ReportError(user,pass,id,softid)
  local PostHost = "http://www.98lm.com/Upload/ReportError.php"
  
  --添加数据到table
  local formtable = {}
  formtable["user"] = user
  formtable["pass2"] = pass
  formtable["id"] = id
  formtable["softid"] = softid
  
  --table转json
  local formjson = json.encode(formtable)
  
  --提交信息
  local retjson = PostHttp(PostHost,formjson)	
  return retjson
end

 

赞 (0)