OCR(圖片識別)之 百度 VS 谷歌

先上效果

在這裏插入圖片描述

結論

  • baidu方案
    優點:速度快,效率高,準確率高,需要聯網
    缺點:收費(測試接口是高精度通用文字識別:500次/天免費,還有很多其他的接口方式提供),需要註冊,申請KEY

  • google方案
    優點:免費,無需聯網,可修改性強
    缺點:速度慢,識別率低

相關連接

百度文字識別:https://cloud.baidu.com/product/ocr/general
谷歌:https://github.com/rmtheis/tess-two
文章代碼地址:https://download.csdn.net/download/qq471208499/11986805

項目運行環境

WIN 10
JDK 1.8
android stutio 3.5.1

項目目錄結構

在這裏插入圖片描述

代碼目錄結構

在這裏插入圖片描述

核心代碼

  • 百度

初始化

	public static void initAccessToken(Context context) {
        OCR ocr = OCR.getInstance(context);
        ocr.initAccessToken(new OnResultListener<AccessToken>() {
            @Override
            public void onResult(AccessToken accessToken) {
                token = accessToken.getAccessToken();
                Log.i("BaiduOCR", token);
            }

            @Override
            public void onError(OCRError ocrError) {
                Log.d("initAccessToken", ocrError.toString());
            }
        }, context.getApplicationContext());
    }

解析圖片

	public static void decodeImg(Context context, final Handler handler, final View mView) {
        File file = new File(copyAss2Ex(mView, context));
        final long startTime = System.currentTimeMillis();
        GeneralBasicParams params = new GeneralBasicParams();
        params.setDetectDirection(true);
        params.setImageFile(file);
        OCR ocr = OCR.getInstance(context);
        ocr.recognizeAccurateBasic(params, new OnResultListener<GeneralResult>() {
            @Override
            public void onResult(GeneralResult generalResult) {
                Log.d("BaiduOCR.decodeImg", generalResult.getJsonRes());
                StringBuilder sb = new StringBuilder();
                for (WordSimple simple: generalResult.getWordList()) {
                    sb.append(simple.getWords());
                    sb.append("\n");
                }
                RecyclerBean bean = getBean(startTime, mView, sb.toString());
                Message message = handler.obtainMessage();
                message.obj = bean;
                handler.sendMessage(message);
            }

            @Override
            public void onError(OCRError ocrError) {
                handler.sendEmptyMessage(-1);
            }
        });
    }

    private static RecyclerBean getBean(long startTime, View mView, String resultStr) {
        RecyclerBean bean = new RecyclerBean();
        bean.setCompany(MainActivity.COMPANY_BAIDU);
        bean.setStartTime(startTime);
        bean.setWitchImg(mView.getId() == R.id.main_img1 ? MainActivity.IMG_1 : MainActivity.IMG_2);
        bean.setEndTime(System.currentTimeMillis());
        bean.setResult(resultStr);
        return bean;
    }

    private static String copyAss2Ex(View view, Context context) {
        String assetName = view.getId() == R.id.main_img1 ? "scan1.png" : "scan2.png";
        return copyAss2Ex(assetName, "pic", context);
    }


    private static String copyAss2Ex(String assetName, String exPath, Context context) {
        File rootPath = context.getExternalFilesDir(null);
        String datapath = rootPath + "/" + exPath + "/" + assetName;
        File file = new File(datapath);
        if (file.exists()) {
            return datapath;
        } else {
            file.getParentFile().mkdirs();
        }
        try (InputStream inputStream = context.getResources().getAssets().open(assetName);
             FileOutputStream outputStream = new FileOutputStream(datapath);) {
            byte[] data = new byte[1024];
            //輸出流
            //開始處理流
            while (inputStream.read(data) != -1) {
                outputStream.write(data);
            }
            return datapath;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
  • 谷歌
	public void decode() {
        long startTime = System.currentTimeMillis();
        ImageView imageView = (ImageView) mView;
        Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
        //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.scan1);
        TessBaseAPI tess = getTess();
        //tess.setVariable("classify_bln_numeric_mode", "1");
        tess.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "0123456789");
        tess.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=-[]}{;:'\"\\|~`,./<>?“”‘’\"");
        tess.setImage(bitmap);
        String resultStr = tess.getUTF8Text();
        tess.end();
        System.gc();
        sendMessage(resultStr, startTime);
    }

    private void sendMessage(String resultStr, long startTime) {
        RecyclerBean bean = new RecyclerBean();
        bean.setCompany(mCompany);
        bean.setStartTime(startTime);
        bean.setWitchImg(mView.getId() == R.id.main_img1 ? MainActivity.IMG_1 : MainActivity.IMG_2);
        bean.setEndTime(System.currentTimeMillis());
        bean.setResult(resultStr);

        Message message = mHandler.obtainMessage();
        message.obj = bean;
        mHandler.sendMessage(message);
    }

其他

loading 加載UI :https://blog.csdn.net/qq471208499/article/details/102729125

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章