diff --git a/zh-cn/application-dev/media/image-encoding.md b/zh-cn/application-dev/media/image-encoding.md index 8f722b582d92901a1a36c45408512aaf70867e03..dbc5566b30438948d3d2f09f34036ead9ecfcdb1 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,38 @@ console.error('Failed to pack the image. And the error is: ' + error); }) ``` + +### 图片编码进文件 + +在编码时,开发者可以传入对应的文件路径,编码后的内存数据将直接写入文件。 + + 方法一:通过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 3fde61a6b3a92c0f4f34a2df38283d3cd1ada3be..bc31ce8c418dff673c658504291263ae7aaf907f 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 + +指定打包参数,将ImageSource图片源编码后直接打包进文件。使用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' + +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.cacheDir + "/image_source.jpg"; +let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); +const imagePackerApi : image.ImagePacker = image.createImagePacker(); +imagePackerApi.packToFile(imageSourceApi, file.fd, packOpts, (err : BusinessError) => {}) +``` + +### packToFile11+ + +packToFile (source: ImageSource, fd: number, option: PackingOption): Promise\ + +指定打包参数,将ImageSource图片源编码后直接打包进文件。使用Promise形式返回结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------- | ---- | -------------- | +| source | [ImageSource](#imagesource) | 是 | 打包的图片源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | --------------------------------- | +| Promise\ | Promise实例,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' + +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.cacheDir + "/image_source.jpg"; +let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); +const imagePackerApi : image.ImagePacker = image.createImagePacker(); +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; + +指定打包参数,将PixelMap图片源编码后直接打包进文件。使用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' + +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 = 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) => {}) +}) +``` + +### packToFile11+ + +packToFile (source: Pixelmap, fd: number, option: PackingOption): Promise\ + +指定打包参数,将PixelMap图片源编码后直接打包进文件。使用Promise形式返回结果。 + +**参数:** + +| 参数名 | 类型 | 必填 | 说明 | +| ------ | ------------------------------- | ---- | -------------------- | +| source | [PixelMap](#pixelmap7) | 是 | 打包的PixelMap资源。 | +| fd | number | 是 | 文件描述符。 | +| option | [PackingOption](#packingoption) | 是 | 设置打包参数。 | + +**返回值:** + +| 类型 | 说明 | +| -------------- | --------------------------------- | +| Promise\ | Promise实例,失败时返回错误信息。 | + +**示例:** + +```ts +import {BusinessError} from '@ohos.base' +import fs from '@ohos.file.fs' + +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 = 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.'); + }).catch((error : BusinessError) => { + console.log('Failed to packToFile.'); + }) +}) +``` + ## image.createImageReceiver9+ createImageReceiver(width: number, height: number, format: number, capacity: number): ImageReceiver