OpenResy+Lua 利用百度识图 将图片地址解析成文字
2015-05-20 11:17
711 查看
LUA代码:(注:LUA里有一个调用百度识图的接口IP:123.125.115.189(stu.baidu.com),不知为什么我的虚拟机无法解析stu.baidu.com,所以我只能PING出IP来用。)
HTML代码:
location /test { content_by_lua ' local request_method = ngx.var.request_method; local args = nil; if "GET" == request_method then args = ngx.req.get_uri_args(); else ngx.req.read_body(); args = ngx.req.get_post_args(); end local imageUrl = args["imageUrl"] function Split(szFullString, szSeparator) local nFindStartIndex = 1 local nSplitIndex = 1 local nSplitArray = {} while true do local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex) if not nFindLastIndex then nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString)) break end nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1) nFindStartIndex = nFindLastIndex + string.len(szSeparator) nSplitIndex = nSplitIndex + 1 end return nSplitArray end local cjson = require "cjson" local http = require "resty.http" local hc = http:new() local ok, code, headers, status, body = hc:request { url = "http://123.125.115.189/n/pc_search?queryImageUrl="..imageUrl, method = "GET", -- POST or GET } local _,aaa = string.find(body,"keywords:\'") local bbb = string.find(body,"|default") local ccc = string.sub(body,aaa+2,bbb-2) ngx.say(tostring(ccc)) '; }
HTML代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>Untitled</title> <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script> </head> <body> <input type="button" value = "test" id ="test" /> <input type="text" id ="imgUrl" /> <input type="button" value = "识图" id ="st" /> </body> <script> function unicode2Chr(str) { str=str.replace(/\\/g,"%"); return unescape(str); } $(function(){ $("#test").click(function(){ alert(); var str = "\x22\\u82f9\\u679c\x22,\x22\\u6307\\u6325\\u5927\\u5e08\x22,\x22\\u51fa\\u67dc\x22"; var code = str.replace(/\x22/g, "") alert(code); alert(unicode2Chr(code)); }); $("#st").click(function(){ var image_Url = $("#imgUrl").val(); $.ajax({ type : "POSST", async : false, url : "/test", data:{imageUrl:image_Url}, success : function(data) { var code = data.replace(/\\x22/g, "") alert(unicode2Chr(code).replace(/%/g, "")); } }); }); }); </script> </html>
相关文章推荐
- cocos2d-x之 利用富文本控件解析xhml标签(文字标签,图片标签,换行标签,标签属性)
- 用正则解析图片地址,并利用XMLHTTP组件将其保存
- 用正则解析图片地址,并利用XMLHTTP组件将其保存
- 用正则解析图片地址,并利用XMLHTTP组件将其保存[整理]
- 用正则解析图片地址,并利用XMLHTTP组件将其保存
- 用正则解析图片地址,并利用XMLHTTP组件将其保存
- asp.net后台InputStream存储图片,前台js接收解析存放路径地址
- 利用Brush修改图片,并写入Response一例(网上BBS中贴可变文字图片的例子)。
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 利用PHP 插件如何判断是否是有图新闻及获取图片地址教程
- 用asp自动解析网页中的图片地址,并将其保存到本地服务器
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- 一个解析URL及图片地址的JS函数
- 利用Google Map解析中文地址
- ASP自动解析网页中的图片地址,并将其保存到本地服务器http://epasser.aydc.com.cn/article/adp/2/content13959.html
- 【Lua正则表达式】解析匹配优酷电视剧集的下载地址用来批量下载
- 用asp解析图片地址,并将其保存
- 利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- 利用Google Map解析中文地址
- Android平台利用Zxing生成二维码与解析图片中的二维码