1. 程式人生 > >百度api識別身份證 + 銀行卡 + 駕駛證 + 行駛證 資訊

百度api識別身份證 + 銀行卡 + 駕駛證 + 行駛證 資訊

銀行卡  呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_BANK_CARD); startActivityForResult(intent, REQUEST_CODE_CAMERA);

駕駛證 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class); intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath()); intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL); startActivityForResult(intent, REQUEST_CODE_DRIVING_LICENSE);

行駛證 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_GENERAL);
startActivityForResult(intent, REQUEST_CODE_VEHICLE_LICENSE);

身份證正面(手動識別) 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
startActivityForResult(intent, REQUEST_CODE_CAMERA);

身份證反面(手動識別) 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
startActivityForResult(intent, REQUEST_CODE_CAMERA);

身份證正面(自動識別) 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true);
intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true);
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
startActivityForResult(intent, REQUEST_CODE_CAMERA);

身份證反面(自動識別) 呼叫:

intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH, FileUtil.getSaveFile(getApplication()).getAbsolutePath());
intent.putExtra(CameraActivity.KEY_NATIVE_ENABLE, true);
intent.putExtra(CameraActivity.KEY_NATIVE_MANUAL, true);
intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
startActivityForResult(intent, REQUEST_CODE_CAMERA);

 主頁許可權判斷 :

主要事相機的許可權:

<uses-permission android:name="android.permission.CAMERA" />

關鍵程式碼:

   /**
     * 判斷相關許可權問題
     */
    public void checkPermission() {
//        int checkINSTALL = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.REQUEST_INSTALL_PACKAGES);
        int checkSelfPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkSelfPermission == PackageManager.PERMISSION_GRANTED) {     //允許  0,  詢問  -1
            //已經獲取到許可權  獲取使用者媒體資源
//            checkUpdate();
        } else {
            //沒有拿到許可權  是否需要在第二次請求許可權的情況下
            // 先自定義彈框說明 同意後在請求系統許可權(就是是否需要自定義DialogActivity)
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1514);
        }
    }

private void initAccessTokenWithAkSk() {     OCR.getInstance().initAccessTokenWithAkSk(             new OnResultListener<AccessToken>() {                 @Override                 public void onResult(AccessToken result) {                     // 本地自動識別需要初始化                     initLicense();

                    Log.d("MainActivity", "onResult: " + result.toString());                     runOnUiThread(new Runnable() {                         @Override                         public void run() {                             Toast.makeText(MainActivity.this, "初始化認證成功", Toast.LENGTH_SHORT).show();                         }                     });                 }

                @Override                 public void onError(OCRError error) {                     error.printStackTrace();                     Log.e("MainActivity", "onError: " + error.getMessage());                     runOnUiThread(new Runnable() {                         @Override                         public void run() {                             Toast.makeText(MainActivity.this, "初始化認證失敗,請檢查 key", Toast.LENGTH_SHORT).show();                         }                     });                 }             }, getApplicationContext(),             // 需要自己配置 https://console.bce.baidu.com             "**************************************",//填寫自己的             // 需要自己配置 https://console.bce.baidu.com             "***************************************");//填寫自己的}

private void initLicense() {
    CameraNativeHelper.init(this, OCR.getInstance().getLicense(),
            new CameraNativeHelper.CameraNativeInitCallback() {
                @Override
                public void onError(int errorCode, Throwable e) {
                    final String msg;
                    switch (errorCode) {
                        case CameraView.NATIVE_SOLOAD_FAIL:
                            msg = "載入so失敗,請確保apk中存在ui部分的so";
                            break;
                        case CameraView.NATIVE_AUTH_FAIL:
                            msg = "授權本地質量控制token獲取失敗";
                            break;
                        case CameraView.NATIVE_INIT_FAIL:
                            msg = "本地質量控制";
                            break;
                        default:
                            msg = String.valueOf(errorCode);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this,
                                    "本地質量控制初始化錯誤,錯誤原因: " + msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE_CAMERA && resultCode == Activity.RESULT_OK) {
        if (data != null) {
            String contentType = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);
            String filePath = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
            if (!TextUtils.isEmpty(contentType)) {
                if (CameraActivity.CONTENT_TYPE_ID_CARD_FRONT.equals(contentType)) {
                    recIDCard(IDCardParams.ID_CARD_SIDE_FRONT, filePath);
                } else if (CameraActivity.CONTENT_TYPE_ID_CARD_BACK.equals(contentType)) {
                    recIDCard(IDCardParams.ID_CARD_SIDE_BACK, filePath);
                } else if (CameraActivity.CONTENT_TYPE_BANK_CARD.equals(contentType)) {
                    recCreditCard(filePath);
                }
            }
        }
    }
    if (requestCode == REQUEST_CODE_DRIVING_LICENSE && resultCode == Activity.RESULT_OK) {
        String filePath = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
        recDrivingCard(filePath);
    }
    if (requestCode == REQUEST_CODE_VEHICLE_LICENSE && resultCode == Activity.RESULT_OK) {
        String filePath = FileUtil.getSaveFile(getApplicationContext()).getAbsolutePath();
        recVehicleCard(filePath);
    }
}

可以檢視   應用詳情      檢視文件    下載SDK

下載地址: