diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets deleted file mode 100644 index 5803dd878fe4a8fcffb79be6d1229f1a46123515..0000000000000000000000000000000000000000 --- a/entry/src/main/ets/pages/Index.ets +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. - */ - -import { SheetExtractionPage } from './SheetExtractionPage'; - -@Entry -@Component -struct Index { - @Provide('pathStack') pageInfo: NavPathStack = new NavPathStack(); - - @Builder - PageMap(name: string) { - if (name === 'SheetExtraction') { - SheetExtractionPage() - } - } - - build() { - Navigation(this.pageInfo) { - Column({ space: 20 }) { - Button('SheetExtraction', { stateEffect: true, type: ButtonType.Capsule }) - .width('80%') - .height(40) - .onClick(() => { - this.pageInfo.pushPath({ name: 'SheetExtraction' }); - }) - } - .padding({ top: 40 }) - } - .title('SheetDemo') - .navDestination(this.PageMap) - .mode(NavigationMode.Stack) - } -} diff --git a/sheetrecognitionlibrary/Index.ets b/sheetrecognitionlibrary/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..020d933017970abe66e1939b2965279514f9d4a6 --- /dev/null +++ b/sheetrecognitionlibrary/Index.ets @@ -0,0 +1,5 @@ +export { MainComponent } from './src/main/ets/components/MainComponent'; +export { SheetRecognitionController } from './src/main/ets/SheetRecognitionController'; + + + diff --git a/sheetrecognitionlibrary/build-profile.json5 b/sheetrecognitionlibrary/build-profile.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e6773f9f5d76a66d6d19fddc9c6ddb3f5621d3b1 --- /dev/null +++ b/sheetrecognitionlibrary/build-profile.json5 @@ -0,0 +1,31 @@ +{ + "apiType": "stageMode", + "buildOption": { + }, + "buildOptionSet": [ + { + "name": "release", + "arkOptions": { + "obfuscation": { + "ruleOptions": { + "enable": false, + "files": [ + "./obfuscation-rules.txt" + ] + }, + "consumerFiles": [ + "./consumer-rules.txt" + ] + } + }, + }, + ], + "targets": [ + { + "name": "default" + }, + { + "name": "ohosTest" + } + ] +} diff --git a/sheetrecognitionlibrary/consumer-rules.txt b/sheetrecognitionlibrary/consumer-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/sheetrecognitionlibrary/hvigorfile.ts b/sheetrecognitionlibrary/hvigorfile.ts new file mode 100644 index 0000000000000000000000000000000000000000..805c5d7f6809c51cff0b4adcc1142979f8f864b6 --- /dev/null +++ b/sheetrecognitionlibrary/hvigorfile.ts @@ -0,0 +1,6 @@ +import { harTasks } from '@ohos/hvigor-ohos-plugin'; + +export default { + system: harTasks, /* Built-in plugin of Hvigor. It cannot be modified. */ + plugins: [] /* Custom plugin to extend the functionality of Hvigor. */ +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/obfuscation-rules.txt b/sheetrecognitionlibrary/obfuscation-rules.txt new file mode 100644 index 0000000000000000000000000000000000000000..272efb6ca3f240859091bbbfc7c5802d52793b0b --- /dev/null +++ b/sheetrecognitionlibrary/obfuscation-rules.txt @@ -0,0 +1,23 @@ +# Define project specific obfuscation rules here. +# You can include the obfuscation configuration files in the current module's build-profile.json5. +# +# For more details, see +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/source-obfuscation-V5 + +# Obfuscation options: +# -disable-obfuscation: disable all obfuscations +# -enable-property-obfuscation: obfuscate the property names +# -enable-toplevel-obfuscation: obfuscate the names in the global scope +# -compact: remove unnecessary blank spaces and all line feeds +# -remove-log: remove all console.* statements +# -print-namecache: print the name cache that contains the mapping from the old names to new names +# -apply-namecache: reuse the given cache file + +# Keep options: +# -keep-property-name: specifies property names that you want to keep +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-obfuscation \ No newline at end of file diff --git a/entry/oh-package.json5 b/sheetrecognitionlibrary/oh-package.json5 similarity index 57% rename from entry/oh-package.json5 rename to sheetrecognitionlibrary/oh-package.json5 index 248c3b7541a589682a250f86a6d3ecf7414d2d6a..79c13132cec5ac313fc47acbd9d33727bb5e0ef8 100644 --- a/entry/oh-package.json5 +++ b/sheetrecognitionlibrary/oh-package.json5 @@ -1,10 +1,9 @@ { - "name": "entry", + "name": "sheetrecognitionlibrary", "version": "1.0.0", "description": "Please describe the basic information.", - "main": "", + "main": "Index.ets", "author": "", - "license": "", + "license": "Apache-2.0", "dependencies": {} -} - +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/ets/SheetRecognitionController.ets b/sheetrecognitionlibrary/src/main/ets/SheetRecognitionController.ets new file mode 100644 index 0000000000000000000000000000000000000000..af5f73d07e43b217957be303d94a563156992afd --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/SheetRecognitionController.ets @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { window } from '@kit.ArkUI'; +import { common } from '@kit.AbilityKit'; +import { WindowUtil } from './utils/WindowUtil'; + +export class SheetRecognitionController { + public static initWindowConfig(windowStage: window.WindowStage): void { + WindowUtil.initialize(windowStage); + } + + public static updatedColorMode(context: common.UIAbilityContext): void { + WindowUtil.updatedColorMode(context); + } +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets b/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets new file mode 100644 index 0000000000000000000000000000000000000000..1e0f27713d7f84a700ae69c5a5355311ba754ed6 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets @@ -0,0 +1,66 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + */ + +import { LengthMetrics } from '@kit.ArkUI'; +import { CommonConstants } from '../constants/CommonConstants'; +import { BreakpointType } from '../utils/BreakpointSystem'; +import { SheetExtractionComponent } from './SheetExtractionComponent'; + +@Component +export struct MainComponent { + @Provide(CommonConstants.KEY_PREFIX_SYSTEM_PATHSTACK) pathStack: NavPathStack = new NavPathStack(); + @StorageProp(CommonConstants.KEY_PREFIX_BREAKPOINT) currentBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; + @StorageLink(CommonConstants.KEY_PREFIX_STATUS_BAR_HEIGHT) topStatusBarHeight: number = 0; + @StorageLink(CommonConstants.KEY_PREFIX_NAVIGATOR_BAR_HEIGHT) navBarHeight: number = 0; + + @Builder + PageMap(name: string) { + if (name === 'SheetExtraction') { + SheetExtractionComponent() + } + } + + build() { + Column() { + Navigation(this.pathStack) { + Column() { + Button($r('app.string.btn_recognize_title'), { stateEffect: true, type: ButtonType.Capsule }) + .width(this.currentBp == WidthBreakpoint.WIDTH_SM ? CommonConstants.FULL_PERCENT : + CommonConstants.BTN_MAX_WIDTH) + .height(CommonConstants.BTN_HEIGHT) + .onClick(() => { + this.pathStack.pushPath({ name: 'SheetExtraction' }); + }) + } + .width(CommonConstants.FULL_PERCENT) + .height(CommonConstants.FULL_PERCENT) + .justifyContent(FlexAlign.End) + .padding({ + right: this.currentBp === WidthBreakpoint.WIDTH_SM ? $r('sys.float.padding_level8') : + $r('sys.float.padding_level0'), + left: this.currentBp === WidthBreakpoint.WIDTH_SM ? $r('sys.float.padding_level8') : + $r('sys.float.padding_level0'), + bottom: CommonConstants.BOTTOM_MARGIN + }) + } + .title($r('app.string.title'), { + paddingStart: LengthMetrics.resource(new BreakpointType({ + sm: $r('app.float.margin_size_SM'), + md: $r('app.float.margin_size_MD'), + lg: $r('app.float.margin_size_LG'), + xl: $r('app.float.margin_size_LG') + }).getValue(this.currentBp)) + }) + .titleMode(NavigationTitleMode.Full) + .mode(NavigationMode.Stack) + .navDestination(this.PageMap) + } + .width(CommonConstants.FULL_PERCENT) + .height(CommonConstants.FULL_PERCENT) + .padding({ + top: this.topStatusBarHeight, + }) + } +} + diff --git a/entry/src/main/ets/pages/SheetExtractionPage.ets b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets similarity index 51% rename from entry/src/main/ets/pages/SheetExtractionPage.ets rename to sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets index 20896e441a3668da441ac4d18c37acafe315ba73..e64fbcea637c52518bced1910697a23f825faa46 100644 --- a/entry/src/main/ets/pages/SheetExtractionPage.ets +++ b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets @@ -6,13 +6,15 @@ import { DocType, DocumentScanner, SaveOption } from '@kit.VisionKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { picker } from '@kit.CoreFileKit'; import { fileIo as fs } from '@kit.CoreFileKit'; -import { promptAction } from '@kit.ArkUI'; +import { FileUtil } from '../utils/FileUtil'; +import { CommonConstants } from '../constants/CommonConstants'; const TAG: string = 'SheetExtractionPage'; @Component -export struct SheetExtractionPage { +export struct SheetExtractionComponent { @Consume('pathStack') pathStack: NavPathStack; + @State filePath: string = ''; build() { Stack({ alignContent: Alignment.Top }) { @@ -21,22 +23,37 @@ export struct SheetExtractionPage { DocumentScanner({ scannerConfig: { //The DocType.SHEET attribute needs to be configured to support table extraction. - supportType: [DocType.DOC, DocType.SHEET], - isGallerySupported: true + supportType: [DocType.SHEET], + isGallerySupported: true, + saveOptions: [SaveOption.JPG, SaveOption.EXCEL] }, onResult: (code: number, saveType: SaveOption, uris: string[]) => { //200 indicates success, SaveOption.EXCEL indicates that the Excel file is returned. //and uris indicates the file path. - if (code === 200 && saveType == SaveOption.EXCEL) { - this.saveToExcel(uris[0]); + if (code === 200) { + if (uris.length > 0) { + if (saveType == SaveOption.EXCEL) { + let res = FileUtil.accessFileSync(uris[0]); + if (!res) { + this.saveToExcel(uris[0], saveType); + } + } + + if (saveType == SaveOption.JPG) { + let res = FileUtil.accessFileSync(uris[0]); + if (!res) { + this.saveToExcel(uris[0], saveType); + } + } + } } this.pathStack.pop(); } }) - .size({ width: '100%', height: '100%' }) + .size({ width: CommonConstants.FULL_PERCENT, height: CommonConstants.FULL_PERCENT }) } - .width('100%') - .height('100%') + .width(CommonConstants.FULL_PERCENT) + .height(CommonConstants.FULL_PERCENT) } /** @@ -44,26 +61,35 @@ export struct SheetExtractionPage { * @param uri * @returns */ - async saveToExcel(uri: string): Promise { + async saveToExcel(uri: string, type: SaveOption): Promise { let originFile: fs.File | undefined = undefined; let targetFile: fs.File | undefined = undefined; let documentSaveOptions = new picker.DocumentSaveOptions; documentSaveOptions.newFileNames = [`DocumentScanner_${new Date().getTime()}`]; - documentSaveOptions.fileSuffixChoices = ['.xlsx']; - let documentViewPicker = new picker.DocumentViewPicker(getContext(this)); + if (type === SaveOption.JPG) { + documentSaveOptions.fileSuffixChoices = ['.JPG']; + } + if (type === SaveOption.EXCEL) { + documentSaveOptions.fileSuffixChoices = ['.xlsx']; + } + let documentViewPicker = new picker.DocumentViewPicker(this.getUIContext().getHostContext()!); let saveUri = new Promise(async (resolve, reject) => { documentViewPicker.save(documentSaveOptions).then((saveResult: Array) => { - let pdfUri = saveResult[0]; + let fileUri = saveResult[0]; try { originFile = fs.openSync(uri, fs.OpenMode.READ_ONLY); - targetFile = fs.openSync(pdfUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + targetFile = fs.openSync(fileUri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); fs.copyFileSync(originFile.fd, targetFile.fd); - promptAction.showToast({ - message: 'The file is saved successfully.', + this.getUIContext().getPromptAction().showToast({ + message: $r('app.string.save_success'), alignment: Alignment.Center }); - return resolve(pdfUri); + return resolve(fileUri); } catch (error) { + this.getUIContext().getPromptAction().showToast({ + message: $r('app.string.save_error'), + alignment: Alignment.Center + }); hilog.error(0x0001, TAG, `error: ${error}`); reject(''); } finally { @@ -74,4 +100,5 @@ export struct SheetExtractionPage { }) return saveUri; } -} \ No newline at end of file +} + diff --git a/sheetrecognitionlibrary/src/main/ets/constants/CommonConstants.ets b/sheetrecognitionlibrary/src/main/ets/constants/CommonConstants.ets new file mode 100644 index 0000000000000000000000000000000000000000..bbb9688b83443e7377f2454451213e4c4320e589 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/constants/CommonConstants.ets @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Common constants for common component. + */ +export class CommonConstants { + public static readonly FULL_PERCENT: string = '100%'; + public static readonly EIGHTY_PERCENT: string = '80%'; + // Duration. + public static readonly DURATION: number = 100; + // bottom margin + public static readonly BOTTOM_MARGIN: number = 44; + public static readonly BTN_HEIGHT: number = 40; + public static readonly BTN_MAX_WIDTH: number = 448; + public static readonly BTN_BG_HEIGHT: number = 40; + public static readonly CARD_FONT_SIZE: number = 16; + public static readonly BOTTOM_BTN_BG: number = 100; + /** + * AppStorage key prefix. + */ + public static readonly KEY_PREFIX: string = 'SheetRecognition'; + public static readonly KEY_PREFIX_BREAKPOINT: string = CommonConstants.KEY_PREFIX + 'CurrentBreakpoint'; + public static readonly KEY_PREFIX_NAVIGATOR_BAR_HEIGHT: string = CommonConstants.KEY_PREFIX + 'NavigatorBarHeight'; + public static readonly KEY_PREFIX_STATUS_BAR_HEIGHT: string = CommonConstants.KEY_PREFIX + 'StatusBarHeight'; + public static readonly KEY_PREFIX_SYSTEM_UICONTEXT: string = CommonConstants.KEY_PREFIX + 'UIContext'; + public static readonly KEY_PREFIX_SYSTEM_PATHSTACK: string = CommonConstants.KEY_PREFIX + 'PathStack'; +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/ets/utils/BreakpointSystem.ets b/sheetrecognitionlibrary/src/main/ets/utils/BreakpointSystem.ets new file mode 100644 index 0000000000000000000000000000000000000000..8175af4620833c20640165acdc4efea8aff6e455 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/utils/BreakpointSystem.ets @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { CommonConstants } from '../constants/CommonConstants'; + +export interface BreakpointTypes { + xs?: T; + sm: T; + md: T; + lg: T; + xl?: T; +} + +export class BreakpointType { + private xs: T; + private sm: T; + private md: T; + private lg: T; + private xl: T; + + public constructor(param: BreakpointTypes) { + this.xs = param.xs ?? param.sm; + this.sm = param.sm; + this.md = param.md; + this.lg = param.lg; + this.xl = param.xl ?? param.lg; + } + + public getValue(currentBreakpoint: WidthBreakpoint): T { + if (currentBreakpoint === WidthBreakpoint.WIDTH_XS) { + return this.xs; + } + if (currentBreakpoint === WidthBreakpoint.WIDTH_SM) { + return this.sm; + } + if (currentBreakpoint === WidthBreakpoint.WIDTH_MD) { + return this.md; + } + if (currentBreakpoint === WidthBreakpoint.WIDTH_XL) { + return this.xl; + } + + return this.lg; + } +} +; + +export class BreakpointSystem { + private static instance: BreakpointSystem; + private currentBreakpoint: WidthBreakpoint = WidthBreakpoint.WIDTH_MD; + private uiContext: UIContext = AppStorage.get(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT) as UIContext; + + public static getInstance(): BreakpointSystem { + if (!BreakpointSystem.instance) { + BreakpointSystem.instance = new BreakpointSystem(); + } + return BreakpointSystem.instance; + } + + public updateBp(): void { + let bpWidth: WidthBreakpoint = this.uiContext.getWindowWidthBreakpoint(); + this.currentBreakpoint = bpWidth; + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_BREAKPOINT, this.currentBreakpoint); + } +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets b/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets new file mode 100644 index 0000000000000000000000000000000000000000..79749f8a5e807458f7996347121a0c597fda86e6 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { BusinessError } from '@kit.BasicServicesKit'; +import { fileIo as fs, ReadOptions, WriteOptions } from '@kit.CoreFileKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; + +const TAG: string = '[FileUtil]'; + +export class FileUtil { + public static accessFileSync(path: string): boolean { + try { + return fs.accessSync(path); + } catch (error) { + const err = error as BusinessError; + hilog.error(0x0000, TAG, `Operate file failed. Cause code: ${err.code}, message: ${err.message}`); + } + return false; + } + + public static getFilePath(context: Context, docUri: string): string { + return context.filesDir + '/' + FileUtil.getFileName(docUri); + } + + public static getFileName(path: string): string { + const normalizedPath = path.replace(/\\/g, '/'); + const lastSlashIndex = normalizedPath.lastIndexOf('/'); + const fullName = lastSlashIndex >= 0 ? normalizedPath.substring(lastSlashIndex + 1) : normalizedPath; + return fullName; + } + + public static getFileSize(path: string): number { + try { + if (fs.accessSync(path)) { + return Math.floor(fs.lstatSync(path).size / 1024 / 1024 * 100) / 100; + } + } catch (error) { + const err = error as BusinessError; + hilog.error(0x0000, TAG, `Operate file failed. Cause code: ${err.code}, message: ${err.message}`); + } + return 0; + } + + public static readWriteFile(srcFilePath: string, dstFilePath: string): void { + let srcFile: fs.File = null!; + let destFile: fs.File = null!; + try { + if (fs.accessSync(dstFilePath)) { + fs.unlink(dstFilePath) + } + srcFile = fs.openSync(srcFilePath, fs.OpenMode.READ_ONLY); + destFile = fs.openSync(dstFilePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); + // Reads the content of the source file and writes it to the destination file + let bufSize = 4096; + let readSize = 0; + let buf = new ArrayBuffer(bufSize); + let readOptions: ReadOptions = { + offset: readSize, + length: bufSize + }; + let readLen = fs.readSync(srcFile.fd, buf, readOptions); + while (readLen > 0) { + readSize += readLen; + let writeOptions: WriteOptions = { + length: readLen + }; + fs.writeSync(destFile.fd, buf, writeOptions); + readOptions.offset = readSize; + readLen = fs.readSync(srcFile.fd, buf, readOptions); + } + } catch (error) { + const err = error as BusinessError; + hilog.error(0x0000, TAG, `Write file failed. Cause code: ${err.code}, message: ${err.message}`); + } finally { + try { + fs.close(srcFile); + fs.close(destFile); + } catch (error) { + const err = error as BusinessError; + hilog.error(0x0000, TAG, `Close file failed. Cause code: ${err.code}, message: ${err.message}`); + } + } + } +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets b/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets new file mode 100644 index 0000000000000000000000000000000000000000..39e8e340ccd472ac3e93ae74ae23068cc014b678 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { window } from '@kit.ArkUI'; +import type { BusinessError } from '@kit.BasicServicesKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { common, ConfigurationConstant } from '@kit.AbilityKit'; +import { CommonConstants } from '../constants/CommonConstants'; +import { BreakpointSystem } from './BreakpointSystem'; + +const TAG: string = '[WindowUtil]'; + +export class WindowUtil { + private static windowClass?: window.Window; + private static uiContext: UIContext; + + public static initialize(windowStage: window.WindowStage) { + try { + WindowUtil.windowClass = windowStage.getMainWindowSync(); + const uiContext: UIContext = WindowUtil.windowClass.getUIContext(); + WindowUtil.uiContext = uiContext; + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT, uiContext); + WindowUtil.registerBreakPoint(windowStage); + WindowUtil.requestFullScreen(windowStage); + } catch (err) { + hilog.error(0x0000, TAG, `WindowUtil initialize failed. Cause: ${err.code} ${err.message}`); + } + } + + public static requestFullScreen(windowStage: window.WindowStage): void { + windowStage.getMainWindow((err: BusinessError, data: window.Window) => { + if (err.code) { + return; + } + const windowClass: window.Window = data; + // Realize the immersive effect. + try { + const promise: Promise = windowClass.setWindowLayoutFullScreen(true); + promise.then(() => { + hilog.info(0x0000, TAG, 'Succeeded in setting the window layout to full-screen mode.'); + }).catch((err: BusinessError) => { + hilog.error(0x0000, TAG, + `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); + }); + } catch { + hilog.error(0x0000, TAG, 'Failed to set the window layout to full-screen mode.'); + } + }); + } + + public static registerBreakPoint(windowStage: window.WindowStage) { + windowStage.getMainWindow((err: BusinessError, data: window.Window) => { + if (err.code) { + hilog.error(0x0000, TAG, `Failed to get main window: ${err.message}`); + return; + } + BreakpointSystem.getInstance().updateBp(); + let avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_NAVIGATOR_BAR_HEIGHT, + WindowUtil.uiContext.px2vp(avoidArea.bottomRect.height)); + avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM); + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_STATUS_BAR_HEIGHT, + WindowUtil.uiContext.px2vp(avoidArea.topRect.height)); + data.on('windowSizeChange', () => BreakpointSystem.getInstance().updateBp()); + data.on('avoidAreaChange', (avoidAreaOption) => { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + if (avoidAreaOption.type === window.AvoidAreaType.TYPE_SYSTEM || + avoidAreaOption.type === window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR) { + WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); + } + }); + }) + } + + // Get status bar height and indicator height. + public static setAvoidArea(type: window.AvoidAreaType, area: window.AvoidArea) { + if (type === window.AvoidAreaType.TYPE_SYSTEM) { + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_STATUS_BAR_HEIGHT, + WindowUtil.uiContext.px2vp(area.topRect.height)); + } else { + AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_NAVIGATOR_BAR_HEIGHT, + WindowUtil.uiContext.px2vp(area.bottomRect.height)); + } + } + + public static updatedColorMode(context: common.UIAbilityContext): void { + context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + } +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/module.json5 b/sheetrecognitionlibrary/src/main/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..55fecf7f8155cddf1c654fa8808da807c822ef5e --- /dev/null +++ b/sheetrecognitionlibrary/src/main/module.json5 @@ -0,0 +1,11 @@ +{ + "module": { + "name": "sheetrecognitionlibrary", + "type": "har", + "deviceTypes": [ + "default", + "tablet", + "2in1" + ] + } +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/resources/base/element/float.json b/sheetrecognitionlibrary/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..77a5f52a1b8e44fc844710ce0a6136632b9bdeb5 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/resources/base/element/float.json @@ -0,0 +1,20 @@ +{ + "float": [ + { + "name": "page_text_font_size", + "value": "50fp" + }, + { + "name": "margin_size_SM", + "value": "16vp" + }, + { + "name": "margin_size_MD", + "value": "24vp" + }, + { + "name": "margin_size_LG", + "value": "32vp" + } + ] +} diff --git a/sheetrecognitionlibrary/src/main/resources/base/element/string.json b/sheetrecognitionlibrary/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..3de9fdf4e5120e62b6df70ef9d0b46d2918e748b --- /dev/null +++ b/sheetrecognitionlibrary/src/main/resources/base/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "btn_recognize_title", + "value": "Sheet Recognition" + }, + { + "name": "title", + "value": "Sheet recognition" + }, + { + "name": "save_success", + "value": "The file is saved successfully" + }, + { + "name": "save_error", + "value": "The file is saved error" + } + ] +} diff --git a/sheetrecognitionlibrary/src/main/resources/en_US/element/string.json b/sheetrecognitionlibrary/src/main/resources/en_US/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..aac56763a770ad3f387a39dba63c959b486254e5 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/resources/en_US/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "btn_recognize_title", + "value": "Sheet Recognition" + }, + { + "name": "title", + "value": "Sheet recognition" + }, + { + "name": "save_success", + "value": "The file is saved successfully" + }, + { + "name": "save_error", + "value": "The file is saved error" + } + ] +} \ No newline at end of file diff --git a/sheetrecognitionlibrary/src/main/resources/zh_CN/element/string.json b/sheetrecognitionlibrary/src/main/resources/zh_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..4622eb1e55aea59487507b6c17d49f122a3ea116 --- /dev/null +++ b/sheetrecognitionlibrary/src/main/resources/zh_CN/element/string.json @@ -0,0 +1,20 @@ +{ + "string": [ + { + "name": "btn_recognize_title", + "value": "表格识别" + }, + { + "name": "title", + "value": "表格识别" + }, + { + "name": "save_success", + "value": "文件保存成功" + }, + { + "name": "save_error", + "value": "文件保存失败" + } + ] +} \ No newline at end of file diff --git a/entry/build-profile.json5 b/sheetrecognitionsample/build-profile.json5 similarity index 100% rename from entry/build-profile.json5 rename to sheetrecognitionsample/build-profile.json5 diff --git a/entry/hvigorfile.ts b/sheetrecognitionsample/hvigorfile.ts similarity index 100% rename from entry/hvigorfile.ts rename to sheetrecognitionsample/hvigorfile.ts diff --git a/entry/obfuscation-rules.txt b/sheetrecognitionsample/obfuscation-rules.txt similarity index 79% rename from entry/obfuscation-rules.txt rename to sheetrecognitionsample/obfuscation-rules.txt index 985b2aeb7658286b17bd26eab8f217c3fe75ea8b..b8fb986ac3cd9b1fbd902bcb19b7face83ba1b52 100644 --- a/entry/obfuscation-rules.txt +++ b/sheetrecognitionsample/obfuscation-rules.txt @@ -2,7 +2,7 @@ # You can include the obfuscation configuration files in the current module's build-profile.json5. # # For more details, see -# https://gitee.com/openharmony/arkcompiler_ets_frontend/blob/master/arkguard/README.md +# https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/source-obfuscation # Obfuscation options: # -disable-obfuscation: disable all obfuscations @@ -15,4 +15,9 @@ # Keep options: # -keep-property-name: specifies property names that you want to keep -# -keep-global-name: specifies names that you want to keep in the global scope \ No newline at end of file +# -keep-global-name: specifies names that you want to keep in the global scope + +-enable-property-obfuscation +-enable-toplevel-obfuscation +-enable-filename-obfuscation +-enable-export-ob \ No newline at end of file diff --git a/sheetrecognitionsample/oh-package.json5 b/sheetrecognitionsample/oh-package.json5 new file mode 100644 index 0000000000000000000000000000000000000000..d8bee4c7c101e4aef8430af8726f8fb857fa3723 --- /dev/null +++ b/sheetrecognitionsample/oh-package.json5 @@ -0,0 +1,13 @@ +{ + "license": "", + "devDependencies": {}, + "author": "", + "name": "sheetrecognitionsample", + "description": "Please describe the basic information.", + "main": "", + "version": "1.0.0", + "dynamicDependencies": {}, + "dependencies": { + "@ohos_samples/textRecognitionlibrary": "file:../sheetrecognitionlibrary" + } +} \ No newline at end of file diff --git a/entry/src/main/ets/entryability/EntryAbility.ets b/sheetrecognitionsample/src/main/ets/entryability/EntryAbility.ets similarity index 87% rename from entry/src/main/ets/entryability/EntryAbility.ets rename to sheetrecognitionsample/src/main/ets/entryability/EntryAbility.ets index 7b14e502f81e2b8f8488298afe3bc38a87472aae..9791fd3698c1d4e92fb290dc7e7e0f5cea4064b8 100644 --- a/entry/src/main/ets/entryability/EntryAbility.ets +++ b/sheetrecognitionsample/src/main/ets/entryability/EntryAbility.ets @@ -5,10 +5,12 @@ import { UIAbility } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; import { window } from '@kit.ArkUI'; +import { SheetRecognitionController } from '@ohos_samples/textRecognitionlibrary'; export default class EntryAbility extends UIAbility { onCreate(): void { hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + SheetRecognitionController.updatedColorMode(this.context); } onDestroy(): void { @@ -24,6 +26,7 @@ export default class EntryAbility extends UIAbility { hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); return; } + SheetRecognitionController.initWindowConfig(windowStage); hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.'); }); } diff --git a/sheetrecognitionsample/src/main/ets/pages/Index.ets b/sheetrecognitionsample/src/main/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..79751d1f55b63f2bd146d00eded103176fd47acc --- /dev/null +++ b/sheetrecognitionsample/src/main/ets/pages/Index.ets @@ -0,0 +1,16 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + */ + +import { MainComponent } from '@ohos_samples/textRecognitionlibrary' + +@Entry +@Component +struct Index { + + build() { + Stack() { + MainComponent() + } + } +} diff --git a/entry/src/main/module.json5 b/sheetrecognitionsample/src/main/module.json5 similarity index 88% rename from entry/src/main/module.json5 rename to sheetrecognitionsample/src/main/module.json5 index 33e91e467daf8c14d97fc4928ad927bf77f074e8..788a3e89f00e9414456b0594534ca89d6e5386ab 100644 --- a/entry/src/main/module.json5 +++ b/sheetrecognitionsample/src/main/module.json5 @@ -1,19 +1,20 @@ { "module": { - "name": "entry", + "name": "sheetrecognitionsample", "type": "entry", "description": "$string:module_desc", "mainElement": "EntryAbility", "deviceTypes": [ "phone", - "tablet" + "tablet", + "2in1" ], "deliveryWithInstall": true, "installationFree": false, "pages": "$profile:main_pages", "abilities": [ { - "name": "EntryAbility", + "name": "SheetRecognitionAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:layered_image", diff --git a/entry/src/main/resources/base/element/color.json b/sheetrecognitionsample/src/main/resources/base/element/color.json similarity index 100% rename from entry/src/main/resources/base/element/color.json rename to sheetrecognitionsample/src/main/resources/base/element/color.json diff --git a/entry/src/main/resources/base/element/string.json b/sheetrecognitionsample/src/main/resources/base/element/string.json similarity index 100% rename from entry/src/main/resources/base/element/string.json rename to sheetrecognitionsample/src/main/resources/base/element/string.json diff --git a/entry/src/main/resources/base/media/background.png b/sheetrecognitionsample/src/main/resources/base/media/background.png similarity index 100% rename from entry/src/main/resources/base/media/background.png rename to sheetrecognitionsample/src/main/resources/base/media/background.png diff --git a/entry/src/main/resources/base/media/foreground.png b/sheetrecognitionsample/src/main/resources/base/media/foreground.png similarity index 100% rename from entry/src/main/resources/base/media/foreground.png rename to sheetrecognitionsample/src/main/resources/base/media/foreground.png diff --git a/entry/src/main/resources/base/media/layered_image.json b/sheetrecognitionsample/src/main/resources/base/media/layered_image.json similarity index 100% rename from entry/src/main/resources/base/media/layered_image.json rename to sheetrecognitionsample/src/main/resources/base/media/layered_image.json diff --git a/entry/src/main/resources/base/media/startIcon.png b/sheetrecognitionsample/src/main/resources/base/media/startIcon.png similarity index 100% rename from entry/src/main/resources/base/media/startIcon.png rename to sheetrecognitionsample/src/main/resources/base/media/startIcon.png diff --git a/entry/src/main/resources/base/profile/main_pages.json b/sheetrecognitionsample/src/main/resources/base/profile/main_pages.json similarity index 100% rename from entry/src/main/resources/base/profile/main_pages.json rename to sheetrecognitionsample/src/main/resources/base/profile/main_pages.json diff --git a/entry/src/main/resources/en_US/element/string.json b/sheetrecognitionsample/src/main/resources/en_US/element/string.json similarity index 100% rename from entry/src/main/resources/en_US/element/string.json rename to sheetrecognitionsample/src/main/resources/en_US/element/string.json diff --git a/entry/src/main/resources/zh_CN/element/string.json b/sheetrecognitionsample/src/main/resources/zh_CN/element/string.json similarity index 100% rename from entry/src/main/resources/zh_CN/element/string.json rename to sheetrecognitionsample/src/main/resources/zh_CN/element/string.json