diff --git a/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets b/CameraKit/entry/src/main/ets/pages/GetFrontCameraImage.ets index c7b693c9d21263cb2f491a7776c3ffa6b5e2d1ad..41a98e774608dd4bb234e3849bd71c0a9a2263d4 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 8cd8920bf67b994891cf4d9ac80ee6be0197e399..b31f264b0fb44e0c453fb0a3e128ebd31d9e9964 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