From 99fd96876dc2f8220d46271ea6f4260dbc045255 Mon Sep 17 00:00:00 2001 From: guoyuefeng Date: Mon, 20 Feb 2023 00:44:09 -0800 Subject: [PATCH] optimize the app Signed-off-by: guoyuefeng --- .../ets/MainAbility/component/animalItem.ets | 3 ++- .../src/main/ets/MainAbility/pages/idInfo.ets | 4 +++- .../src/main/ets/MainAbility/pages/index.ets | 13 ++++++++++++- .../src/main/ets/MainAbility/pages/save2File.ets | 4 +++- .../src/main/ets/MainAbility/tools/ToolUtils.ets | 16 ++++++++++++---- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/FA/OCRDemo/entry/src/main/ets/MainAbility/component/animalItem.ets b/FA/OCRDemo/entry/src/main/ets/MainAbility/component/animalItem.ets index 25dfa82f..7a1564b7 100755 --- a/FA/OCRDemo/entry/src/main/ets/MainAbility/component/animalItem.ets +++ b/FA/OCRDemo/entry/src/main/ets/MainAbility/component/animalItem.ets @@ -67,6 +67,7 @@ export default struct AnimalItem { ToolUtils.ocrResult(ocrPath, (result) => { console.log(this.TAG + ':ocr result=>'+result); + ToolUtils.isStart = false; AppStorage.SetOrCreate('slOcrResult', result); @@ -79,4 +80,4 @@ export default struct AnimalItem { .width('100%') .height('100%') } -} \ No newline at end of file +} diff --git a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/idInfo.ets b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/idInfo.ets index b9df75ca..d5d90182 100755 --- a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/idInfo.ets +++ b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/idInfo.ets @@ -61,6 +61,7 @@ struct IdInfo { ToolUtils.ocrResult(ToolUtils.aobamao, (result) => { console.log('111 OCR result = ' + result); this.result = result; + ToolUtils.isStart = false; this.ocrDialog.close(); }); }) @@ -76,6 +77,7 @@ struct IdInfo { console.log('111 OCR result = ' + result); this.result = result; this.ocrDialog.close(); + ToolUtils.isStart = false; }); }) .margin('10vp') @@ -136,4 +138,4 @@ struct ocrProgressDialog { .alignItems(HorizontalAlign.End) .backgroundColor($r("app.color.white_bg_color")) } -} \ No newline at end of file +} diff --git a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/index.ets b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/index.ets index ebcd2c0b..f3556fcc 100755 --- a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/index.ets +++ b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/index.ets @@ -25,7 +25,7 @@ struct Index { isInitOCR: boolean = false; aboutToAppear() { - console.log('OCR first page aboutToAppear'); + console.error('OCR first page aboutToAppear'); ToolUtils.initOCR((result) => { console.log('OCR init OCR result = ' + result); if (0 == result) { @@ -45,6 +45,17 @@ struct Index { Button('动物图片文字识别') .fontSize('40fp') .onClick(() => { + + + +// ToolUtils.initOCR((result) => { +// console.error('OCR init OCR result = ' + result); +// prompt.showToast({message:'OCR init result' + result, duration:5000}); +// if (0 == result) { +// this.isInitOCR = true; +// } +// }); + if (!this.isInitOCR) { prompt.showToast({message:'OCR未初始化,请稍后再试。。。', duration:5000}); return; diff --git a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/save2File.ets b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/save2File.ets index 61dfefcf..8754c5fa 100755 --- a/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/save2File.ets +++ b/FA/OCRDemo/entry/src/main/ets/MainAbility/pages/save2File.ets @@ -60,6 +60,7 @@ struct Save2File { ToolUtils.ocrResult(ToolUtils.testImage1, (result) => { console.log('111 OCR result = ' + result); this.ocrDialog.close(); + ToolUtils.isStart = false; let path = this.dir + 'ocrresult1.txt'; console.log('OCR PATH = ' + path); @@ -86,6 +87,7 @@ struct Save2File { ToolUtils.ocrResult(ToolUtils.testImage2, (result) => { console.log('111 OCR result = ' + result); this.ocrDialog.close(); + ToolUtils.isStart = false; let path = this.dir + 'ocrresult2.txt'; console.log('OCR PATH = ' + path); let fd = fileio.openSync(path, 0o100 | 0o2, 0o666); @@ -156,4 +158,4 @@ struct ocrProgressDialog { .alignItems(HorizontalAlign.End) .backgroundColor($r("app.color.white_bg_color")) } -} \ No newline at end of file +} diff --git a/FA/OCRDemo/entry/src/main/ets/MainAbility/tools/ToolUtils.ets b/FA/OCRDemo/entry/src/main/ets/MainAbility/tools/ToolUtils.ets index c400f5ca..5fdf008f 100755 --- a/FA/OCRDemo/entry/src/main/ets/MainAbility/tools/ToolUtils.ets +++ b/FA/OCRDemo/entry/src/main/ets/MainAbility/tools/ToolUtils.ets @@ -15,16 +15,24 @@ import tessApi from "libtesseract.so"; + export class ToolUtil { + isStart : boolean = false;false; + + //该方法需要在回调成功的地方将ToolUtil的isStart赋值为false ocrResult(path, callback){ - console.log('OCR ocrResult path = ' + path); - tessApi.startOCR(path, callback); + if (!this.isStart) { + this.isStart = true; + console.log('OCR ocrResult path = ' + path); + tessApi.startOCR(path, callback); + } } initOCR(callback) { - console.log('OCR init'); + console.error('OCR init'); tessApi.initOCR('/system/app/', 'eng+chi_sim', callback); + console.error('OCR init end'); } // initOCR() { @@ -53,4 +61,4 @@ export class ToolUtil { } let mToolUtil = new ToolUtil(); -export default mToolUtil as ToolUtil; \ No newline at end of file +export default mToolUtil as ToolUtil; -- Gitee