javaocr技术原理_最全的OCR图像识别技术源码内有说明(附
完整源码)
【实例简介】
tessdata是语⾔包 需要放到⼿机SD卡根⽬录android radiogroup
tess-two是OCR引擎,需要导⼊到项⽬中(项⽬中已经导⼊)
【实例截图】
【核⼼代码】
package l.tesseract;
import java.io.File;
import java.io.FileNotFoundException;
sseract.android.TessBaseAPI;
import android.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.app.Activity;
t.Intent;
aphics.Bitmap;
aphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int PHOTO_CAPTURE = 0x11;// 拍照
private static final int PHOTO_RESULT = 0x12;// 结果
private static String LANGUAGE = "eng";
private static String IMG_PATH = getSDPath() java.io.File.separator "ocrtest";
private static TextView tvResult;
private static ImageView ivSelected;
private static ImageView ivTreated;
private static Button btnCamera;
private static Button btnSelect;
private static CheckBox chPreTreat;
private static RadioGroup radioGroup;
private static String textResult;
private static Bitmap bitmapSelected;
private static Bitmap bitmapTreated;
private static final int SHOWRESULT = 0x101;
private static final int SHOWTREATEDIMG = 0x102;
// 该handler⽤于处理修改结果的任务
public static Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOWRESULT:
if (textResult.equals(""))
tvResult.setText("识别失败");
else
tvResult.setText(textResult);
break;
case SHOWTREATEDIMG:
tvResult.setText("识别中......");
showPicture(ivTreated, bitmapTreated);
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
// 若⽂件夹不存在 ⾸先创建⽂件夹
File path = new File(IMG_PATH);
if (!ists()) {
path.mkdirs();
}
tvResult = (TextView) findViewById(R.id.tv_result);
ivSelected = (ImageView) findViewById(R.id.iv_selected);
ivTreated = (ImageView) findViewById(R.id.iv_treated);
btnCamera = (Button) findViewById(R.id.btn_camera);
btnSelect = (Button) findViewById(R.id.btn_select);
chPreTreat = (CheckBox) findViewById(R.id.ch_pretreat);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
btnCamera.setOnClickListener(new cameraButtonListener());
btnSelect.setOnClickListener(new selectButtonListener());
// ⽤于设置解析语⾔
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.rb_en:
LANGUAGE = "eng";
break;
case R.id.rb_ch:
LANGUAGE = "chi_sim";
break;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().u.main, menu);
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_CANCELED)
return;
if (requestCode == PHOTO_CAPTURE) {
tvResult.setText("abc");
startPhotoCrop(Uri.fromFile(new File(IMG_PATH, "temp.jpg")));
}
// 处理结果
if (requestCode == PHOTO_RESULT) {
bitmapSelected = decodeUriAsBitmap(Uri.fromFile(new File(IMG_PATH, "temp_cropped.jpg")));
if (chPreTreat.isChecked())
tvResult.setText("预处理中......");
else
tvResult.setText("识别中......");
// 显⽰选择的图⽚
showPicture(ivSelected, bitmapSelected);
// 新线程来处理识别
new Thread(new Runnable() {
@Override
public void run() {
if (chPreTreat.isChecked()) {
bitmapTreated = ImgPretreatment
.doPretreatment(bitmapSelected);
Message msg = new Message();
msg.what = SHOWTREATEDIMG;
myHandler.sendMessage(msg);
textResult = doOcr(bitmapTreated, LANGUAGE);
} else {
bitmapTreated = ImgPretreatment
.converyToGrayImg(bitmapSelected);
Message msg = new Message();
msg.what = SHOWTREATEDIMG;
myHandler.sendMessage(msg);
textResult = doOcr(bitmapTreated, LANGUAGE);
}
Message msg2 = new Message();
msg2.what = SHOWRESULT;
myHandler.sendMessage(msg2);
}
}).start();
}
}
// 拍照识别
class cameraButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(IMG_PATH, "temp.jpg"))); startActivityForResult(intent, PHOTO_CAPTURE);
}
};
// 从相册选取照⽚并裁剪
class selectButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra("crop", "true");

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。