Aoi-hosizora / Biji_Baibuti

SCUT Baibuti Project - Biji
Apache License 2.0
2 stars 4 forks source link

异步识别文字 鸵鸟算法待改 #13

Closed Aoi-hosizora closed 5 years ago

Aoi-hosizora commented 5 years ago
    /**
     * 异步识别文字
     * @param bitmap 图片路径
     */
    private void idenWordsSync(final Bitmap bitmap) {
        idenLoadingDialog = new ProgressDialog(ModifyNoteActivity.this);
        idenLoadingDialog.setTitle(R.string.MNoteActivity_OCRSyncAlertTitle);
        idenLoadingDialog.setMessage(getResources().getString(R.string.MNoteActivity_OCRSyncAlertMsg));

        class HasDismiss {
            private boolean dismiss = false;
            HasDismiss() {}
            void setDismiss() { this.dismiss = true; }
            boolean getDismiss() { return this.dismiss; }
        }
        final HasDismiss isHasDismiss = new HasDismiss();

        final Observable<String> mObservable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

                // 识别 ExtractUtil.recognition <<< 异步
                final String extaText = ExtractUtil.recognition(bitmap, ModifyNoteActivity.this);

                emitter.onNext(extaText); // 处理识别后响应
                emitter.onComplete(); // 完成
            }
        })
        .subscribeOn(Schedulers.io()) //生产事件在io
        .observeOn(AndroidSchedulers.mainThread()); //消费事件在UI线程

        idenLoadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) { ////////// 待改
                // Toast.makeText(ModifyNoteActivity.this, "d", Toast.LENGTH_SHORT).show();
                // ExtractUtil.tessBaseAPI.clear();
                // mObservable.onTerminateDetach();
                isHasDismiss.setDismiss();
            }
        });
        idenLoadingDialog.show();

        mObservable.subscribe(new Observer<String>() {
            @Override
            public void onComplete() {

            }

            @Override
            public void onError(Throwable e) {
                if (idenLoadingDialog != null && idenLoadingDialog.isShowing()) {
                    idenLoadingDialog.dismiss();
                }
                Toast.makeText(ModifyNoteActivity.this, R.string.MNoteActivity_OCRSyncAlertError, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSubscribe(Disposable d) {
                subsInsert = d;
            }

            // 识别后的处理
            @Override
            public void onNext(final String extaText) {
                // 关闭进度条
                if (idenLoadingDialog != null && idenLoadingDialog.isShowing()) {
                    idenLoadingDialog.dismiss();
                }
                // 处理回显
                ShowLogE("idenWordsSync.onNext()", isHasDismiss.getDismiss() + "");
                if (!isHasDismiss.getDismiss()) // GOMI Code
                    idenWordsNextReturn(extaText);
            }
        });
    }

image

Aoi-hosizora commented 5 years ago

记录,鸵鸟算法+1

/**
     * 具体保存笔记过程,SavingFileAsNote()用
     * @param Path
     * @param mnote
     */
    private void SavingDocxPdfAsNote(final boolean isSaveAsDocx, final String Path, final Note mnote) {
        ShowLogE("SavingDocxAsNote", "Path: " + Path);

        class HasDismiss {
            private boolean dismiss = false;
            HasDismiss() {}
            void setDismiss() { this.dismiss = true; }
            boolean getDismiss() { return this.dismiss; }
        }
        final HasDismiss isHasDismiss = new HasDismiss();

        String Msg = String.format(getResources().getString(R.string.VMNoteActivity_SavingDataMessage), isSaveAsDocx ? "Docx" : "Pdf", Path);
        savingDialog.setTitle(getResources().getString(R.string.VMNoteActivity_SavingData));
        savingDialog.setMessage(Msg);
        savingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                // 取消保存
                isHasDismiss.setDismiss();
            }
        });
        savingDialog.show();

        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(200);
                }
                catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            if (isSaveAsDocx)
                                ToDocUtil.CreateDocxByNote(Path, mnote.getTitle(), mnote.getContent(), true);
                            else
                                ToDocUtil.CreatePdfByNote(Path, mnote.getTitle(), mnote.getContent(), true);

                            if (!isHasDismiss.getDismiss()) {
                                if (savingDialog != null)
                                    savingDialog.dismiss();

                                Toast.makeText(ViewModifyNoteActivity.this, "文件 " + Path + " 保存成功。", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                ShowLogE("SavingDocxPdfAsNote", "Saning is HasDismiss");
                                File f = new File(Path);
                                if (f.exists())
                                    f.delete();
                            }

                        }
                        catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                });
            }
        }).start();
    }

image