Ocr 图文识别技术——基于百度云OCR技术学习与总结

                                                           Ocr 图文识别技术——基于百度云OCR技术学习与总结

 

1.百度智能云 控制台  https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/overview/index  创建应用,生成API Key,Secret Key

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第1张图片

2.在应用详情页面下载对应应用的授权文件

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第2张图片

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第3张图片

3.百度智能云   Android-SDK文档链接:https://cloud.baidu.com/doc/OCR/s/fjwvxzeiy

4.SDK下载地址:http://ai.baidu.com/sdk/#ocr

  Ocr 图文识别技术——基于百度云OCR技术学习与总结_第4张图片

下面我跟大家讲一下开发步骤。

1.下载sdk

2.依赖sdk以及armeabi到lib目录下

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第5张图片

3.下载的sdk里面有一个ocr-ui model,如果需要的话,可以作为model到项目中

4.清单文件添加权限:




5.Proguard配置:app——proguard-rules.pro

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第6张图片

-keep class com.baidu.ocr.sdk.**{*;}
-dontwarn com.baidu.ocr.**

6.代码中:初始化OCR单例:

Ocr 图文识别技术——基于百度云OCR技术学习与总结_第7张图片

OCR.getInstance().initAccessTokenWithAkSk(new OnResultListener() {
    @Override
    public void onResult(AccessToken result) {
        // 调用成功,返回AccessToken对象
        String token = result.getAccessToken();
    }
    @Override
    public void onError(OCRError error) {
        // 调用失败,返回OCRError子类SDKError对象
    }
}, getApplicationContext(), "您的应用AK", "您的应用SK");

 

7.功能调用以及回调事件:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.tv_car://车牌识别
            Intent intent = new Intent(MainActivity.this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intent, REQUEST_CODE_LICENSE_PLATE);
            break;
        case R.id.tv_company://营业执照识别
            Intent intentCompany = new Intent(MainActivity.this, CameraActivity.class);
            intentCompany.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentCompany.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentCompany, REQUEST_CODE_BUSINESS_LICENSE);
            break;
        case R.id.general_basic://通用文字识别
            Intent intentGener = new Intent(MainActivity.this, CameraActivity.class);
            intentGener.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentGener.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentGener, REQUEST_CODE_GENERAL_BASIC);
            break;
        case R.id.accurate_basic://通用文字识别(高精度版)
            Intent intentAccur = new Intent(MainActivity.this, CameraActivity.class);
            intentAccur.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentAccur.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentAccur, REQUEST_CODE_GENERAL_ENHANCED);
            break;
        case R.id.general_location://通用文字识别(含位置信息版)
            Intent intentGenLoca = new Intent(MainActivity.this, CameraActivity.class);
            intentGenLoca.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentGenLoca.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentGenLoca, REQUEST_CODE_GENERAL);
            break;
        case R.id.accurate_location://通用文字识别(高精度含位置信息版)
            Intent intentAccuLoca = new Intent(MainActivity.this, CameraActivity.class);
            intentAccuLoca.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentAccuLoca.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentAccuLoca, REQUEST_CODE_ACCURATE);
            break;
        case R.id.general_enhance://通用文字识别(含生僻字版)
            Intent intentEnhance = new Intent(MainActivity.this, CameraActivity.class);
            intentEnhance.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentEnhance.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentEnhance, REQUEST_CODE_GENERAL_ENHANCED);
            break;
        case R.id.general_webimage://网络图片文字识别
            Intent intentWeb = new Intent(MainActivity.this, CameraActivity.class);
            intentWeb.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentWeb.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentWeb, REQUEST_CODE_GENERAL_WEBIMAGE);
            break;
        case R.id.idcard://身份证识别
            Intent intentIdCard = new Intent(MainActivity.this, IDCardActivity.class);
            startActivity(intentIdCard);
            break;
        case R.id.bankcard://银行卡识别
            Intent intentBank = new Intent(MainActivity.this, CameraActivity.class);
            intentBank.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentBank.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_BANK_CARD);
            startActivityForResult(intentBank, REQUEST_CODE_BANKCARD);
            break;
        case R.id.driving_license://驾驶证识别
            Intent intentDirver = new Intent(MainActivity.this, CameraActivity.class);
            intentDirver.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentDirver.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentDirver, REQUEST_CODE_VEHICLE_LICENSE);
            break;
        case R.id.vehicle_license://行驶证识别
            Intent intentVehicle = new Intent(MainActivity.this, CameraActivity.class);
            intentVehicle.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentVehicle.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentVehicle, REQUEST_CODE_DRIVING_LICENSE);
            break;
        case R.id.receipt://通用票据识别
            Intent intentReceipt = new Intent(MainActivity.this, CameraActivity.class);
            intentReceipt.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentReceipt.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentReceipt, REQUEST_CODE_RECEIPT);
            break;
        case R.id.passport://护照识别
            Intent intentPassport = new Intent(MainActivity.this, CameraActivity.class);
            intentPassport.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentPassport.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_PASSPORT);
            startActivityForResult(intentPassport, REQUEST_CODE_PASSPORT);
            break;
        case R.id.numbers://数字识别
            Intent intentNum = new Intent(MainActivity.this, CameraActivity.class);
            intentNum.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentNum.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentNum, REQUEST_CODE_NUMBERS);
            break;
        case R.id.qrcode://二维码识别
            Intent intentCode = new Intent(MainActivity.this, CameraActivity.class);
            intentCode.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentCode.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentCode, REQUEST_CODE_QRCODE);
            break;
        case R.id.business_card://名片识别
            Intent intentBusiness = new Intent(MainActivity.this, CameraActivity.class);
            intentBusiness.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentBusiness.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentBusiness, REQUEST_CODE_BUSINESSCARD);
            break;
        case R.id.handwritting://手写识别
            Intent intentHandwrite = new Intent(MainActivity.this, CameraActivity.class);
            intentHandwrite.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentHandwrite.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentHandwrite, REQUEST_CODE_HANDWRITING);
            break;
        case R.id.lottery://彩票识别
            Intent intentLottery = new Intent(MainActivity.this, CameraActivity.class);
            intentLottery.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentLottery.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentLottery, REQUEST_CODE_LOTTERY);
            break;
        case R.id.vat_invoice://增值税发票识别
            Intent intentVat = new Intent(MainActivity.this, CameraActivity.class);
            intentVat.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentVat.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentVat, REQUEST_CODE_VATINVOICE);
            break;
        case R.id.custom://自定义模板识别
            Intent intentCustom = new Intent(MainActivity.this, CameraActivity.class);
            intentCustom.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    FileUtils.getSaveFile(getApplication()).getAbsolutePath());
            intentCustom.putExtra(CameraActivity.KEY_CONTENT_TYPE,
                    CameraActivity.CONTENT_TYPE_GENERAL);
            startActivityForResult(intentCustom, REQUEST_CODE_CUSTOM);
            break;
    }
}

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

    // 识别成功回调,通用文字识别(含位置信息)
    if (requestCode == REQUEST_CODE_GENERAL && resultCode == Activity.RESULT_OK) {
        RecognizeService.recGeneral(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,通用文字识别(含位置信息高精度版)
    if (requestCode == REQUEST_CODE_ACCURATE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recAccurate(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,通用文字识别
    if (requestCode == REQUEST_CODE_GENERAL_BASIC && resultCode == Activity.RESULT_OK) {
        RecognizeService.recGeneralBasic(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,通用文字识别(高精度版)
    if (requestCode == REQUEST_CODE_ACCURATE_BASIC && resultCode == Activity.RESULT_OK) {
        RecognizeService.recAccurateBasic(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,通用文字识别(含生僻字版)
    if (requestCode == REQUEST_CODE_GENERAL_ENHANCED && resultCode == Activity.RESULT_OK) {
        RecognizeService.recGeneralEnhanced(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,网络图片文字识别
    if (requestCode == REQUEST_CODE_GENERAL_WEBIMAGE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recWebimage(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,银行卡识别
    if (requestCode == REQUEST_CODE_BANKCARD && resultCode == Activity.RESULT_OK) {
        RecognizeService.recBankCard(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,行驶证识别
    if (requestCode == REQUEST_CODE_VEHICLE_LICENSE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recVehicleLicense(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,驾驶证识别
    if (requestCode == REQUEST_CODE_DRIVING_LICENSE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recDrivingLicense(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,车牌识别
    if (requestCode == REQUEST_CODE_LICENSE_PLATE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recLicensePlate(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,营业执照识别
    if (requestCode == REQUEST_CODE_BUSINESS_LICENSE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recBusinessLicense(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,通用票据识别
    if (requestCode == REQUEST_CODE_RECEIPT && resultCode == Activity.RESULT_OK) {
        RecognizeService.recReceipt(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,护照
    if (requestCode == REQUEST_CODE_PASSPORT && resultCode == Activity.RESULT_OK) {
        RecognizeService.recPassport(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,二维码
    if (requestCode == REQUEST_CODE_QRCODE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recQrcode(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,彩票
    if (requestCode == REQUEST_CODE_LOTTERY && resultCode == Activity.RESULT_OK) {
        RecognizeService.recLottery(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,增值税发票
    if (requestCode == REQUEST_CODE_VATINVOICE && resultCode == Activity.RESULT_OK) {
        RecognizeService.recVatInvoice(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,数字
    if (requestCode == REQUEST_CODE_NUMBERS && resultCode == Activity.RESULT_OK) {
        RecognizeService.recNumbers(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,手写
    if (requestCode == REQUEST_CODE_HANDWRITING && resultCode == Activity.RESULT_OK) {
        RecognizeService.recHandwriting(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,名片
    if (requestCode == REQUEST_CODE_BUSINESSCARD && resultCode == Activity.RESULT_OK) {
        RecognizeService.recBusinessCard(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }

    // 识别成功回调,自定义模板
    if (requestCode == REQUEST_CODE_CUSTOM && resultCode == Activity.RESULT_OK) {
        RecognizeService.recCustom(this, FileUtils.getSaveFile(getApplicationContext()).getAbsolutePath(),
                new RecognizeService.ServiceListener() {
                    @Override
                    public void onResult(String result) {
                        alertText(result);
                    }
                });
    }
}

8.释放内存资源

@Override
protected void onDestroy() {
    super.onDestroy();
    // 释放内存资源
    OCR.getInstance(this).release();
}

9.demo链接:https://download.csdn.net/download/qq_36692282/11709460

10百度云这次不是全部免费的,只有200次一天的免费,之后要收费的,我 还以为 良心了额,开源了,小失落啊

 

你可能感兴趣的:(ocr,android,OCR,图文识别,百度云)