doomvsjing 2020-06-11
目前Android这块没有理想的免费OCR开源框架,项目需要,我们选择了百度的OCR 产品;
这里以AS开发环境,身份证信息识别功能为例,阐述集成使用ORC的过程;
2.有兴趣的可以运行下Demo,我这里直接集成其中ocr_ui文件(依赖该Moudle,相关so库也在改Moudle中)
3.(必须)将下载包libs目录中的ocr-sdk.jar文件拷贝到工程libs目录中,并加入工程依赖。
4.(必须)将libs目录下armeabi,arm64-v8a,armeabi-v7a,x86文件夹按需添加到android studio工程src/main/jniLibs
5.确保已经在工程AndroidManifest.xml文件中添加如下权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Xm 代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_ocr" android:layout_width="match_parent" android:layout_height="match_parent" 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="cn.example.multiplechoicelistview.OCRActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/iv_identityScan" android:layout_width="60dp" android:layout_height="60dp" android:src="@mipmap/shaomiao" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="11dp" android:paddingBottom="24.5dp" android:text="扫描身份证身份识别信息" android:textColor="#333333" android:textSize="14sp" /> </LinearLayout> </RelativeLayout>
逻辑代码(可直接复制使用)
public class OCRActivity extends AppCompatActivity { private ProgressDialog dialog; // 标识 private static boolean hasGotToken = false; private static final int REQUEST_CODE_CAMERA = 102; private static final String APIKey = "官网上传包名的APIKey"; private static final String SecretKey = "官网上传包名的SecretKey"; private ImageView iv; public static void start(Activity activity) { Intent intent = new Intent(activity, OCRActivity.class); activity.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ocr); iv = (ImageView) findViewById(R.id.iv_identityScan); initOCR(); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (hasGotToken) { applyCard(); }else { Log.d("tag", "百度OCR token还未成功获取"); initOCR(); } } }); } public static File getSaveFile(Context context) { File file = new File(context.getFilesDir(), "pic.jpg"); return file; } private void applyCard() { Intent intent = new Intent(this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_NATIVE_TOKEN, OCR.getInstance().getLicense()); intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT); startActivityForResult