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;
@Override
protected 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();
}
@Override
public 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) {
// 没有权限
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)){
//如果没勾选“不再询问”,向用户发起权限请求
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0);
}else{
Toast.makeText(this,"请前往设置——>存储卡权限——>允许",Toast.LENGTH_SHORT).show();
}
} else {
// 有权限,接着你要干的活
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, "识别图片内容失败", 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;
}
/**
* 开始识别文字
*/
private void startReadText() {
File f = new File(path+"/tessdata") ;
if(!f.exists()){
Toast.makeText(this,"请先下载好语言包置于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();//初始化需要耗时,可以启动时程序时,预初始化
mBaseAPI.init(path + File.separator, "chi_sim");
Bitmap bitmap = getBitmap(resId);
if (bitmap == null) {
mHandler.sendEmptyMessage(1);
} else {
mBaseAPI.setImage(bitmap);
//根据Init的语言,获得ocr后的字符串
String t = mBaseAPI.getUTF8Text();//耗时操作
Message obtain = Message.obtain();
obtain.what = 0;
obtain.obj = t;
mHandler.sendMessage(obtain);
}
}
}.start();
}
}
<LinearLayout
xmlns: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="开始识别"/>
<TextView
android:text="识别结果展示区:"
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>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有