站长原创,版权所有ITEEDU,2011-07-25
在经历了一系列尝试之后,老板要求我将Ocr做成一个服务器,也就说实现:从客户端接收其传过来的图片内容,并对其进行解析,存放成本地临时图片,然后在调用我编写的的imagegick接口函数,将传过来的.jpg图片转换成.bmp图片,然后再调用我编写的ocr接口函数,将.bmp图片上的文字信息提取出来,并且通过web将其发送给客户端。 本地上测试时,我们安装了apache2服务器,第一次测试用的是CGI来实现的,可是CGI无法对图片的内容进行正确的提取,甚至一提取就会出错。我在本地上的测试是通过这样子进行的:
(1) 新建一个html的网页,具体内容如下(html代码),并且将该html取名为index.html,并且将其放置在apache2的数据目录:/var/www/下边。注意:action对象的值为:/cgi-bin/cgicc.cgi。测试该网页:在浏览器的地址栏中输入:http://localhost/cgi.html,其运行结果如图1所示。
Cgi.html文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html lang="en" dir="ltr" > <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <head> <style><!-- body { color: black; background: white; } span.red { color:red; } hr.half { width: 60%; margin-left: auto; margin-right: auto; } div.center { text-align: center; } div.notice { border: solid thin; padding: 1em; margin: 1em 0; background: #ddd; text-align: center; }table { width: 90%; margin-left: auto; margin-right: auto; } tr.title, td.title { color: white; background: black; font-weight: bold; text-align: center; } tr.data, td.data { background: #ddd; } td.form { background: #ddd; text-align: center; } --></style>
<title>GNU cgicc v3.2.9 File Upload Test</title> </head> <body> <h1>GNU cgi<span class="red" >cc</span> v3.2.9 File Upload Test</h1> <!-- This page generated by cgicc for --> <h4>Thanks for using cgi<span class="red" >cc</span>, (192.168.3.15)!</h4> <br /> <form method="post" action="/cgi-bin/cgicc.cgi" enctype="multipart/form-data"> <table>
<tr><td class="title">Send a file</td><td class="form"><input type="file" name="userfile" accept="text/plain" /></td></tr>
<tr><td class="title">Upload Redirection</td><td class="form"><input type="checkbox" name="redirect" />Bounce uploaded file back to browser</td></tr>
</table> <div class="center"><p><input type="submit" name="submit" value="Send the file" /><input type="reset" value="Nevermind" /></p></div></form>
<hr class="half" /> <div align="center" class="smaller" > GNU cgi<span class="red" >cc</span> v3.2.9<br /> Compiled at 11:58:36 on Jul 12 2011<br /> Configured for x86_64-unknown-linux-gnu. Running on Linux 2.6.32-21-server (ubuntuserver). <br />Total time for request = 1008 us (0.001008 s)</div> </body></html>
(2) 编写提取cgi.html发送过来的信息,其代码如下:(代码)
(3) 将其取名为cgicc.cgi,并且放置在apache2服务器的:/usr/lib/cgi-bin/目录下边。
(4) 测试,在浏览器的地址栏中输入:http://localhost/cgi.html,选择上传的文件,点击”确定”,查看返回信息
(5) 测试结果发现:内容中的字符可以提取,提取的字符也可以转换成相应的类型,但是如果是图片的内容,就会出错,具体错误信息,如图2所示:
图1 cgi.html显示结果
#include <new> #include <string> #include <vector> #include <stdexcept> #include <iostream> #include <cstdlib> #include <string.h> #include "cgicc/CgiDefs.h" #include "cgicc/Cgicc.h" #include "cgicc/HTTPHTMLHeader.h" #include "cgicc/HTMLClasses.h" #include "styles.h" using namespace std; using namespace cgicc; // Print the form for this CGI // Main Street, USA int main(int /*Arc*/,char ** /*argv*/) { Cgicc cgi; // Output the HTTP headers for an HTML document, and the HTML 4.0 DTD info cout << HTTPHTMLHeader() << HTMLDoctype(HTMLDoctype::eStrict) << endl; cout << html().set("lang", "en").set("dir", "ltr") << endl; // Set up the page's header and title. cout << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />" << endl; cout << head() << endl; cout << style() << comment() << endl; cout << styles; cout << comment() << style() << endl; cout << title() << "GNU cgicc v" << cgi.getVersion() << " File Upload Test" << title() << endl; cout << head() << endl; cout << body() << endl; const_file_iterator file; file = cgi.getFile("userfile"); if(file != cgi.getFiles().end()) { cout << "<form method=\"post\" action=\"/cgi-bin/getfile.cgi" << cgi.getEnvironment().getScriptName() << "\" enctype=\"multipart/form-data\">" << endl; cout << table() << endl; cout << tr() << td("Filename").set("class","title") << td(file->getFilename()).set("class","data") << tr() << endl; cout << tr() << td("Data Length").set("class","title") << td().set("class","data") << "<input type=\"text\" name=\"length\" " << " value=\"" << file->getDataLength()<< "\">" << td() << tr() << endl; cout << tr() << td("File Data").set("class","title") << td().set("class","data") << pre() //file->writeToStream(cout); << "<textarea type=\"textarea\" name=\"data\" rows=\"30\" cols=\"800\" id=\"data\"" << " value="; file->writeToStream(cout); cout << "\">" << "</textarea>" << endl; cout << td() << tr() << endl; cout << pre() << td() << tr() << endl; cout << table() << endl; cout << "<div class=\"center\"><p>" << "<input type=\"submit\" name=\"submit\" value=\"Send\" />" << "</p></div></form>" << endl; } cout << "get the jpg's stream and its length into the form,the send it to the server hehe" <<endl; cout << body() << html() << endl; //已经确定file->getData.c_str()是上传图片的内容,将其生成本地txt,查看其二进制流,与原图片的二进制流对比,一样。 //问题:写入.jpg就变为空;写入txt,就是图片的内容…… ofstream output; output.open("/var/www/data/test.jpg"); output.write(file->getData().c_str(),file->getDataLength()); output.close(); cout << "close the file" << endl; return EXIT_SUCCESS; //} }
图2 返回信息
此时,查看/var/www/data/目录,其下边生成了test.jpg临时图片。