1e41f4b71Sopenharmony_ci# ImageBitmap 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciImageBitmap对象可以存储canvas渲染的像素数据。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 从 API Version 8 开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci## 接口 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci### ImageBitmap<sup>12+</sup> 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciImageBitmap(data: PixelMap, unit?: LengthMetricsUnit) 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci通过PixelMap创建ImageBitmap对象。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**参数:** 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 24e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 25e41f4b71Sopenharmony_ci| data | [PixelMap](../../apis-image-kit/js-apis-image.md#pixelmap7) | 是 | 图片的数据源支持PixelMap对象。 | 26e41f4b71Sopenharmony_ci| unit | [LengthMetricsUnit](ts-canvasrenderingcontext2d.md#lengthmetricsunit12) | 否 | 用来配置ImageBitmap对象的单位模式,配置后无法动态更改,配置方法同[CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md#lengthmetricsunit12)。<br>默认值:DEFAULT。 | 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci### ImageBitmap 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ciImageBitmap(src: string, unit?: LengthMetricsUnit) 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci通过ImageSrc创建ImageBitmap对象。 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**参数:** 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 43e41f4b71Sopenharmony_ci| ---- | ------ | ---- | ---------------------------------------- | 44e41f4b71Sopenharmony_ci| src | string | 是 | 图片的数据源支持本地图片。<br>1、string格式用于加载本地图片,例如ImageBitmap("common/images/example.jpg"),type为"entry"和"feature"类型的Module,其图片加载路径的起点为当前Module的ets文件夹,type为"har"和"shared"类型的Module,其图片加载路径的起点为当前构建的"entry"或"feature"类型Module的ets文件夹。<br/>type为"har"和"shared"类型的Module中推荐使用[ImageSource](../../../media/image/image-decoding.md)图片解码方式将资源图片解码为统一的PixelMap加载使用。<br/>2、支持本地图片类型:bmp、jpg、png、svg和webp类型。<br/>**说明:**<br/>- ArkTS卡片上不支持`http://`等网络相关路径前缀、`datashare://`路径前缀以及`file://data/storage`路径前缀的字符串。 | 45e41f4b71Sopenharmony_ci| unit<sup>12+</sup> | [LengthMetricsUnit](../js-apis-arkui-graphics.md#lengthmetricsunit12) | 否 | 用来配置ImageBitmap对象的单位模式,配置后无法动态更改,配置方法同[CanvasRenderingContext2D](ts-canvasrenderingcontext2d.md#lengthmetricsunit12)。<br>默认值:DEFAULT。 | 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ci## 属性 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 描述 | 57e41f4b71Sopenharmony_ci| ------ | ------ | ----- | -------- | --------------------------- | 58e41f4b71Sopenharmony_ci| width | number | 是 | 否 | ImageBitmap的像素宽度。<br>默认单位为vp。 | 59e41f4b71Sopenharmony_ci| height | number | 是 | 否 | ImageBitmap的像素高度。<br>默认单位为vp。 | 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci## close 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ciclose(): void 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci释放ImageBitmap对象相关联的所有图形资源,并将ImageBitmap对象的宽高置为0。close示例代码同创建ImageBitmap代码。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci## ImageBitmap支持并发线程绘制 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci从API version 11开始,当应用创建[Worker线程](../../../arkts-utils/worker-introduction.md),支持使用postMessage将ImageBitmap实例传到Worker中进行绘制,并使用onmessage接收Worker线程发送的绘制结果进行显示。 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci## 示例 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci### 示例1 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci ```ts 82e41f4b71Sopenharmony_ci // xxx.ets 83e41f4b71Sopenharmony_ci @Entry 84e41f4b71Sopenharmony_ci @Component 85e41f4b71Sopenharmony_ci struct ImageExample { 86e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 87e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 88e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/example.jpg") 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci build() { 91e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 92e41f4b71Sopenharmony_ci Canvas(this.context) 93e41f4b71Sopenharmony_ci .width('100%') 94e41f4b71Sopenharmony_ci .height('100%') 95e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 96e41f4b71Sopenharmony_ci .onReady(() => { 97e41f4b71Sopenharmony_ci this.context.drawImage(this.img, 0, 0, 500, 500, 0, 0, 400, 200) 98e41f4b71Sopenharmony_ci this.img.close() 99e41f4b71Sopenharmony_ci }) 100e41f4b71Sopenharmony_ci } 101e41f4b71Sopenharmony_ci .width('100%') 102e41f4b71Sopenharmony_ci .height('100%') 103e41f4b71Sopenharmony_ci } 104e41f4b71Sopenharmony_ci } 105e41f4b71Sopenharmony_ci ``` 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci  108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci### 示例2 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci ```ts 112e41f4b71Sopenharmony_ci// xxx.ets 113e41f4b71Sopenharmony_ci@Entry 114e41f4b71Sopenharmony_ci@Component 115e41f4b71Sopenharmony_cistruct Demo { 116e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true) 117e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci build() { 120e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 121e41f4b71Sopenharmony_ci Canvas(this.context) 122e41f4b71Sopenharmony_ci .width('100%') 123e41f4b71Sopenharmony_ci .height('50%') 124e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 125e41f4b71Sopenharmony_ci .onReady(() => { 126e41f4b71Sopenharmony_ci this.context.fillStyle = "#00ff00" 127e41f4b71Sopenharmony_ci this.context.fillRect(0, 0, 100, 100) 128e41f4b71Sopenharmony_ci let pixel = this.context.getPixelMap(0, 0, 100, 100) 129e41f4b71Sopenharmony_ci let image = new ImageBitmap(pixel) 130e41f4b71Sopenharmony_ci this.context.drawImage(image, 100, 100) 131e41f4b71Sopenharmony_ci }) 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci } 134e41f4b71Sopenharmony_ci .width('100%') 135e41f4b71Sopenharmony_ci .height('100%') 136e41f4b71Sopenharmony_ci } 137e41f4b71Sopenharmony_ci} 138e41f4b71Sopenharmony_ci ``` 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci  141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci### 示例3 144e41f4b71Sopenharmony_ci```ts 145e41f4b71Sopenharmony_ciimport worker from '@ohos.worker'; 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci@Entry 148e41f4b71Sopenharmony_ci@Component 149e41f4b71Sopenharmony_cistruct imageBitmapExamplePage { 150e41f4b71Sopenharmony_ci private settings: RenderingContextSettings = new RenderingContextSettings(true); 151e41f4b71Sopenharmony_ci private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings); 152e41f4b71Sopenharmony_ci private myWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ts'); 153e41f4b71Sopenharmony_ci private img:ImageBitmap = new ImageBitmap("common/images/example.jpg") 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci build() { 156e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 157e41f4b71Sopenharmony_ci Canvas(this.context) 158e41f4b71Sopenharmony_ci .width('100%') 159e41f4b71Sopenharmony_ci .height('100%') 160e41f4b71Sopenharmony_ci .backgroundColor('#ffff00') 161e41f4b71Sopenharmony_ci .onReady(() => { 162e41f4b71Sopenharmony_ci this.myWorker.postMessage({ myImage: this.img }); 163e41f4b71Sopenharmony_ci this.myWorker.onmessage = (e): void => { 164e41f4b71Sopenharmony_ci if (e.data.myImage) { 165e41f4b71Sopenharmony_ci let image: ImageBitmap = e.data.myImage 166e41f4b71Sopenharmony_ci this.context.transferFromImageBitmap(image) 167e41f4b71Sopenharmony_ci } 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci }) 170e41f4b71Sopenharmony_ci } 171e41f4b71Sopenharmony_ci .width('100%') 172e41f4b71Sopenharmony_ci .height('100%') 173e41f4b71Sopenharmony_ci } 174e41f4b71Sopenharmony_ci} 175e41f4b71Sopenharmony_ci``` 176e41f4b71Sopenharmony_ciWorker线程在onmessage中接收到主线程postMessage发送的ImageBitmap,并进行绘制。 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci```ts 179e41f4b71Sopenharmony_ciworkerPort.onmessage = function (e: MessageEvents) { 180e41f4b71Sopenharmony_ci if (e.data.myImage) { 181e41f4b71Sopenharmony_ci let img = e.data.myImage 182e41f4b71Sopenharmony_ci let offCanvas = new OffscreenCanvas(600, 600) 183e41f4b71Sopenharmony_ci let offContext = offCanvas.getContext("2d") 184e41f4b71Sopenharmony_ci offContext.drawImage(img, 0, 0, 500, 500, 0, 0, 400, 200) 185e41f4b71Sopenharmony_ci let image = offCanvas.transferToImageBitmap() 186e41f4b71Sopenharmony_ci workerPort.postMessage({ myImage: image }); 187e41f4b71Sopenharmony_ci } 188e41f4b71Sopenharmony_ci} 189e41f4b71Sopenharmony_ci``` 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci 