From 93fd43b0ca63fd7bb155f4f845eb9b3a8131d337 Mon Sep 17 00:00:00 2001 From: d30044417 Date: Fri, 19 Dec 2025 14:39:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20SheetExtraction=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=8D=95=E6=8D=89&&=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/SheetExtractionComponent.ets | 37 +++++++++++-------- .../src/main/ets/utils/FileUtil.ets | 6 +-- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets index d6e95aa..1363bf2 100644 --- a/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets +++ b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets @@ -3,8 +3,7 @@ */ import { DocType, DocumentScanner, SaveOption } from '@kit.VisionKit'; -import { picker } from '@kit.CoreFileKit'; -import { fileIo as fs } from '@kit.CoreFileKit'; +import { fileIo as fs, picker } from '@kit.CoreFileKit'; import { FileUtil } from '../utils/FileUtil'; import { CommonConstants } from '../constants/CommonConstants'; import Logger from '../utils/Logger'; @@ -18,31 +17,34 @@ export struct SheetExtractionComponent { build() { Stack({ alignContent: Alignment.Top }) { - //The document scanning control is embedded in the blank page. - //The control provides the table extraction capability. + // The document scanning control is embedded in the blank page. + // The control provides the table extraction capability. DocumentScanner({ scannerConfig: { - //The DocType.SHEET attribute needs to be configured to support table extraction. + // The DocType.SHEET attribute needs to be configured to support table extraction. 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. + // 200 indicates success, SaveOption.EXCEL indicates that the Excel file is returned. + // and uris indicates the file path. if (code === 200) { if (uris.length > 0) { if (saveType == SaveOption.EXCEL) { let res = FileUtil.accessFileSync(uris[0]); if (!res) { - this.saveToExcel(uris[0], saveType); + this.saveToExcel(uris[0], saveType).catch((error: string) => { + Logger.error(TAG, `SaveToExcel error message: ${error}`); + }); } } - if (saveType == SaveOption.JPG) { let res = FileUtil.accessFileSync(uris[0]); if (!res) { - this.saveToExcel(uris[0], saveType); + this.saveToExcel(uris[0], saveType).catch((error: string) => { + Logger.error(TAG, `SaveToExcel error message: ${error}`); + }); } } } @@ -84,17 +86,22 @@ export struct SheetExtractionComponent { message: $r('app.string.save_success'), alignment: Alignment.Center }); - return resolve(fileUri); + resolve(fileUri); } catch (error) { + const err: BusinessError = error as BusinessError; this.getUIContext().getPromptAction().showToast({ message: $r('app.string.save_error'), alignment: Alignment.Center }); - Logger.error(TAG, `error: ${error}`); - reject(''); + reject(err.message); } finally { - fs.closeSync(originFile); - fs.closeSync(targetFile); + try { + originFile && fs.closeSync(originFile); + targetFile && fs.closeSync(targetFile); + } catch (error) { + const err = error as BusinessError; + Logger.error(TAG, `Close file failed. Cause code: ${err.code}, message: ${err.message}`); + } } }) }) diff --git a/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets b/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets index 50b58d1..dae7732 100644 --- a/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets +++ b/sheetrecognitionlibrary/src/main/ets/utils/FileUtil.ets @@ -82,11 +82,11 @@ export class FileUtil { } } catch (error) { const err = error as BusinessError; - Logger.error(TAG, `Write file failed. Cause code: ${err.code}, message: ${err.message}`); + Logger.error(TAG, `Read or write file failed. Cause code: ${err.code}, message: ${err.message}`); } finally { try { - fs.close(srcFile); - fs.close(destFile); + srcFile && fs.closeSync(srcFile); + destFile && fs.closeSync(destFile); } catch (error) { const err = error as BusinessError; Logger.error(TAG, `Close file failed. Cause code: ${err.code}, message: ${err.message}`); -- Gitee From cc7df124e788fee87796bfaf7c247a14cc97d490 Mon Sep 17 00:00:00 2001 From: d30044417 Date: Mon, 22 Dec 2025 10:47:25 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20WindowUtil=E5=8F=96=E6=B6=88?= =?UTF-8?q?=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/components/MainComponent.ets | 5 ++ .../src/main/ets/utils/WindowUtil.ets | 60 ++++++++++--------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets b/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets index 1e0f277..cce0e03 100644 --- a/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets +++ b/sheetrecognitionlibrary/src/main/ets/components/MainComponent.ets @@ -5,6 +5,7 @@ import { LengthMetrics } from '@kit.ArkUI'; import { CommonConstants } from '../constants/CommonConstants'; import { BreakpointType } from '../utils/BreakpointSystem'; +import { WindowUtil } from '../utils/WindowUtil'; import { SheetExtractionComponent } from './SheetExtractionComponent'; @Component @@ -21,6 +22,10 @@ export struct MainComponent { } } + aboutToDisappear(): void { + WindowUtil.unRegisterBreakPoint(); + } + build() { Column() { Navigation(this.pathStack) { diff --git a/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets b/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets index 7b87de9..93628f0 100644 --- a/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets +++ b/sheetrecognitionlibrary/src/main/ets/utils/WindowUtil.ets @@ -32,40 +32,30 @@ export class WindowUtil { const uiContext: UIContext = WindowUtil.windowClass.getUIContext(); WindowUtil.uiContext = uiContext; AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_SYSTEM_UICONTEXT, uiContext); - WindowUtil.registerBreakPoint(windowStage); - WindowUtil.requestFullScreen(windowStage); + WindowUtil.registerBreakPoint(WindowUtil.windowClass); + WindowUtil.requestFullScreen(WindowUtil.windowClass); } catch (err) { Logger.error(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(() => { - Logger.info(TAG, 'Succeeded in setting the window layout to full-screen mode.'); - }).catch((err: BusinessError) => { - Logger.error(TAG, - `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); - }); - } catch { - Logger.error(TAG, 'Failed to set the window layout to full-screen mode.'); - } - }); + public static requestFullScreen(windowClass: window.Window): void { + // Realize the immersive effect. + try { + const promise: Promise = windowClass.setWindowLayoutFullScreen(true); + promise.then(() => { + Logger.info(TAG, 'Succeeded in setting the window layout to full-screen mode.'); + }).catch((err: BusinessError) => { + Logger.error(TAG, + `Failed to set the window layout to full-screen mode. Cause: ${err.code}, ${err.message}`); + }); + } catch { + Logger.error(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) { - Logger.error(TAG, `Failed to get main window: ${err.message}`); - return; - } + public static registerBreakPoint(data: window.Window) { + try { BreakpointSystem.getInstance().updateBp(); let avoidArea = data.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR); AppStorage.setOrCreate(CommonConstants.KEY_PREFIX_NAVIGATOR_BAR_HEIGHT, @@ -81,7 +71,21 @@ export class WindowUtil { WindowUtil.setAvoidArea(avoidAreaOption.type, avoidAreaOption.area); } }); - }) + } catch (e) { + const error = e as BusinessError; + Logger.error(TAG, + `Register avoidAreaChange or windowSizeChange failed. code: ${error.code}, message: ${error.message}`); + } + } + + public static unRegisterBreakPoint() { + try { + WindowUtil.windowClass?.off('windowSizeChange'); + WindowUtil.windowClass?.off('avoidAreaChange'); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `windowClass off failed. code: ${err.code}, message: ${err.message}`); + } } // Get status bar height and indicator height. -- Gitee From 32f26ba87d0b5d7106d4604a948a757148c9de96 Mon Sep 17 00:00:00 2001 From: d30044417 Date: Sun, 4 Jan 2026 10:54:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E4=BA=8C=E8=BD=AE=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E8=A7=84=E8=8C=83=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/ets/components/SheetExtractionComponent.ets | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets index 1363bf2..456cfed 100644 --- a/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets +++ b/sheetrecognitionlibrary/src/main/ets/components/SheetExtractionComponent.ets @@ -74,6 +74,9 @@ export struct SheetExtractionComponent { if (type === SaveOption.EXCEL) { documentSaveOptions.fileSuffixChoices = ['.xlsx']; } + if (!this.getUIContext().getHostContext()) { + return ''; + } let documentViewPicker = new picker.DocumentViewPicker(this.getUIContext().getHostContext()!); let saveUri = new Promise(async (resolve, reject) => { documentViewPicker.save(documentSaveOptions).then((saveResult: Array) => { -- Gitee