1# @ohos.multimedia.image (图片处理)(系统接口) 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--Designer: @liyang_bryan--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9本模块提供图片处理效果,包括通过属性创建PixelMap、读取图像像素数据、读取区域内的图片数据等。 10 11> **说明:** 12> 13> - 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.multimedia.image (图片处理)](arkts-apis-image.md)。 16 17## 导入模块 18 19```ts 20import { image } from '@kit.ImageKit'; 21``` 22 23## DecodingOptions<sup>12+</sup> 24 25图像解码设置选项。 26 27**系统接口:** 该接口为系统接口 28 29**系统能力:** SystemCapability.Multimedia.Image.ImageSource 30 31| 名称 | 类型 | 只读 | 可选 | 说明 | 32| ----------------- | ----------------- | ---- | ---- | ---------------- | 33| resolutionQuality | [ResolutionQuality](#resolutionquality12) | 否 | 是 | 画质效果等级。 | 34 35## ResolutionQuality<sup>12+</sup> 36 37枚举,画质效果等级类型。 38 39**系统能力:** SystemCapability.Multimedia.Image.Core 40 41| 名称 | 值 | 说明 | 42| ---------------------------- | ------ | ---------- | 43| LOW | 1 | 低画质效果,解码耗时短。<br/>此接口为系统接口。| 44| MEDIUM | 2 | 中等画质效果,解码耗时中等。<br/>此接口为系统接口。| 45| HIGH | 3 | 最高等级画质效果,解码耗时长。<br/>此接口为系统接口。| 46 47## image.createPictureByHdrAndSdrPixelMap<sup>20+</sup> 48 49createPictureByHdrAndSdrPixelMap(hdrPixelMap: PixelMap, sdrPixelMap: PixelMap): Promise\<Picture> 50 51根据HDR PixelMap和SDR PixelMap创建Picture对象。系统将使用HDR和SDR PixelMap生成一个增益图(gainmap),返回的Picture对象将包含SDR PixelMap和生成的gainmap PixelMap,像素格式为RGBA8888。使用Promise异步回调。 52 53**系统接口:** 该接口为系统接口。 54 55**系统能力:** SystemCapability.Multimedia.Image.Core 56 57**参数:** 58 59| 参数名 | 类型 | 必填 | 说明 | 60| ------------ | ------------------- | ---- | ---------------- | 61| hdrPixelMap | [PixelMap](arkts-apis-image-PixelMap.md) | 是 | HDR PixelMap,位深16bit或10bit,像素格式为FP16/RGBA1010102/YCBCR_P010,色彩空间是BT2020_HLG。 | 62| sdrPixelMap | [PixelMap](arkts-apis-image-PixelMap.md) | 是 | SDR PIxelMap,位深8bit,像素格式为RGBA8888/NV21,色彩空间是P3。 | 63 64**返回值:** 65 66| 类型 | 说明 | 67| ------------------ | ----------------- | 68|Promise\<[Picture](arkts-apis-image-Picture.md)> | 返回Picture包含sdr和gainmap,像素格式为RGBA8888。 | 69 70**错误码:** 71 72以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。 73 74| 错误码ID | 错误信息 | 75| -------- | ------------------------------------------------------------ | 76| 7600201 | Unsupported operation. HdrPixelMap's PixelMapFormat is not RGBA_F16\RGBA_1010102\YCBCR_P010, or its color space is not BT2020_HLG. Or sdrPixelMap's PixelMapFormat is not RGBA_8888\NV21\NV12, or its color space is not P3. | 77 78**示例:** 79 80```ts 81import { fileIo as fs } from '@kit.CoreFileKit'; 82import { BusinessError } from '@kit.BasicServicesKit'; 83 84async function CreatePictureTest(context: Context) { 85 const resourceMgr = context.resourceManager; 86 const rawFile = await resourceMgr.getRawFileContent("test.jpg"); // SDR 87 let imageSource: image.ImageSource = image.createImageSource(rawFile); 88 let options1: image.DecodingOptions = { 89 desiredDynamicRange : image.DecodingDynamicRange.SDR, 90 } 91 let options2: image.DecodingOptions = { 92 desiredDynamicRange : image.DecodingDynamicRange.HDR, // 通过AIHDR将SDR解码为HDR。 93 } 94 let sdrPixelMap = await imageSource.createPixelMap(options1); 95 let hdrPixelMap = await imageSource.createPixelMap(options2); 96 97 // 获取计算生成的gainmap并编码。 98 let picture: image.Picture = await image.createPictureByHdrAndSdrPixelMap(hdrPixelMap, sdrPixelMap); 99 if (picture != null) { 100 console.info('Create picture succeeded'); 101 } else { 102 console.error('Create picture failed'); 103 } 104 const imagePackerObj = image.createImagePacker(); 105 let packOpts : image.PackingOption = { format : "image/jpeg", quality: 98}; 106 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 107 const path: string = context.filesDir + "/hdr-test.jpg"; 108 let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 109 imagePackerObj.packToFile(picture, file.fd, packOpts).then(() => { 110 }).catch((error : BusinessError) => { 111 console.error('Failed to pack the image. And the error is: ' + error); 112 }) 113} 114```