From 1eccc167ccce88b9342fb569ae4935b8ebf46b84 Mon Sep 17 00:00:00 2001 From: yangfan Date: Mon, 13 Nov 2023 14:38:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?ImagePacker=E6=8E=A5=E5=8F=A3=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B0=86=E7=BC=96=E7=A0=81=E5=90=8E=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=86=99=E5=85=A5=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangfan --- zh-cn/application-dev/media/image-encoding.md | 35 +++- .../reference/apis/js-apis-image.md | 153 ++++++++++++++++++ 2 files changed, 187 insertions(+), 1 deletion(-) diff --git a/zh-cn/application-dev/media/image-encoding.md b/zh-cn/application-dev/media/image-encoding.md index 8f722b582d9..eca60f8987a 100644 --- a/zh-cn/application-dev/media/image-encoding.md +++ b/zh-cn/application-dev/media/image-encoding.md @@ -7,7 +7,7 @@ 图片编码相关API的详细介绍请参见:[图片编码接口说明](../reference/apis/js-apis-image.md#imagepacker)。 1. 创建图像编码ImagePacker对象。 - + ```ts // 导入相关模块包 import image from '@ohos.multimedia.image'; @@ -48,3 +48,36 @@ console.error('Failed to pack the image. And the error is: ' + error); }) ``` + +5. 图片编码进文件,文件保存在内存中。 + + 方法一:通过PixelMap编码进文件。 + + ```ts + import {BusinessError} from '@ohos.base' + import fs from '@ohos.file.fs' + import featureAbility from '@ohos.ability.featureAbility' + const path : string = context.getCacheDir() + "pixel_map.jpg"; + let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); + imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => { + // 直接打包进文件 + }).catch((error : BusinessError) => { + console.error('Failed to pack the image. And the error is: ' + error); + }) + ``` + + 方法二:通过imageSource编码进文件。 + + ```ts + import {BusinessError} from '@ohos.base' + import fs from '@ohos.file.fs' + import featureAbility from '@ohos.ability.featureAbility' + const context : _Context = featureAbility.getContext(); + const filePath : string = context.getCacheDir() + "/image_source.jpg"; + let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); + imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => { + // 直接打包进文件 + }).catch((error : BusinessError) => { + console.error('Failed to pack the image. And the error is: ' + error); + }) + ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-image.md b/zh-cn/application-dev/reference/apis/js-apis-image.md index 3fde61a6b3a..7215461acd7 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-image.md +++ b/zh-cn/application-dev/reference/apis/js-apis-image.md @@ -2463,6 +2463,159 @@ imagePackerApi.release().then(()=>{ }) ``` +### packToFile11+ + +packToFile(source: ImageSource, fd: number, option: PackingOption, callback: AsyncCallback\): void + +按给定格式编码进文件,使用callback形式返回结果。 + +**系统能力:** SystemCapability.Multimedia.Image.ImagePacker + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------- | ---- | ------------------------------ | +| source | [ImageSource](#imagesource) | 是 | 打包的图片源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | +| callback | AsyncCallback\ | 是 | 获取回调,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' +import featureAbility from '@ohos.ability.featureAbility' + +const context : _Context = featureAbility.getContext(); +const path : string = context.getCacheDir() + "/test.png"; +const imageSourceApi : image.ImageSource = image.createImageSource(path); +let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 }; +const filePath : string = context.getCacheDir() + "/image_source.jpg"; +let file = fs.openSync(filePath, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); +imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts, (err : BusinessError => {}) +``` + +### packToFile11+ + +packToFile (source: ImageSource, fd: number, option: PackingOption): Promise\ + +按给定格式编码进文件,使用promise形式返回结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------- | ---- | -------------- | +| source | [ImageSource](#imagesource) | 是 | 打包的图片源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | --------------------------------- | +| Promise\ | Promise实例,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' +import featureAbility from '@ohos.ability.featureAbility' + +const context : _Context = featureAbility.getContext(); +const path : string = context.getCacheDir() + "/test.png"; +const imageSourceApi : image.ImageSource = image.createImageSource(path); +let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 }; +const filePath : string = context.getCacheDir() + "/image_source.jpg"; +let file = fs.openSync(filePath, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); +imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts).then(()=>{ + console.log('Succeeded in packToFile.'); +}).catch((error : BusinessError)=>{ + console.log('Failed to packToFile.'); +}) +``` + +### packToFile11+ + +packToFile (source: Pixelmap, fd: number, option: PackingOption, callback: AsyncCallback\): void; + +按给定格式编码进文件,使用callback形式返回结果。 + +**系统能力:** SystemCapability.Multimedia.Image.ImagePacker + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| -------- | ------------------------------- | ---- | ------------------------------ | +| source | [PixelMap](#pixelmap7) | 是 | 打包的PixelMap资源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | +| callback | AsyncCallback\ | 是 | 获取回调,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' +import featureAbility from '@ohos.ability.featureAbility' + +const color : ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小,取值为:height * width *4 +let bufferArr : Uint8Array = new Uint8Array(color); +let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } } +image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { + let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } + const context : _Context = featureAbility.getContext(); + const path : string = context.getCacheDir() + "pixel_map.jpg"; + let file = fs.openSync(path, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); + imagePackerApi.packToFile(pixelmap, file.fd, packOpts, (err : BusinessError) => {}) +}) +``` + +### packToFile11+ + +packToFile (source: Pixelmap, fd: number, option: PackingOption): Promise\ + +按给定格式编码进文件,使用promise形式返回结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------- | ---- | -------------------- | +| source | [PixelMap](#pixelmap7) | 是 | 打包的PixelMap资源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | --------------------------------- | +| Promise\ | Promise实例,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' +import featureAbility from '@ohos.ability.featureAbility' + +const color : ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小,取值为:height * width *4 +let bufferArr : Uint8Array = new Uint8Array(color); +let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } } +image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { + let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } + const context : _Context = featureAbility.getContext(); + const path : string = context.getCacheDir() + "pixel_map.jpg"; + let file = fs.openSync(path, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); + imagePackerApi.packToFile(pixelmap, file.fd, packOpts) + .then(() => { + console.log('Succeeded in packToFile.'); + }).catch((error : BusinessError) => { + console.log('Failed to packToFile.'); + }) +}) +``` + ## image.createImageReceiver9+ createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver -- Gitee From 6023d179764196f733d56e621709e34919611fec Mon Sep 17 00:00:00 2001 From: yangfan Date: Mon, 13 Nov 2023 20:16:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=88=A0=E9=99=A4|=E5=89=8D=E5=90=8E?= =?UTF-8?q?=E5=A4=9A=E4=BD=99=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangfan --- zh-cn/application-dev/reference/apis/js-apis-image.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/zh-cn/application-dev/reference/apis/js-apis-image.md b/zh-cn/application-dev/reference/apis/js-apis-image.md index 7215461acd7..fd90f59c54f 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-image.md +++ b/zh-cn/application-dev/reference/apis/js-apis-image.md @@ -2492,7 +2492,7 @@ const path : string = context.getCacheDir() + "/test.png"; const imageSourceApi : image.ImageSource = image.createImageSource(path); let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 }; const filePath : string = context.getCacheDir() + "/image_source.jpg"; -let file = fs.openSync(filePath, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); +let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts, (err : BusinessError => {}) ``` @@ -2528,7 +2528,7 @@ const path : string = context.getCacheDir() + "/test.png"; const imageSourceApi : image.ImageSource = image.createImageSource(path); let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 }; const filePath : string = context.getCacheDir() + "/image_source.jpg"; -let file = fs.openSync(filePath, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); +let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts).then(()=>{ console.log('Succeeded in packToFile.'); }).catch((error : BusinessError)=>{ @@ -2567,7 +2567,7 @@ image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } const context : _Context = featureAbility.getContext(); const path : string = context.getCacheDir() + "pixel_map.jpg"; - let file = fs.openSync(path, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); + let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); imagePackerApi.packToFile(pixelmap, file.fd, packOpts, (err : BusinessError) => {}) }) ``` @@ -2606,7 +2606,7 @@ image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } const context : _Context = featureAbility.getContext(); const path : string = context.getCacheDir() + "pixel_map.jpg"; - let file = fs.openSync(path, fs.OpenMode.CREATE|fs.OpenMode.READ_WRITE); + let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); imagePackerApi.packToFile(pixelmap, file.fd, packOpts) .then(() => { console.log('Succeeded in packToFile.'); -- Gitee From 2fb6f5b5aa4d8cd216850946a7b545ec1a670f88 Mon Sep 17 00:00:00 2001 From: yangfan Date: Wed, 15 Nov 2023 14:20:28 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangfan --- zh-cn/application-dev/media/image-encoding.md | 4 +- .../reference/apis/js-apis-image.md | 38 +++++++++---------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/zh-cn/application-dev/media/image-encoding.md b/zh-cn/application-dev/media/image-encoding.md index eca60f8987a..dbc5566b304 100644 --- a/zh-cn/application-dev/media/image-encoding.md +++ b/zh-cn/application-dev/media/image-encoding.md @@ -49,7 +49,9 @@ }) ``` -5. 图片编码进文件,文件保存在内存中。 +### 图片编码进文件 + +在编码时,开发者可以传入对应的文件路径,编码后的内存数据将直接写入文件。 方法一:通过PixelMap编码进文件。 diff --git a/zh-cn/application-dev/reference/apis/js-apis-image.md b/zh-cn/application-dev/reference/apis/js-apis-image.md index fd90f59c54f..bc31ce8c418 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-image.md +++ b/zh-cn/application-dev/reference/apis/js-apis-image.md @@ -2467,7 +2467,7 @@ imagePackerApi.release().then(()=>{ packToFile(source: ImageSource, fd: number, option: PackingOption, callback: AsyncCallback\): void -按给定格式编码进文件,使用callback形式返回结果。 +指定打包参数,将ImageSource图片源编码后直接打包进文件。使用callback形式返回结果。 **系统能力:** SystemCapability.Multimedia.Image.ImagePacker @@ -2485,22 +2485,22 @@ packToFile(source: ImageSource, fd: number, option: PackingOption, callback: Asy ```ts import {BusinessError} from '@ohos.base' import fs from '@ohos.file.fs' -import featureAbility from '@ohos.ability.featureAbility' -const context : _Context = featureAbility.getContext(); -const path : string = context.getCacheDir() + "/test.png"; +const context : Context = getContext(this); +const path : string = context.filesDir + "/test.png"; const imageSourceApi : image.ImageSource = image.createImageSource(path); let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 }; -const filePath : string = context.getCacheDir() + "/image_source.jpg"; +const filePath : string = context.cacheDir + "/image_source.jpg"; let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); -imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts, (err : BusinessError => {}) +const imagePackerApi : image.ImagePacker = image.createImagePacker(); +imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts, (err : BusinessError) => {}) ``` ### packToFile11+ packToFile (source: ImageSource, fd: number, option: PackingOption): Promise\ -按给定格式编码进文件,使用promise形式返回结果。 +指定打包参数,将ImageSource图片源编码后直接打包进文件。使用Promise形式返回结果。 **参数:** @@ -2521,14 +2521,14 @@ packToFile (source: ImageSource, fd: number, option: PackingOption): Promise\{ console.log('Succeeded in packToFile.'); }).catch((error : BusinessError)=>{ @@ -2540,7 +2540,7 @@ imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts).then(()=>{ packToFile (source: Pixelmap, fd: number, option: PackingOption, callback: AsyncCallback\): void; -按给定格式编码进文件,使用callback形式返回结果。 +指定打包参数,将PixelMap图片源编码后直接打包进文件。使用callback形式返回结果。 **系统能力:** SystemCapability.Multimedia.Image.ImagePacker @@ -2558,16 +2558,16 @@ packToFile (source: Pixelmap, fd: number, option: PackingOption, callback: Asyn ```ts import {BusinessError} from '@ohos.base' import fs from '@ohos.file.fs' -import featureAbility from '@ohos.ability.featureAbility' const color : ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小,取值为:height * width *4 let bufferArr : Uint8Array = new Uint8Array(color); let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } } image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } - const context : _Context = featureAbility.getContext(); - const path : string = context.getCacheDir() + "pixel_map.jpg"; + const context : Context = getContext(this); + const path : string = context.cacheDir + "/pixel_map.jpg"; let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); + const imagePackerApi : image.ImagePacker = image.createImagePacker(); imagePackerApi.packToFile(pixelmap, file.fd, packOpts, (err : BusinessError) => {}) }) ``` @@ -2576,7 +2576,7 @@ image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { packToFile (source: Pixelmap, fd: number, option: PackingOption): Promise\ -按给定格式编码进文件,使用promise形式返回结果。 +指定打包参数,将PixelMap图片源编码后直接打包进文件。使用Promise形式返回结果。 **参数:** @@ -2597,16 +2597,16 @@ packToFile (source: Pixelmap, fd: number, option: PackingOption): Promise\ ```ts import {BusinessError} from '@ohos.base' import fs from '@ohos.file.fs' -import featureAbility from '@ohos.ability.featureAbility' const color : ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小,取值为:height * width *4 let bufferArr : Uint8Array = new Uint8Array(color); let opts : image.InitializationOptions = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } } image.createPixelMap(color, opts).then((pixelmap : image.PixelMap) => { let packOpts : image.PackingOption = { format: "image/jpeg", quality: 98 } - const context : _Context = featureAbility.getContext(); - const path : string = context.getCacheDir() + "pixel_map.jpg"; + const context : Context = getContext(this); + const path : string = context.cacheDir + "/pixel_map.jpg"; let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); + const imagePackerApi : image.ImagePacker = image.createImagePacker(); imagePackerApi.packToFile(pixelmap, file.fd, packOpts) .then(() => { console.log('Succeeded in packToFile.'); -- Gitee