tesseract android ocr 识别 例子

  • G3_615677
    了解作者
  • 9.2MB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-17 11:59
    上传日期
ocr的源码 不用你在linix上面进行二次编译 不过需要你去下载语言包
tesseract-android-tools-test.zip
内容介绍
package com.wy.ocrtest; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.concurrent.TimeUnit; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Matrix; import android.media.ExifInterface; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; import com.googlecode.tesseract.android.TessBaseAPI; import com.wy.util.GreyDealOld; public class FileOcrActivity extends Activity { private ImageView mImageView; private EditText editText; private Button ocrbtn,btnPhoto; private ImageButton openfilebtn; private ImageButton selectOcrLangbtn; private ProgressDialog mProgressDialog; private LanguageDialog mSelectOcrLangDialog; private String fileurl="/mnt/sdcard/ocr/img.jpg"; private Context mContext; private Bitmap mbitmap; private String ocrLang = "eng"; private String[] language = {"zh","en","it","es","fr","de"}; private String[] ocrLangs = {"chi_sim","eng","ita","spa","fra","deu-frak"}; private static final int FAILURE = 12001; private static final int SUCCESS = 12002; private static final String TESSBASE_PATH = "/mnt/sdcard/ocr/"; private String DEFAULT_LANGUAGE = "eng"; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch(msg.what){ case 0: //识别成功 String ocrResult = msg.getData().getString("text"); editText.setText(ocrResult); break; case 1: Toast.makeText(mContext, ocrLang +"不支持", Toast.LENGTH_SHORT).show(); break; case 3: // 选择识别语言 int langint = msg.getData().getInt("position"); ocrLang = ocrLangs[langint]; int id = mContext.getResources().getIdentifier(language[langint], "drawable", mContext.getResources().getString(R.string.packagename)); selectOcrLangbtn.setImageResource(id); selectOcrLangbtn.invalidate(); mSelectOcrLangDialog.dismiss(); DEFAULT_LANGUAGE=ocrLangs[langint]; break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mContext = this; this.setContentView(R.layout.fileocr); mImageView = (ImageView) this.findViewById(R.id.file_image); mbitmap = BitmapFactory.decodeFile(fileurl); if (mbitmap != null) { mImageView.setImageBitmap(mbitmap); } else{ mImageView.setImageResource(R.drawable.testimg); } //识别 editText = (EditText) this.findViewById(R.id.file_text); ocrbtn = (Button) this.findViewById(R.id.ocrbutton); ocrbtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Log.e("aaaaaaaaa","11111"+fileurl); new OcrLoader(mContext, mHandler).execute(fileurl); } }); //查找图片 openfilebtn = (ImageButton) this.findViewById(R.id.openfilebt); openfilebtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(FileOcrActivity.this, OpenFileActivity.class); startActivityForResult(intent,5); //返回图片 } }); //选择识别语言 selectOcrLangbtn = (ImageButton) this.findViewById(R.id.selectocrlangbtn); selectOcrLangbtn.setImageResource(R.drawable.en); selectOcrLangbtn.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { mSelectOcrLangDialog = new LanguageDialog(mContext, mHandler, true); mSelectOcrLangDialog.setTitle(R.string.ocrlang); mSelectOcrLangDialog.show(); } }); //拍照返回图片 btnPhoto = (Button) this.findViewById(R.id.photobutton); btnPhoto.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(FileOcrActivity.this, CameraActivity.class); startActivityForResult(intent,5); //拍照返回图片 } }); } //返回图片路径 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if ( resultCode==5 ) { fileurl=data.getStringExtra("file"); mbitmap = BitmapFactory.decodeFile(fileurl); if (mbitmap != null) { mImageView.setImageBitmap(mbitmap); mImageView.invalidate(); } } } private class OcrLoader extends AsyncTask<String, String, String> { private Context mContext; private Handler mHandler; public OcrLoader(Context mContext,Handler mHandler){ this.mContext=mContext; this.mHandler=mHandler; } @Override protected String doInBackground(String... params) { String m_sResult=null; String orcimg=params[0]; int second = 3; try { publishProgress("请等待,正在进行文本识别中..."); TessBaseAPI baseApi = new TessBaseAPI(); Log.e("TTTT",TESSBASE_PATH+"========="+DEFAULT_LANGUAGE); baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); baseApi.setPageSegMode(TessBaseAPI.PSM_SINGLE_LINE); //options是为了缩放图片,这个酌情缩放,图片小的话可以不缩放 BitmapFactory.Options options=new BitmapFactory.Options(); //缩小为原来的1/2 options.inSampleSize=1/5; //bitmap,我这里是以流的形式,只要能形成Bitmap就行 Bitmap bitmap=BitmapFactory.decodeFile(orcimg, options); publishProgress("开始锐化..." ); // bitmap=sharpenImageAmeliorate(bitmap); publishProgress("开始转为黑白化..." ); // bitmap=sharpenImageGary(bitmap); //如果图片有Alpha值,那么最好设置一下 ExifInterface exif = new ExifInterface(orcimg); int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); int rotate = 0; switch (exifOrientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotate = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotate = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotate = 270; break; } if (rotate != 0) { // Getting width & height of the given image. int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting pre rotate Matrix mtx = new Matrix(); mtx.preRotate(rotate); // Rotating Bitmap bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false); // tesseract req. ARGB_8888 bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); } //设置要ocr的图片bitmap baseApi.setImage(bitmap); //根据Init的语言,获得ocr后的字符串 m_sResult= baseApi.getUTF8Text(); // remove html codes // apostrophe m_sResult = m_sResult.replace("&#39;", "'"); m_sResult = m_sResult.replace("&apos;", "'"); // quotation mark m_sResult = m_sResu
评论
    相关推荐
    • Android学习
      Android学习Android学习Android学习Android学习Android学习Android学习
    • Android678
      Android678Android678Android678Android678Android678Android678Android678Android678Android678Android678
    • android 例子
      android 例子android 例子android 例子android 例子android 例子
    • android图片
      android精美小图片android精美小图片android精美小图片android精美小图片
    • android课件
      清华老师上课的android课件。ch_01android概述ch_02android开发基础ch_03资源的访问ch_04android用户界面ch_05Ativity和Intent。其他自己看。
    • AndroidKiller
      安卓App反编译工具AndroidKiller\AndroidKiller\AndroidKiller
    • AndroidSample
      Android示例 这是一个示例Android应用,用于降低Google Play服务和Android Volley的使用率。 有关更多详细信息,请参阅以下链接: Google Play服务: Android凌空抽射:
    • android source
      android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source android 源代码 source
    • Android 教程
      Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android 案例 教程,Android ...
    • android资料
      Android程序员指南 Android_UI开发专题 Android程序开发初级教程 android的五大类各种常用类的资料整理 Android高手进阶教程