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

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