From 31e6d8facf85a08d7ee025a1bcf7a2c1230d63d5 Mon Sep 17 00:00:00 2001 From: lidan Date: Wed, 6 Aug 2025 11:10:55 +0800 Subject: [PATCH 1/2] fix code style --- AppScope/app.json5 | 2 +- AppScope/resources/base/element/string.json | 2 +- README.md | 36 ++++++++-------- .../main/ets/common/utils/AddressParse.ets | 4 +- entry/src/main/ets/common/utils/Logger.ets | 7 ++-- .../src/main/ets/common/utils/OCRManager.ets | 3 +- entry/src/main/ets/pages/Index.ets | 4 +- .../ets/{model => viewmodel}/DataModel.ets | 0 .../src/main/ets/views/ConsigneeInfoItem.ets | 6 +-- entry/src/main/ets/views/DialogBuilder.ets | 10 ++--- .../main/resources/base/element/string.json | 42 +++++++++---------- .../main/resources/en_US/element/string.json | 2 +- .../main/resources/zh_CN/element/string.json | 2 +- entry/src/mock/mock-config.json5 | 2 - entry/src/ohosTest/ets/test/Ability.test.ets | 35 ---------------- entry/src/ohosTest/ets/test/List.test.ets | 5 --- entry/src/ohosTest/module.json5 | 11 ----- entry/src/test/List.test.ets | 5 --- entry/src/test/LocalUnit.test.ets | 33 --------------- 19 files changed, 61 insertions(+), 150 deletions(-) rename entry/src/main/ets/{model => viewmodel}/DataModel.ets (100%) delete mode 100644 entry/src/mock/mock-config.json5 delete mode 100644 entry/src/ohosTest/ets/test/Ability.test.ets delete mode 100644 entry/src/ohosTest/ets/test/List.test.ets delete mode 100644 entry/src/ohosTest/module.json5 delete mode 100644 entry/src/test/List.test.ets delete mode 100644 entry/src/test/LocalUnit.test.ets diff --git a/AppScope/app.json5 b/AppScope/app.json5 index 091a1cb..c25242c 100644 --- a/AppScope/app.json5 +++ b/AppScope/app.json5 @@ -1,6 +1,6 @@ { "app": { - "bundleName": "com.example.ocrdemo", + "bundleName": "com.example.corevisionkitocr", "vendor": "example", "versionCode": 1000000, "versionName": "1.0.0", diff --git a/AppScope/resources/base/element/string.json b/AppScope/resources/base/element/string.json index 030636c..ebbaf9e 100644 --- a/AppScope/resources/base/element/string.json +++ b/AppScope/resources/base/element/string.json @@ -2,7 +2,7 @@ "string": [ { "name": "app_name", - "value": "OCRDemo" + "value": "CoreVisionKitOCR" } ] } diff --git a/README.md b/README.md index 4d84d67..4479cc8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## 介绍 -- 本示例基于基础视觉服务,使用`@kit.CoreVisionKit`提供的通用文字识别能力,通过拍照(CameraPicker)或者相册(PhotoViewPicker)方式,将印刷品文字(如:收货信息)转化为图像信息,再利用文字识别技术将图像信息转化为设备可以使用的文本字符,最后可以根据实际业务规则提取结构化数据。 +本示例基于基础视觉服务,使用`@kit.CoreVisionKit`提供的通用文字识别能力,通过拍照(CameraPicker)或者相册(PhotoViewPicker)方式,将印刷品文字(如:收货信息)转化为图像信息,再利用文字识别技术将图像信息转化为设备可以使用的文本字符,最后可以根据实际业务规则提取结构化数据。 ## 效果预览 @@ -21,21 +21,24 @@ ``` entry/src/main/ ├──ets -| ├──common/constants -| | └──CommonConstants.ets // 公共常量类 -| └──common/utils -| | ├──AddressParse.ets // 收货信息解析类 -| | ├──OCRManager.ets // 视觉识别类 -| | ├──PromptActionManager.ets // 弹窗管理类 -| | └──Logger.ets // 日志类 +| ├──common +| | ├──constants +| | | └──CommonConstants.ets // 公共常量类 +| | └──utils +| | ├──AddressParse.ets // 收货信息解析类 +| | ├──OCRManager.ets // 视觉识别类 +| | ├──PromptActionManager.ets // 弹窗管理类 +| | └──Logger.ets // 日志类 | ├──entryability -| | └──EntryAbility.ets // 入口Ability +| | └──EntryAbility.ets // 入口Ability +| ├──viewmodel +| | └──DataModel.ets // UI模型类 | ├──views -| | ├──ConsigneeInfoItem.ets // 列表项UI -| | └──DialogBuilder.ets // 弹窗UI +| | ├──ConsigneeInfoItem.ets // 列表项UI +| | └──DialogBuilder.ets // 弹窗UI | └──pages -| └──Index.ets // 首页 -└──resources // 资源目录 +| └──Index.ets // 首页 +└──resources // 资源目录 ``` ## 具体实现 @@ -49,12 +52,9 @@ entry/src/main/ ## 相关权限 -### 依赖 +无 -1. 本示例依赖 `@ohos/hvigor-ohos-plugin`。 -2. 使用DevEco Studio版本大于本示例推荐版本,请根据 DevEco Studio 提示更新 hvigor 插件版本。 - -### 约束与限制 +## 约束与限制 1. 本示例仅支持标准系统上运行,支持设备:华为手机。 2. Core Vision Kit仅支持中国境内(不包含中国香港、中国澳门、中国台湾)提供服务。 diff --git a/entry/src/main/ets/common/utils/AddressParse.ets b/entry/src/main/ets/common/utils/AddressParse.ets index f05bc38..d8ec0ce 100644 --- a/entry/src/main/ets/common/utils/AddressParse.ets +++ b/entry/src/main/ets/common/utils/AddressParse.ets @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { ConsigneeInfo } from '../../viewmodel/DataModel'; -import { ConsigneeInfo } from "../../model/DataModel"; /** * This example uses regular expression processing to extract formatted data, @@ -135,7 +135,7 @@ export class AddressParse { // Otherwise select the longest segment return segments.reduce((longest, current) => - current.length > longest.length ? current : longest, ""); + current.length > longest.length ? current : longest, ''); } // Finally, return the entire text diff --git a/entry/src/main/ets/common/utils/Logger.ets b/entry/src/main/ets/common/utils/Logger.ets index b5ef505..ffece6f 100644 --- a/entry/src/main/ets/common/utils/Logger.ets +++ b/entry/src/main/ets/common/utils/Logger.ets @@ -17,8 +17,8 @@ import { hilog } from '@kit.PerformanceAnalysisKit'; export class Logger { private static readonly DOMAIN: number = 0xFF00; - private static readonly TAG: string = 'com.example.orcdemo'; - private static readonly PREFIX: string = '[ocr-demo]'; + private static readonly TAG: string = 'com.example.corevisionkitocr'; + private static readonly PREFIX: string = '[ocr]'; public static debug(logTag: string, messageFormat: string, ...args: Object[]): void { hilog.debug(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); @@ -40,5 +40,6 @@ export class Logger { hilog.fatal(Logger.DOMAIN, Logger.TAG, `${Logger.PREFIX} ${logTag}: ${messageFormat}`, args); } - private constructor() {} + private constructor() { + } } diff --git a/entry/src/main/ets/common/utils/OCRManager.ets b/entry/src/main/ets/common/utils/OCRManager.ets index bb5c753..4252e3b 100644 --- a/entry/src/main/ets/common/utils/OCRManager.ets +++ b/entry/src/main/ets/common/utils/OCRManager.ets @@ -65,7 +65,8 @@ export class OCRManager { } static async recognizeText(uri: string): Promise { - // Visual information to be recognized. Currently, only the visual information of the PixelMap type in color data format RGBA_8888 is supported. + // Visual information to be recognized. + // Currently, only the visual information of the PixelMap type in color data format RGBA_8888 is supported. let visionInfo: textRecognition.VisionInfo = { pixelMap: await OCRManager.getPixelMap(uri) }; let result: textRecognition.TextRecognitionResult = await textRecognition.recognizeText(visionInfo); visionInfo.pixelMap.release(); diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 6e1f153..2424aad 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -15,7 +15,7 @@ import { ComponentContent, LoadingDialog } from '@kit.ArkUI'; import { PromptActionManager } from '../common/utils/PromptActionManager'; -import { ConsigneeInfo, Params } from '../model/DataModel'; +import { ConsigneeInfo, Params } from '../viewmodel/DataModel'; import { AddressParse } from '../common/utils/AddressParse'; import CommonConstants from '../common/constants/CommonConstants'; import { ConsigneeInfoItem } from '../views/ConsigneeInfoItem'; @@ -141,7 +141,7 @@ struct Index { } }, (item: ConsigneeInfo, index: number) => JSON.stringify(item) + index) } - .width("100%") + .width('100%') .height(LayoutPolicy.matchParent) .scrollBar(BarState.Off) .divider({ strokeWidth: 0.5, color: $r('app.color.divider_color') }) diff --git a/entry/src/main/ets/model/DataModel.ets b/entry/src/main/ets/viewmodel/DataModel.ets similarity index 100% rename from entry/src/main/ets/model/DataModel.ets rename to entry/src/main/ets/viewmodel/DataModel.ets diff --git a/entry/src/main/ets/views/ConsigneeInfoItem.ets b/entry/src/main/ets/views/ConsigneeInfoItem.ets index 2b7ab05..6df9733 100644 --- a/entry/src/main/ets/views/ConsigneeInfoItem.ets +++ b/entry/src/main/ets/views/ConsigneeInfoItem.ets @@ -13,8 +13,8 @@ * limitations under the License. */ -import CommonConstants from "../common/constants/CommonConstants"; -import { ConsigneeInfo } from "../model/DataModel"; +import CommonConstants from '../common/constants/CommonConstants'; +import { ConsigneeInfo } from '../viewmodel/DataModel'; @Builder export function ConsigneeInfoItem(item: ConsigneeInfo, checkAvailable?: () => void) { @@ -46,7 +46,7 @@ export function ConsigneeInfoItem(item: ConsigneeInfo, checkAvailable?: () => vo checkAvailable?.(); }) } - .width("100%") + .width('100%') .constraintSize({ minHeight: 48 }) .justifyContent(FlexAlign.SpaceBetween) } \ No newline at end of file diff --git a/entry/src/main/ets/views/DialogBuilder.ets b/entry/src/main/ets/views/DialogBuilder.ets index 11c1368..9a15ceb 100644 --- a/entry/src/main/ets/views/DialogBuilder.ets +++ b/entry/src/main/ets/views/DialogBuilder.ets @@ -13,10 +13,10 @@ * limitations under the License. */ -import { Params } from "../model/DataModel" -import { common } from "@kit.AbilityKit" -import { PromptActionManager } from "../common/utils/PromptActionManager" -import { OCRManager } from "../common/utils/OCRManager" +import { Params } from '../viewmodel/DataModel' +import { common } from '@kit.AbilityKit' +import { PromptActionManager } from '../common/utils/PromptActionManager' +import { OCRManager } from '../common/utils/OCRManager' @Builder export function dialogBuilder(params: Params): void { @@ -69,7 +69,7 @@ export function dialogBuilder(params: Params): void { }) } } - .size({ width: "calc(100% - 32vp)", height: 235 }) + .size({ width: 'calc(100% - 32vp)', height: 235 }) .borderRadius(32) .backgroundColor($r('app.color.light_background')) .padding(16) diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index 9d60a2b..c17c641 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -10,87 +10,87 @@ }, { "name": "EntryAbility_label", - "value": "OCRDemo" + "value": "CoreVisionKitOCR" }, { "name": "page_title", - "value": "图片识别" + "value": "Picture recognition" }, { "name": "page_subtitle", - "value": "新增地址" + "value": "New address" }, { "name": "dialog_title", - "value": "图片识别" + "value": "Picture recognition" }, { "name": "dialog_subtitle", - "value": "选择获取图片的方式" + "value": "Select the method of obtaining the picture" }, { "name": "dialog_camera_btn", - "value": "拍照" + "value": "Photograph" }, { "name": "dialog_photo_btn", - "value": "相册" + "value": "Album" }, { "name": "dialog_cancel_btn", - "value": "取消" + "value": "cancel" }, { "name": "loading_content", - "value": "图片识别中" + "value": "Recognition..." }, { "name": "input_placeholder", - "value": "图片识别的文本到此处,将自动识别收货信息例:丽丽,139*******,广东省深圳市宝安区某小区" + "value": "Text recognized from images will appear here, automatically identifying shipping information.Example: Lili, 139*******, Certain Community, Bao'an District, Shenzhen City, Guangdong Province" }, { "name": "open_dialog_btn", - "value": "图片识别" + "value": "Recognition" }, { "name": "parse_btn", - "value": "识别" + "value": "Parse" }, { "name": "empty_toast", - "value": "请输入要识别的文本" + "value": "Please enter the text to be parsed" }, { "name": "save_btn", - "value": "保存地址" + "value": "Saving address" }, { "name": "save_success_toast", - "value": "保存成功" + "value": "Saving successfully" }, { "name": "list_name_label", - "value": "收货人" + "value": "Name" }, { "name": "list_phone_label", - "value": "电话" + "value": "Phone" }, { "name": "list_address_label", - "value": "地址" + "value": "Address" }, { "name": "list_name_placeholder", - "value": "收货人姓名" + "value": "Consignee's name" }, { "name": "list_phone_placeholder", - "value": "收货人电话" + "value": "Consignee's phone number" }, { "name": "list_address_placeholder", - "value": "详细地址与门牌号" + "value": "Full address and house number" } ] } \ No newline at end of file diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index 1d8fd0a..c17c641 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -10,7 +10,7 @@ }, { "name": "EntryAbility_label", - "value": "OCRDemo" + "value": "CoreVisionKitOCR" }, { "name": "page_title", diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 9d60a2b..2901507 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -10,7 +10,7 @@ }, { "name": "EntryAbility_label", - "value": "OCRDemo" + "value": "CoreVisionKitOCR" }, { "name": "page_title", diff --git a/entry/src/mock/mock-config.json5 b/entry/src/mock/mock-config.json5 deleted file mode 100644 index 7a73a41..0000000 --- a/entry/src/mock/mock-config.json5 +++ /dev/null @@ -1,2 +0,0 @@ -{ -} \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/Ability.test.ets b/entry/src/ohosTest/ets/test/Ability.test.ets deleted file mode 100644 index 85c78f6..0000000 --- a/entry/src/ohosTest/ets/test/Ability.test.ets +++ /dev/null @@ -1,35 +0,0 @@ -import { hilog } from '@kit.PerformanceAnalysisKit'; -import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; - -export default function abilityTest() { - describe('ActsAbilityTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }) - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }) - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }) - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }) - it('assertContain', 0, () => { - // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - let a = 'abc'; - let b = 'b'; - // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - expect(a).assertContain(b); - expect(a).assertEqual(a); - }) - }) -} \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets deleted file mode 100644 index 794c7dc..0000000 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ /dev/null @@ -1,5 +0,0 @@ -import abilityTest from './Ability.test'; - -export default function testsuite() { - abilityTest(); -} \ No newline at end of file diff --git a/entry/src/ohosTest/module.json5 b/entry/src/ohosTest/module.json5 deleted file mode 100644 index 509a3a2..0000000 --- a/entry/src/ohosTest/module.json5 +++ /dev/null @@ -1,11 +0,0 @@ -{ - "module": { - "name": "entry_test", - "type": "feature", - "deviceTypes": [ - "phone" - ], - "deliveryWithInstall": true, - "installationFree": false - } -} diff --git a/entry/src/test/List.test.ets b/entry/src/test/List.test.ets deleted file mode 100644 index bb5b5c3..0000000 --- a/entry/src/test/List.test.ets +++ /dev/null @@ -1,5 +0,0 @@ -import localUnitTest from './LocalUnit.test'; - -export default function testsuite() { - localUnitTest(); -} \ No newline at end of file diff --git a/entry/src/test/LocalUnit.test.ets b/entry/src/test/LocalUnit.test.ets deleted file mode 100644 index 165fc16..0000000 --- a/entry/src/test/LocalUnit.test.ets +++ /dev/null @@ -1,33 +0,0 @@ -import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; - -export default function localUnitTest() { - describe('localUnitTest', () => { - // Defines a test suite. Two parameters are supported: test suite name and test suite function. - beforeAll(() => { - // Presets an action, which is performed only once before all test cases of the test suite start. - // This API supports only one parameter: preset action function. - }); - beforeEach(() => { - // Presets an action, which is performed before each unit test case starts. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: preset action function. - }); - afterEach(() => { - // Presets a clear action, which is performed after each unit test case ends. - // The number of execution times is the same as the number of test cases defined by **it**. - // This API supports only one parameter: clear action function. - }); - afterAll(() => { - // Presets a clear action, which is performed after all test cases of the test suite end. - // This API supports only one parameter: clear action function. - }); - it('assertContain', 0, () => { - // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - let a = 'abc'; - let b = 'b'; - // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - expect(a).assertContain(b); - expect(a).assertEqual(a); - }); - }); -} \ No newline at end of file -- Gitee From 5088df69721b2b18be949a676cfec10e6545a1dd Mon Sep 17 00:00:00 2001 From: lidan Date: Wed, 6 Aug 2025 17:30:31 +0800 Subject: [PATCH 2/2] update string.json --- entry/src/main/resources/base/element/string.json | 4 ++-- entry/src/main/resources/en_US/element/string.json | 4 ++-- entry/src/main/resources/zh_CN/element/string.json | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/entry/src/main/resources/base/element/string.json b/entry/src/main/resources/base/element/string.json index c17c641..9c19e2f 100644 --- a/entry/src/main/resources/base/element/string.json +++ b/entry/src/main/resources/base/element/string.json @@ -26,7 +26,7 @@ }, { "name": "dialog_subtitle", - "value": "Select the method of obtaining the picture" + "value": "Image capture options" }, { "name": "dialog_camera_btn", @@ -46,7 +46,7 @@ }, { "name": "input_placeholder", - "value": "Text recognized from images will appear here, automatically identifying shipping information.Example: Lili, 139*******, Certain Community, Bao'an District, Shenzhen City, Guangdong Province" + "value": "Text recognized from images will appear here, automatically identifying shipping information.Example: Lili, 139******* " }, { "name": "open_dialog_btn", diff --git a/entry/src/main/resources/en_US/element/string.json b/entry/src/main/resources/en_US/element/string.json index c17c641..9c19e2f 100644 --- a/entry/src/main/resources/en_US/element/string.json +++ b/entry/src/main/resources/en_US/element/string.json @@ -26,7 +26,7 @@ }, { "name": "dialog_subtitle", - "value": "Select the method of obtaining the picture" + "value": "Image capture options" }, { "name": "dialog_camera_btn", @@ -46,7 +46,7 @@ }, { "name": "input_placeholder", - "value": "Text recognized from images will appear here, automatically identifying shipping information.Example: Lili, 139*******, Certain Community, Bao'an District, Shenzhen City, Guangdong Province" + "value": "Text recognized from images will appear here, automatically identifying shipping information.Example: Lili, 139******* " }, { "name": "open_dialog_btn", diff --git a/entry/src/main/resources/zh_CN/element/string.json b/entry/src/main/resources/zh_CN/element/string.json index 2901507..a6e8334 100644 --- a/entry/src/main/resources/zh_CN/element/string.json +++ b/entry/src/main/resources/zh_CN/element/string.json @@ -46,7 +46,7 @@ }, { "name": "input_placeholder", - "value": "图片识别的文本到此处,将自动识别收货信息例:丽丽,139*******,广东省深圳市宝安区某小区" + "value": "图片识别的文本到此处,将自动识别收货信息 例:丽丽,139*******,广东省深圳市宝安区某小区" }, { "name": "open_dialog_btn", -- Gitee