diff --git a/textrecognitionlibrary/src/main/ets/components/MainComponent.ets b/textrecognitionlibrary/src/main/ets/components/MainComponent.ets index c8a14df1789d1bb2521a6d5e42c06da7c83f4073..37781b23b7cfb8a855937292b948c82be9511525 100644 --- a/textrecognitionlibrary/src/main/ets/components/MainComponent.ets +++ b/textrecognitionlibrary/src/main/ets/components/MainComponent.ets @@ -7,6 +7,7 @@ import { CommonConstants } from '../constants/CommonConstants'; import { BreakpointType } from '../utils/BreakpointSystem'; import { TextRecognitionComponent } from './TextRecognitionComponent'; import Logger from '../utils/Logger'; +import { WindowUtil } from '../utils/WindowUtil'; @Component export struct MainComponent { @@ -29,6 +30,7 @@ export struct MainComponent { async aboutToDisappear(): Promise { await textRecognition.release(); + WindowUtil.unRegisterBreakPoint(); Logger.info('OCRDemo', 'OCR service released successfully'); } @@ -37,7 +39,7 @@ export struct MainComponent { 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 : + .width(this.currentBp === WidthBreakpoint.WIDTH_SM ? CommonConstants.FULL_PERCENT : CommonConstants.BTN_MAX_WIDTH) .height(CommonConstants.BTN_HEIGHT) .onClick(() => { @@ -75,18 +77,23 @@ export struct MainComponent { } private async selectImage() { - let uri = await this.openPhoto(); - if (uri === undefined) { - Logger.error('OCRDemo', 'Failed to get uri.'); - return; + try { + let uri = await this.openPhoto(); + if (uri === undefined) { + Logger.error('OCRDemo', 'Failed to get uri.'); + return; + } + this.pathStack.pushPath({ + name: 'textRecognition', + param: uri + }); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error('OCRDemo', `Failed to get photo image uri. code:${err.code},message:${err.message}`); } - this.pathStack.pushPath({ - name: 'textRecognition', - param: uri - }); } - private openPhoto(): Promise { + private openPhoto(): Promise { return new Promise((resolve, reject) => { let photoPicker: photoAccessHelper.PhotoViewPicker = new photoAccessHelper.PhotoViewPicker(); photoPicker.select({ @@ -95,8 +102,7 @@ export struct MainComponent { }).then((res: photoAccessHelper.PhotoSelectResult) => { resolve(res.photoUris[0]); }).catch((err: BusinessError) => { - Logger.error('OCRDemo', `Failed to get photo image uri. code:${err.code},message:${err.message}`); - reject(''); + reject(err); }) }) } diff --git a/textrecognitionlibrary/src/main/ets/components/TextRecognitionComponent.ets b/textrecognitionlibrary/src/main/ets/components/TextRecognitionComponent.ets index 5ff92045513271381f7dea27ae209a81f18e6293..f01adcf2d8b3a85f0b7c202c8b72ba7d06509daf 100644 --- a/textrecognitionlibrary/src/main/ets/components/TextRecognitionComponent.ets +++ b/textrecognitionlibrary/src/main/ets/components/TextRecognitionComponent.ets @@ -19,6 +19,8 @@ import { fileIo } from '@kit.CoreFileKit'; import { CommonConstants } from '../constants/CommonConstants'; import Logger from '../utils/Logger'; +const TAG: string = '[OCRDemo]'; + @Component export struct TextRecognitionComponent { @State cardDataSource: Record[] = []; @@ -26,21 +28,29 @@ export struct TextRecognitionComponent { @StorageProp(CommonConstants.KEY_PREFIX_BREAKPOINT) currentBp: WidthBreakpoint = WidthBreakpoint.WIDTH_SM; @State isPresent: boolean = false; @State sdkApiVersion: number = 0; - private imageSource: image.ImageSource | undefined = undefined; @State chooseImage: PixelMap | undefined = undefined; @State dataValues: string = ''; + private imageSource: image.ImageSource | undefined = undefined; private pageParam: string = ''; async aboutToAppear(): Promise { - const initResult = await textRecognition.init(); - Logger.info('OCRDemo', `OCR service initialization result:${this.currentBp}`); - this.loadImage(this.pageParam); + try { + await textRecognition.init(); + this.loadImage(this.pageParam); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `Failed to load image. code:${err.code},message:${err.message}`); + } } aboutToDisappear(): void { if (this.chooseImage && this.imageSource) { - this.chooseImage.release(); - this.imageSource.release(); + this.chooseImage.release().catch((error: BusinessError) => { + Logger.error(TAG, `ChooseImage release failed. code:${error.code},message:${error.message}`); + }); + this.imageSource.release().catch((error: BusinessError) => { + Logger.error(TAG, `ImageSource release failed. code:${error.code},message:${error.message}`); + }); } } @@ -57,38 +67,6 @@ export struct TextRecognitionComponent { }) } - private loadImage(name: string) { - setTimeout(async () => { - let fileSource = await fileIo.open(name, fileIo.OpenMode.READ_ONLY); - this.imageSource = image.createImageSource(fileSource.fd); - this.chooseImage = await this.imageSource.createPixelMap(); - }, CommonConstants.DURATION) - } - - private async textRecognitionTest() { - if (!this.chooseImage) { - return; - } - - if (this.dataValues.length > 0) { - this.isPresent = !this.isPresent; - return; - } - - let visionInfo: textRecognition.VisionInfo = { - pixelMap: this.chooseImage - }; - let textConfiguration: textRecognition.TextRecognitionConfiguration = { - isDirectionDetectionSupported: false - }; - // Call the OCR API. - let recognitionResult = await textRecognition.recognizeText(visionInfo, textConfiguration); - let recognitionString = recognitionResult.value; - this.dataValues = recognitionString; - this.isPresent = !this.isPresent; - Logger.info('OCRDemo', `Succeeded in recognizing text:${recognitionString}`); - } - build() { NavDestination() { Column() { @@ -107,7 +85,7 @@ export struct TextRecognitionComponent { Stack() { Button($r('app.string.btn_recognize_result'), { stateEffect: true, type: ButtonType.Capsule }) .width(this.currentBp == WidthBreakpoint.WIDTH_SM ? CommonConstants.FULL_PERCENT : - CommonConstants.BTN_MAX_WIDTH) + CommonConstants.BTN_MAX_WIDTH) .height(CommonConstants.BTN_HEIGHT) .onClick(() => { this.textRecognitionTest(); @@ -118,9 +96,9 @@ export struct TextRecognitionComponent { .padding({ top: 16, right: this.currentBp === WidthBreakpoint.WIDTH_SM ? $r('sys.float.padding_level8') : - $r('sys.float.padding_level0'), + $r('sys.float.padding_level0'), left: this.currentBp === WidthBreakpoint.WIDTH_SM ? $r('sys.float.padding_level8') : - $r('sys.float.padding_level0'), + $r('sys.float.padding_level0'), bottom: CommonConstants.BOTTOM_MARGIN }) } @@ -143,4 +121,49 @@ export struct TextRecognitionComponent { this.pageParam = context.pathInfo.param as string; }) } + + private loadImage(name: string) { + setTimeout(async () => { + let fileSource: fileIo.File = null!; + try { + fileSource = await fileIo.open(name, fileIo.OpenMode.READ_ONLY); + this.imageSource = image.createImageSource(fileSource.fd); + this.chooseImage = await this.imageSource.createPixelMap(); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `FileIo open failed. code:${err.code},message:${err.message}`); + } finally { + fileSource && fileIo.closeSync(fileSource); + } + }, CommonConstants.DURATION) + } + + private async textRecognitionTest() { + if (!this.chooseImage) { + return; + } + + if (this.dataValues.length > 0) { + this.isPresent = !this.isPresent; + return; + } + + let visionInfo: textRecognition.VisionInfo = { + pixelMap: this.chooseImage + }; + let textConfiguration: textRecognition.TextRecognitionConfiguration = { + isDirectionDetectionSupported: false + }; + // Call the OCR API. + try { + let recognitionResult = await textRecognition.recognizeText(visionInfo, textConfiguration); + let recognitionString = recognitionResult.value; + this.dataValues = recognitionString; + this.isPresent = !this.isPresent; + Logger.info(TAG, `Succeeded in recognizing text:${recognitionString}`); + } catch (error) { + const err: BusinessError = error as BusinessError; + Logger.error(TAG, `TextRecognition recognizeText failed. code:${err.code},message:${err.message}`); + } + } } \ No newline at end of file diff --git a/textrecognitionlibrary/src/main/ets/utils/WindowUtil.ets b/textrecognitionlibrary/src/main/ets/utils/WindowUtil.ets index 20d5919bc9671636d78022c0783c8c4908491b89..d1be4ec03f8b9558f9557dd4a7d254638da9e860 100644 --- a/textrecognitionlibrary/src/main/ets/utils/WindowUtil.ets +++ b/textrecognitionlibrary/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.