package libaier.ocr;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.http.util.EncodingUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.R.color;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class OCR_TestActivity extends Activity {
// 获得用于动态添加TextView的Layout
LinearLayout words_layout = null;
// 用于添加的TextView
TextView words_view[] = null;
// 网络取词用到
private String meaning = null;
// 网络取词相关
private String url = "http://dict.cn/ws.php";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
readWords();
}
public void readWords() {
words_layout = (LinearLayout) findViewById(R.id.words);
// 因为只是示例,打开文件方面做的很粗糙,直接现在SD卡根目录下放了一个名为Text.txt的文件,内容为“Libaier is
// Tuesday”
String str = readFileSdcard("mnt/sdcard/Test.txt");
// 根据空格把这个句子分开,得到每一个单词,以后详细做的时候要考虑标点等多种情况,示例就简
String[] words_str = str.split(" ");
words_view = new TextView[words_str.length];
for (int i = 0; i < words_str.length; i++) {
// 将每个TextView即每个单词依次添加到layout上,并为每个TextView添加翻译监听,这里并没实现真正“划”词的效果,要实现只需修改translate()函数即可,关于划词的具体实现会在讨论第二种OCR方法时具体讨论
words_view[i] = new TextView(this);
words_view[i].setText(words_str[i]);
final String word_totranslate = (String) words_view[i].getText();
words_view[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
translate(word_totranslate);
}
});
words_layout.addView(words_view[i]);
// 在每个单词后添加一个空格只为美观
words_view[i] = new TextView(this);
words_view[i].setText(" ");
words_layout.addView(words_view[i]);
}
}
// 读取SD卡文件函数
public String readFileSdcard(String fileName) {
String res = "";
try {
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch (Exception e) {
e.printStackTrace();
}
return res;
}
// 翻译函数
private void translate(String str) {
String explain = getWord(url + "?q=" + str);
if (explain.equals("Not Found")) {
Toast.makeText(getApplicationContext(), "查无此词", Toast.LENGTH_SHORT)
.show();
} else {
Toast
.makeText(getApplicationContext(), explain,
Toast.LENGTH_SHORT).show();
}
}
// 从网络取词
private String getWord(String urlword) {
String wordResult = "";
URL url = null;
try {
url = new URL(urlword);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("User-Agent", "Mozilla/4.0"
+ "(compatible; MSIE 6.0; Windows 2000)");
conn.setRequestProperty("Content-type", "text/xml; charset=UTF-8");
conn.connect();
InputStream XMLBody = conn.getInputStream();// 解析返回的xml
wordResult = parseXML(XMLBody);
}
catch (Exception ex) {
Log.i("error", ex + "");
// showDialog("" + ex);
}
return wordResult;
}
// 解析xml
private String parseXML(InputStream is) {
String result = "";
String mean = "";
String example = "";
String e11 = "";
String e21 = "";
// 解析xml,采用Dom解析
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Log.i("--", "----------11-");
doc = db.parse(is);
Log.i("--", "-----------");
doc.getDocumentElement().normalize();
Log.i("getWord", "====================");
// 首先解析出单词的意思
NodeList nmean = doc.getElementsByTagName("def");
if (nmean.getLength() == 0) {// 如果没有def标签,就认为是找不到该单词
meaning = "Not Found";
}
else {
for (int i = 0; i < nmean.getLength(); i++) {
mean = nmean.item(0).getFirstChild().getNodeValue();
}
meaning = mean;
}
if (meaning.equals("Not Found")) {
result = meaning;
}
// 解析出例句
else {
NodeList nexample = doc.getElementsByTagName("sent");
for (int i = 0; i < nexample.getLength(); i++) {
example += "\n";
Element el = (Element) nexample.item(i);
Node orig = el.getElementsByTagName("orig").item(0);
e11 = orig.getFirstChild().getNodeValue();
e11 = e11.substring(0, e11.lastIndexOf("<em>"))
+ " "
+ e11.substring(e11.lastIndexOf("<em>") + 4, e11
.lastIndexOf("</em>"))
+ e11.substring(e11.lastIndexOf("</em>") + 5, e11
.length());
example += e11;
Node trans = el.getElementsByTagName("trans").item(0);
e21 = trans.getFirstChild().getNodeValue();
example += e21;
}
// 然后解析出相应的例句
result = mean;
}
}
catch (Exception ex) {
// showDialog(ex + "hello");
}
return result;
}
}
- 1
- 2
前往页