From 181d65e075f80c74fd2af043371aff6efec354b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=A2=A6=E9=BE=99?= Date: Fri, 5 Dec 2025 09:21:26 +0800 Subject: [PATCH] =?UTF-8?q?1.getContext=E5=BA=9F=E5=BC=83=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E6=94=B9=202.=E5=88=A0=E9=99=A4=E5=A4=9A?= =?UTF-8?q?=E4=BD=99=E6=9C=AA=E4=BD=BF=E7=94=A8=E7=9A=84=E5=8C=85=203.?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=89=8D=E7=BD=AE=E6=91=84=E5=83=8F=E5=A4=B4?= =?UTF-8?q?=E9=BB=91=E5=B1=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/ets/pages/GetFrontCameraImage.ets | 7 +++---- .../src/main/ets/pages/CompressedImage.ets | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets b/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets index c7b693c9..41a98e77 100644 --- a/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets +++ b/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets @@ -33,7 +33,6 @@ struct GetFrontCameraImage { // 1、Use the system camera framework camera module to obtain physical camera information. let cameraManager = camera.getCameraManager(context); let camerasInfo: Array = cameraManager.getSupportedCameras(); - let cameraDevice: camera.CameraDevice = camerasInfo[0]; // Detecting camera status cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => { console.log(`camera : ${cameraStatusInfo.camera.cameraId}`); @@ -41,11 +40,11 @@ struct GetFrontCameraImage { }); // 2、Create and start a physical camera input stream channel // Set as front camera camera.CameraPosition.CAMERA_POSITION_FRONT - let cameraInput = cameraManager.createCameraInput(camera.CameraPosition.CAMERA_POSITION_FRONT, - camera.CameraType.CAMERA_TYPE_DEFAULT); + let fontCamera = camerasInfo.find(cam => cam.cameraPosition === camera.CameraPosition.CAMERA_POSITION_FRONT); + let cameraInput = cameraManager.createCameraInput(fontCamera); await cameraInput.open(); // 3、Retrieve the physical camera information and query the output formats supported by the preview stream of the camera. Create a preview output channel by combining it with the surfaceId provided by XComponent - let outputCapability = cameraManager.getSupportedOutputCapability(cameraDevice, camera.SceneMode.NORMAL_PHOTO); + let outputCapability = cameraManager.getSupportedOutputCapability(fontCamera, camera.SceneMode.NORMAL_PHOTO); let previewProfile = outputCapability.previewProfiles[0]; let surfaceId = this.xComponentController.getXComponentSurfaceId(); let previewOutput = cameraManager.createPreviewOutput(previewProfile, surfaceId); diff --git a/ImageKit/entry/src/main/ets/pages/CompressedImage.ets b/ImageKit/entry/src/main/ets/pages/CompressedImage.ets index 8cd8920b..b31f264b 100644 --- a/ImageKit/entry/src/main/ets/pages/CompressedImage.ets +++ b/ImageKit/entry/src/main/ets/pages/CompressedImage.ets @@ -19,10 +19,8 @@ // [Start CompressedImage] import { image } from '@kit.ImageKit'; -import { resourceManager } from '@kit.LocalizationKit'; -import { fileUri } from '@kit.CoreFileKit'; -import { BusinessError } from '@kit.BasicServicesKit'; -import { fileIo} from '@kit.CoreFileKit'; +import { fileIo } from '@kit.CoreFileKit'; +import { common } from '@kit.AbilityKit'; class CompressedImageInfo { imageUri: string = ""; // URI of compressed image storage location @@ -35,14 +33,15 @@ class CompressedImageInfo { * @param maxCompressedImageSize:Specify the compression target size for the image, in kb * @returns compressedImageInfo:Return the final compressed image information */ -async function compressedImage(sourcePixelMap: image.PixelMap, maxCompressedImageSize: number): Promise { +async function compressedImage(sourcePixelMap: image.PixelMap, + maxCompressedImageSize: number): Promise { // Create an ImagePacker object for image encoding const imagePackerApi = image.createImagePacker(); const IMAGE_QUALITY = 80; const packOpts: image.PackingOption = { format: "image/jpeg", quality: IMAGE_QUALITY }; // Encode through PixelMap. Compressed ImageData is the image file stream obtained by packaging. let compressedImageData: ArrayBuffer = await imagePackerApi.packToData(sourcePixelMap, packOpts); - // 压缩目标图像字节长度 + const maxCompressedImageByte = maxCompressedImageSize * 1024; // Image compression. First, determine whether the minimum byte size of the image that can be compressed by packToData meets the specified image compression size when setting the image quality parameter quality to 80. // If satisfied, use the packToData method to binary search for the quality closest to the specified image compression target size to compress the image. @@ -51,7 +50,8 @@ async function compressedImage(sourcePixelMap: image.PixelMap, maxCompressedImag // and finally find the compressed image data with the closest scaling factor to the specified image compression target size. if (maxCompressedImageByte > compressedImageData.byteLength) { // Using packToData binary compression to obtain image file streams - compressedImageData = await packingImage(compressedImageData, sourcePixelMap, IMAGE_QUALITY, maxCompressedImageByte); + compressedImageData = + await packingImage(compressedImageData, sourcePixelMap, IMAGE_QUALITY, maxCompressedImageByte); } else { // Use scale to scale the image first, use a while loop to decrease the scale by 0.4 times each time, then use packToData (with the image quality parameter quality set to 80) to obtain the compressed image size, // and finally find the compressed image data with the closest scaling factor to the specified image compression target size @@ -98,7 +98,8 @@ async function packToData(sourcePixelMap: image.PixelMap, imageQuality: number): * @param maxCompressedImageByte:Compress the byte length of the target image * @returns compressedImageData:Return the compressed image data after binary packToData */ -async function packingImage(compressedImageData: ArrayBuffer, sourcePixelMap: image.PixelMap, imageQuality: number, maxCompressedImageByte: number): Promise { +async function packingImage(compressedImageData: ArrayBuffer, sourcePixelMap: image.PixelMap, imageQuality: number, + maxCompressedImageByte: number): Promise { // The range of image quality parameters is 80-100. Here, an array for packToData binary image quality parameters is created with 5 as the minimum binary unit. const packingArray: number[] = []; const DICHOTOMY_ACCURACY = 5; @@ -145,7 +146,8 @@ async function packingImage(compressedImageData: ArrayBuffer, sourcePixelMap: im * @returns compressedImageInfo:Return compressed image information */ async function saveImage(compressedImageData: ArrayBuffer): Promise { - const context: Context = getContext(); + // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext + const context: Context = this.getUIContext().getHostContext() as common.UIAbilityContext; // Define the compressed image URI to be saved. AfterCompression.jpeg represents the compressed image. const compressedImageUri: string = context.filesDir + '/' + 'afterCompression.jpeg'; try { @@ -168,5 +170,6 @@ async function saveImage(compressedImageData: ArrayBuffer): Promise