• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16// 导入相关模块包
17// [Start pixelmap_import_image_module_in_encode]
18import { image } from '@kit.ImageKit';
19// [End pixelmap_import_image_module_in_encode]
20import { BusinessError } from '@kit.BasicServicesKit';
21import { fileIo as fs } from '@kit.CoreFileKit';
22import Logger from './Logger';
23
24const context: Context = getContext(this);
25// [Start pixelmap_create_image_packer]
26const imagePackerApi = image.createImagePacker();
27// [End pixelmap_create_image_packer]
28
29/**
30 *  图片编码,通过PixelMap将图片打包成文件流,再将文件流写入文件保存。
31 *
32 * @param pixelMap - 位图操作对象。
33 * @param isHdr - 是否为HDR格式。
34 **/
35export async function encodeToStreamByPixelMap(pixelMap: image.PixelMap, isHdr:boolean) {
36  const path: string = context.cacheDir + '/pixel_map.jpg';
37  let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
38  // [Start pixelmap_set_image_packer_option]
39  let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
40  // [End pixelmap_set_image_packer_option]
41  if(isHdr == true) {
42    // [Start pixelmap_hdr_content_encoding]
43    packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO;
44    // [End pixelmap_hdr_content_encoding]
45  }
46  // [Start pixelmap_encoding_through_pixelmap]
47  imagePackerApi.packToData(pixelMap, packOpts).then((data: ArrayBuffer) => {
48    // data 为打包获取到的文件流,写入文件保存即可得到一张图片
49    fs.write(file.fd, data);
50    Logger.info('Succeeded to pack the image and write to the file.');
51  }).catch((error: BusinessError) => {
52    Logger.error('Failed to pack the image. And the error is: ', String(error));
53  }).finally(()=>{
54    fs.closeSync(file.fd);
55  })
56  // [End pixelmap_encoding_through_pixelmap]
57}
58
59/**
60 *  图片编码,通过imageSource将图片打包成文件流,再将文件流写入文件保存。
61 *
62 * @param pixelMap - 位图操作对象。
63 * @param isHdr - 是否为HDR格式。
64 **/
65export async function encodeToStreamByImageSource(imageSource:image.ImageSource,isHdr:boolean){
66  let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
67  if(isHdr == true) {
68    packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO;
69  }
70  const path: string = context.cacheDir + '/pixel_map.jpg';
71  let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
72  // [Start pixelmap_encoding_through_image_source]
73  imagePackerApi.packToData(imageSource, packOpts).then( (data : ArrayBuffer) => {
74    // data 为打包获取到的文件流,写入文件保存即可得到一张图片
75    fs.write(file.fd, data);
76    Logger.info('Succeeded to pack the image and write to the file.');
77  }).catch((error : BusinessError) => {
78    Logger.error('Failed to pack the image. And the error is: ', String(error));
79  }).finally(()=>{
80    fs.closeSync(file.fd);
81  })
82  // [End pixelmap_encoding_through_image_source]
83}
84
85/**
86 *  图片编码,通过pixelMap将图片直接打包成文件保存。
87 *
88 * @param pixelMap - 位图操作对象。
89 * @param isHdr - 是否为HDR格式。
90 **/
91export async function encodeToFileByPixelMap(pixelMap: image.PixelMap,isHdr:boolean) {
92  let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
93  if(isHdr == true) {
94    packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO;
95  }
96  // [Start pixelmap_encoding_into_file_through_pixelmap]
97  const path : string = context.cacheDir + '/pixel_map.jpg';
98  let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
99  await imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {
100    // 直接打包进文件
101    Logger.info('Succeeded to pack the image and write to the file.');
102  }).catch((error : BusinessError) => {
103    Logger.error('Failed to pack the image. And the error is: ', String(error));
104  }).finally(()=>{
105    fs.closeSync(file.fd);
106  })
107  // [End pixelmap_encoding_into_file_through_pixelmap]
108}
109
110/**
111 *  图片编码,通过imageSource将图片直接打包成文件保存。
112 *
113 * @param pixelMap - 位图操作对象。
114 * @param isHdr - 是否为HDR格式。
115 **/
116export async function encodeToFileByImageSource(imageSource:image.ImageSource,isHdr:boolean) {
117  let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 };
118  if(isHdr == true) {
119    packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO;
120  }
121  // [Start pixelmap_encoding_into_file_through_image_source]
122  const filePath : string = context.cacheDir + '/image_source.jpg';
123  let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
124  imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => {
125    // 直接打包进文件
126    Logger.info('Succeeded to pack the image and write to the file.');
127  }).catch((error : BusinessError) => {
128    Logger.error('Failed to pack the image. And the error is: ', String(error));
129  }).finally(()=>{
130    fs.closeSync(file.fd);
131  })
132  // [End pixelmap_encoding_into_file_through_image_source]
133}