国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android中使用orc實(shí)現(xiàn)文字識(shí)別實(shí)例

2019-10-23 18:27:36
字體:
供稿:網(wǎng)友

一、什么是orc?

引用百度百科的介紹,指利用光學(xué)字符識(shí)別(ORC全稱:Optical Character Recognition)技術(shù),將圖片、照片上的文字內(nèi)容,直接轉(zhuǎn)換為可編輯文本,支持JPG、PNG、GIF、BMP、DOC等圖片格式。簡(jiǎn)單一句話,就是可以把圖片上的文字識(shí)別出來。應(yīng)用的場(chǎng)景有很多,比如說:身份證號(hào)碼識(shí)別,銀行卡號(hào)識(shí)別等等。

二、效果展示

這里筆者實(shí)現(xiàn)的僅僅是一個(gè)效果,實(shí)際使用可能需要對(duì)它進(jìn)行訓(xùn)練以提高識(shí)別率,第一次做gif圖片,效果不是很好

Android,orc文字識(shí)別,android文字識(shí)別技術(shù),文字識(shí)別

Android,orc文字識(shí)別,android文字識(shí)別技術(shù),文字識(shí)別

Android,orc文字識(shí)別,android文字識(shí)別技術(shù),文字識(shí)別

三、開始集成

Github上面已經(jīng)提供了android端的工具api,Github地址:https://github.com/rmtheis/tess-two

集成流2

1.下載中文簡(jiǎn)體語言包

2.導(dǎo)入依賴

3.API的使用,獲取TessBaseAPI mBaseAPI = new TessBaseAPI();實(shí)例

4.API的使用,初始化TessBaseAPI設(shè)置,設(shè)置識(shí)別的語言和語言包所在文件路徑 mBaseAPI.init(path + File.separator, "chi_sim");

5.API的使用,設(shè)置Bitmap,mBaseAPI.setImage(bitmap);

6.API的使用,從Bitmap獲取文字信息,mBaseAPI.getUTF8Text();

1.下載中文簡(jiǎn)體語言包

語言包下載地址

找到tessdata——>chi_sim.traineddata

下載好了之后,需要放到sd卡中,目錄不限,但是必須要放在tessdata目錄里面,如果沒有tessdata目錄需要手動(dòng)創(chuàng)建,例如我是Demo中是放在sd卡根目錄中,就直接在sd卡根目錄創(chuàng)建tessdata目錄,然后把下載好的chi_sim.traineddata語言包丟進(jìn)去,實(shí)際項(xiàng)目中,在識(shí)別時(shí)候最好坐下語言包是否復(fù)制到位的檢查,以免出現(xiàn)異常。Demo中僅僅是檢查了是否創(chuàng)建tessdata目錄,這里實(shí)際上仍然存在風(fēng)險(xiǎn)的。

2.導(dǎo)入依賴

Gradle方式添加:https://github.com/rmtheis/tess-two

3.MainActivity代碼

public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView mTvInfo;private TessBaseAPI mBaseAPI;private ProgressBar mProbar;private String path;private RadioGroup mRadioGroup;private RadioButton mRbtnIdCard;private RadioButton mRbtnBankNumber;private RadioButton mRbtnTxt;@Overrideprotected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  findViewById(R.id.btn_start).setOnClickListener(this);  mProbar = (ProgressBar) findViewById(R.id.pb);  mTvInfo = (TextView) findViewById(R.id.tv_info);  mRadioGroup = (RadioGroup) findViewById(R.id.rg);  mRbtnIdCard = (RadioButton) findViewById(R.id.rb_idCard);  mRbtnBankNumber = (RadioButton) findViewById(R.id.rb_bankNumber);  mRbtnTxt = (RadioButton) findViewById(R.id.rb_txt);  mRadioGroup.check(0);  path = Environment.getExternalStorageDirectory().getAbsoluteFile().getAbsolutePath();}@Overridepublic void onClick(View v) {  mTvInfo.setText("");  switch (v.getId()) {    case R.id.btn_start:      if (Build.VERSION.SDK_INT >= 23) {        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {          // 沒有權(quán)限          if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)){            //如果沒勾選“不再詢問”,向用戶發(fā)起權(quán)限請(qǐng)求            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0);          }else{            Toast.makeText(this,"請(qǐng)前往設(shè)置——>存儲(chǔ)卡權(quán)限——>允許",Toast.LENGTH_SHORT).show();          }        } else {          // 有權(quán)限,接著你要干的活          startReadText();        }      }else{        startReadText();      }      break;  }}private Handler mHandler = new Handler() {  @Override  public void handleMessage(Message msg) {    switch (msg.what) {      case 0:        String s = (String) msg.obj;        if (!TextUtils.isEmpty(s)) {          mProbar.setVisibility(View.GONE);          mTvInfo.setText(s);          //釋放bitmap          mBaseAPI.clear();        } else {          mProbar.setVisibility(View.GONE);          Toast.makeText(MainActivity.this, "識(shí)別圖片內(nèi)容失敗", Toast.LENGTH_SHORT).show();        }        break;      case 1:        Toast.makeText(MainActivity.this, "讀取圖片失敗", Toast.LENGTH_SHORT).show();        break;    }  }};private Bitmap getBitmap(int id) {  Bitmap bitmap = null;  try {    bitmap = BitmapFactory.decodeResource(getResources(), id);  } catch (Exception e) {    return null;  }  return bitmap;}/** * 開始識(shí)別文字 */private void startReadText() {  File f = new File(path+"/tessdata") ;  if(!f.exists()){    Toast.makeText(this,"請(qǐng)先下載好語言包置于sd/tessdata目錄",Toast.LENGTH_SHORT).show();    return;  }  final int btnId = mRadioGroup.getCheckedRadioButtonId();  final int resId ;  if(R.id.rb_idCard==btnId){    resId = R.drawable.idcard;  }else if(R.id.rb_bankNumber==btnId){    resId = R.drawable.bank_number;  }else{    resId = R.drawable.tet_info;  }  mProbar.setVisibility(View.VISIBLE);  new Thread() {    @Override    public void run() {      mBaseAPI = new TessBaseAPI();//初始化需要耗時(shí),可以啟動(dòng)時(shí)程序時(shí),預(yù)初始化      mBaseAPI.init(path + File.separator, "chi_sim");      Bitmap bitmap = getBitmap(resId);      if (bitmap == null) {        mHandler.sendEmptyMessage(1);      } else {        mBaseAPI.setImage(bitmap);        //根據(jù)Init的語言,獲得ocr后的字符串        String t = mBaseAPI.getUTF8Text();//耗時(shí)操作        Message obtain = Message.obtain();        obtain.what = 0;        obtain.obj = t;        mHandler.sendMessage(obtain);      }    }  }.start();}}

4.activity_main.xml代碼

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.demo.orc.MainActivity"><RadioGroup  android:id="@+id/rg"  android:orientation="horizontal"  android:layout_width="match_parent"  android:layout_height="wrap_content">  <RadioButton    android:checked="true"    android:id="@+id/rb_idCard"    android:text="身份證"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />  <RadioButton    android:id="@+id/rb_bankNumber"    android:text="銀行卡"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />  <RadioButton    android:id="@+id/rb_txt"    android:text="文字"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /></RadioGroup><Button  android:id="@+id/btn_start"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="開始識(shí)別"/><TextView  android:text="識(shí)別結(jié)果展示區(qū):"  android:layout_width="match_parent"  android:layout_height="wrap_content" /><FrameLayout  android:layout_width="match_parent"  android:layout_height="match_parent">  <ProgressBar    android:id="@+id/pb"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="center"    android:visibility="gone"/>  <TextView    android:id="@+id/tv_info"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:text=""/></FrameLayout></LinearLayout>

四、提高識(shí)別率

Demo識(shí)別率其實(shí)不是很理想,比如把數(shù)字0識(shí)別成了字母O等,這是因?yàn)槲覀兊母緵]有進(jìn)行樣本訓(xùn)練。關(guān)于樣本的訓(xùn)練,我目前還沒實(shí)際操作過,因?yàn)楣镜淖R(shí)別需求更為復(fù)雜,這個(gè)框架難以達(dá)到效果,公司買了第三方的一個(gè)識(shí)別框架。不過僅僅是實(shí)現(xiàn)身份證號(hào),銀行卡號(hào),和一些簡(jiǎn)單的文字信息,用這個(gè)框架足以實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。

 

注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高州市| 白河县| 宁津县| 济宁市| 三河市| 商南县| 栾川县| 北辰区| 荣成市| 南漳县| 上蔡县| 东海县| 安顺市| 荥经县| 舟山市| 子长县| 濮阳市| 民县| 通州区| 奉节县| 永宁县| 土默特左旗| 和硕县| 高碑店市| 镇宁| 元氏县| 澎湖县| 盐山县| 湟源县| 客服| 宾川县| 大悟县| 永吉县| 上杭县| 洮南市| 遵化市| 旌德县| 响水县| 东明县| 乌兰察布市| 济宁市|