From 8d4b509b0a1ac658d722713c340e74ff0d14721f Mon Sep 17 00:00:00 2001 From: WangLin305 Date: Tue, 9 Sep 2025 20:49:30 +0800 Subject: [PATCH] catch throw error --- entry/src/main/ets/pages/Index.ets | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index f14114b..a756552 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -27,14 +27,20 @@ class CompressedImageInfo { imageByteLength: number = 0; } -async function compressedImage(sourcePixelMap: image.PixelMap, maxCompressedImageSize: number): Promise { +async function compressedImage(sourcePixelMap: image.PixelMap, + maxCompressedImageSize: number): Promise { const imagePackerApi = image.createImagePacker(); const IMAGE_QUALITY = 0; const packOpts: image.PackingOption = { format: "image/jpeg", quality: IMAGE_QUALITY }; - let compressedImageData: ArrayBuffer = await imagePackerApi.packToData(sourcePixelMap, packOpts); + let compressedImageData: ArrayBuffer = await imagePackerApi.packToData(sourcePixelMap, packOpts) + .catch((error: BusinessError) => { + hilog.error(0x0000, TAG, `releaseVideo catch error, code: ${error.code}, message: ${error.message}`); + return new ArrayBuffer(0); + }); const maxCompressedImageByte = maxCompressedImageSize * CommonConstants.BYTE_CONVERSION; if (maxCompressedImageByte > compressedImageData.byteLength) { - compressedImageData = await packingImage(compressedImageData, sourcePixelMap, IMAGE_QUALITY, maxCompressedImageByte); + compressedImageData = + await packingImage(compressedImageData, sourcePixelMap, IMAGE_QUALITY, maxCompressedImageByte); } else { let imageScale = 1; const REDUCE_SCALE = CommonConstants.REDUCE_SCALE; @@ -55,12 +61,15 @@ async function compressedImage(sourcePixelMap: image.PixelMap, maxCompressedImag async function packing(sourcePixelMap: image.PixelMap, imageQuality: number): Promise { const imagePackerApi = image.createImagePacker(); const packOpts: image.PackingOption = { format: "image/jpeg", quality: imageQuality }; - const data: ArrayBuffer = await imagePackerApi.packToData(sourcePixelMap, packOpts); + const data: ArrayBuffer = await imagePackerApi.packToData(sourcePixelMap, packOpts).catch((error: BusinessError) => { + hilog.error(0x0000, TAG, `releaseVideo catch error, code: ${error.code}, message: ${error.message}`); + return new ArrayBuffer(0); + }); return data; } async function packingImage(compressedImageData: ArrayBuffer, sourcePixelMap: image.PixelMap, - imageQuality: number, maxCompressedImageByte: number): Promise { + imageQuality: number, maxCompressedImageByte: number): Promise { const packingArray: number[] = []; const DICHOTOMY_ACCURACY = CommonConstants.DICHOTOMY_ACCURACY; for (let i = 0; i <= CommonConstants.PICTURE_QUALITY_MAX; i += DICHOTOMY_ACCURACY) { @@ -99,12 +108,12 @@ async function saveImage(compressedImageData: ArrayBuffer): Promise