1# @ohos.wallpaper (壁纸)(系统接口)
2
3壁纸管理服务为OpenHarmony系统服务,提供壁纸切换功能。从API 9开始壁纸管理的接口调整为系统API,壁纸的切换只能通过系统应用来完成。壁纸管理提供壁纸切换通道,使用壁纸的应用(如:桌面)需订阅壁纸变化通知并刷新壁纸显示。
4
5> **说明:**
6> 
7> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.wallpaper (壁纸)](js-apis-wallpaper.md)
9
10
11## 导入模块
12
13
14```ts
15import { wallpaper } from '@kit.BasicServicesKit';
16```
17## WallpaperResourceType<sup>10+</sup>
18
19定义壁纸资源的枚举类型。
20
21**系统能力**: SystemCapability.MiscServices.Wallpaper
22
23**系统接口**:此接口为系统接口。
24
25| 名称 | 值 |说明 |
26| -------- | -------- |-------- |
27| DEFAULT | 0 |默认为图片资源。 |
28| PICTURE | 1 |图片资源。 |
29| VIDEO | 2 |视频资源。 |
30| PACKAGE | 3 |包资源。 |
31
32## wallpaper.setVideo<sup>10+</sup>
33
34setVideo(source: string, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
35
36将视频资源设置为桌面或锁屏的动态壁纸。使用callback异步回调。
37
38**需要权限**:ohos.permission.SET_WALLPAPER
39
40**系统能力**: SystemCapability.MiscServices.Wallpaper
41
42**系统接口**:此接口为系统接口。
43
44**参数:**
45
46| 参数名 | 类型 | 必填 | 说明 |
47| -------- | -------- | -------- | -------- |
48| source | string | 是 | mp4文件的Uri路径。 |
49| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
50| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 |
51
52**错误码:**
53
54以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
55
56| **错误码ID** | **错误信息**                                |
57| ------------ | ------------------------------------------- |
58| 201          | permission denied.                                                                              |
59| 202          | permission verification failed, application which is not a system application uses system API.  |
60| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
61
62**示例:**
63
64```ts
65import { BusinessError } from '@kit.BasicServicesKit';
66
67let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
68try {
69    wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
70        if (error) {
71            console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
72            return;
73        }
74        console.log(`success to setVideo.`);
75    });
76} catch (error) {
77    console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
78}
79
80```
81
82## wallpaper.setVideo<sup>10+</sup>
83
84setVideo(source: string, wallpaperType: WallpaperType): Promise&lt;void&gt;
85
86将视频资源设置为桌面或锁屏的动态壁纸。使用promise异步回调。
87
88**需要权限**:ohos.permission.SET_WALLPAPER
89
90**系统能力**: SystemCapability.MiscServices.Wallpaper
91
92**系统接口**:此接口为系统接口。
93
94**参数:**
95
96| 参数名 | 类型 | 必填 | 说明 |
97| -------- | -------- | -------- | -------- |
98| source | string | 是 | mp4文件的Uri路径。 |
99| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
100
101**错误码:**
102
103以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
104
105| **错误码ID** | **错误信息**                                |
106| ------------ | ------------------------------------------- |
107| 201          | permission denied.                                                                              |
108| 202          | permission verification failed, application which is not a system application uses system API.  |
109| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
110
111**返回值:**
112
113| 类型 | 说明 |
114| -------- | -------- |
115| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
116
117**示例:**
118
119```ts
120import { BusinessError } from '@kit.BasicServicesKit';
121
122let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.mp4";
123try {
124    wallpaper.setVideo(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
125        console.log(`success to setVideo.`);
126    }).catch((error: BusinessError) => {
127        console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
128    });
129} catch (error) {
130    console.error(`failed to setVideo because: ${JSON.stringify(error)}`);
131}
132```
133
134## wallpaper.setCustomWallpaper<sup>10+</sup>
135
136setCustomWallpaper(source: string, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
137
138将指定的zip资源包设置为桌面或锁屏的壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。且具有ohos.permission.GET_WALLPAPER权限的应用可以访问/data/wallpaper/目录获取设置的资源。使用callback异步回调。
139
140**需要权限**:ohos.permission.SET_WALLPAPER
141
142**系统能力**: SystemCapability.MiscServices.Wallpaper
143
144**系统接口**:此接口为系统接口。
145
146**参数:**
147
148| 参数名 | 类型 | 必填 | 说明 |
149| -------- | -------- | -------- | -------- |
150| source | string | 是 | 指定的zip资源包。 |
151| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
152| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 |
153
154**错误码:**
155
156以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
157
158| **错误码ID** | **错误信息**                                |
159| ------------ | ------------------------------------------- |
160| 201          | permission denied.                                                                              |
161| 202          | permission verification failed, application which is not a system application uses system API.  |
162| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
163
164**示例:**
165
166```ts
167import { BusinessError } from '@kit.BasicServicesKit';
168
169let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip";
170try {
171    wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
172        if (error) {
173            console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
174            return;
175        }
176        console.log(`success to setCustomWallpaper.`);
177    });
178} catch (error) {
179    console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
180}
181
182```
183
184## wallpaper.setCustomWallpaper<sup>10+</sup>
185
186setCustomWallpaper(source: string, wallpaperType: WallpaperType): Promise&lt;void&gt;
187
188将指定的zip资源包设置为桌面或锁屏的壁纸资源,仅当com.ohos.sceneboard存在时,支持使用该接口。且具有ohos.permission.GET_WALLPAPER权限的应用可以访问/data/wallpaper/目录获取设置的资源。使用Promise异步回调。
189
190**需要权限**:ohos.permission.SET_WALLPAPER
191
192**系统能力**: SystemCapability.MiscServices.Wallpaper
193
194**系统接口**:此接口为系统接口。
195
196**参数:**
197
198| 参数名 | 类型 | 必填 | 说明 |
199| -------- | -------- | -------- | -------- |
200| source | string | 是 | 指定的zip资源包。 |
201| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
202
203**返回值:**
204
205| 类型 | 说明 |
206| -------- | -------- |
207| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
208
209**错误码:**
210
211以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
212
213| **错误码ID** | **错误信息**                                |
214| ------------ | ------------------------------------------- |
215| 201          | permission denied.                                                                              |
216| 202          | permission verification failed, application which is not a system application uses system API.  |
217| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
218
219**示例:**
220
221```ts
222import { BusinessError } from '@kit.BasicServicesKit';
223
224let wallpaperPath = "/data/storage/el2/base/haps/entry/files/test.zip";
225try {
226    wallpaper.setCustomWallpaper(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
227        console.log(`success to setCustomWallpaper.`);
228    }).catch((error: BusinessError) => {
229        console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
230    });
231} catch (error) {
232    console.error(`failed to setCustomWallpaper because: ${JSON.stringify(error)}`);
233}
234```
235
236## wallpaper.on('wallpaperChange')<sup>10+</sup>
237
238on(type: 'wallpaperChange', callback: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) =&gt; void): void
239
240订阅壁纸变化通知事件。不支持多线程并发调用。
241
242**系统能力**: SystemCapability.MiscServices.Wallpaper
243
244**系统接口**:此接口为系统接口。
245
246**参数:**
247
248| 参数名 | 类型 | 必填 | 说明 |
249| -------- | -------- | -------- | -------- |
250| type | string | 是 | 事件回调类型。支持的事件为'wallpaperChange',完成壁纸切换后触发该事件。 |
251| callback | function | 是 | 壁纸变化触发该回调方法,返回壁纸类型和壁纸资源类型。<br/>- wallpaperType:壁纸类型。<br/>- resourceType:壁纸资源类型。<br/>- uri:壁纸资源地址。 |
252
253**错误码:**
254
255以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
256
257| **错误码ID** | **错误信息**                                |
258| ------------ | ------------------------------------------- |
259| 202          | permission verification failed, application which is not a system application uses system API.  |
260| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
261
262**示例:**
263
264```ts
265try {
266    let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => {
267        console.log(`wallpaper color changed.`);
268    };
269    wallpaper.on('wallpaperChange', listener);
270} catch (error) {
271    console.error(`failed to on because: ${JSON.stringify(error)}`);
272}
273```
274
275## wallpaper.off('wallpaperChange')<sup>10+</sup>
276
277off(type: 'wallpaperChange', callback?: (wallpaperType: WallpaperType, resourceType: WallpaperResourceType, uri?: string) =&gt; void): void
278
279取消订阅壁纸变化通知事件。不支持多线程并发调用。
280
281**系统能力**: SystemCapability.MiscServices.Wallpaper
282
283**系统接口**:此接口为系统接口。
284
285**参数:**
286
287| 参数名 | 类型 | 必填 | 说明 |
288| -------- | -------- | -------- | -------- |
289| type | string | 是 | 事件回调类型。支持的事件为'wallpaperChange',完成壁纸切换后触发该事件。 |
290| callback | function | 否 |   表示要取消的壁纸变化回调,不填写该参数则取消订阅该type对应的所有回调。<br/>- wallpaperType:壁纸类型。<br/>- resourceType:壁纸资源类型。<br/>- uri:壁纸资源地址。 |
291
292**错误码:**
293
294以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
295
296| **错误码ID** | **错误信息**                                |
297| ------------ | ------------------------------------------- |
298| 202          | permission verification failed, application which is not a system application uses system API.  |
299| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
300
301**示例:**
302
303```ts
304let listener = (wallpaperType: wallpaper.WallpaperType, resourceType: wallpaper.WallpaperResourceType): void => {
305    console.log(`wallpaper color changed.`);
306};
307try {
308    wallpaper.on('wallpaperChange', listener);
309} catch (error) {
310    console.error(`failed to on because: ${JSON.stringify(error)}`);
311}
312
313try {
314    // 取消订阅listener
315    wallpaper.off('wallpaperChange', listener);
316} catch (error) {
317    console.error(`failed to off because: ${JSON.stringify(error)}`);
318}
319
320try {
321    // 取消所有'wallpaperChange'类型的订阅
322    wallpaper.off('wallpaperChange');
323} catch (error) {
324    console.error(`failed to off because: ${JSON.stringify(error)}`);
325}
326```
327
328## wallpaper.getColorsSync<sup>9+</sup>
329
330getColorsSync(wallpaperType: WallpaperType): Array&lt;RgbaColor&gt;
331
332获取指定类型壁纸的主要颜色信息。
333
334**系统能力**: SystemCapability.MiscServices.Wallpaper
335
336**系统接口**:此接口为系统接口。
337
338**参数:**
339
340| 参数名 | 类型 | 必填 | 说明 |
341| -------- | -------- | -------- | -------- |
342| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
343
344**返回值**:
345
346| 类型 | 说明 |
347| -------- | -------- |
348| Array&lt;[RgbaColor](js-apis-wallpaper.md#rgbacolordeprecated)&gt; | 返回壁纸的主要颜色信息。 |
349
350**错误码:**
351
352以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
353
354| **错误码ID** | **错误信息**                                |
355| ------------ | ------------------------------------------- |
356| 202          | permission verification failed, application which is not a system application uses system API.  |
357| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
358
359**示例**:
360
361```ts
362try {
363    let colors = wallpaper.getColorsSync(wallpaper.WallpaperType.WALLPAPER_SYSTEM);
364    console.log(`success to getColorsSync: ${JSON.stringify(colors)}`);
365} catch (error) {
366    console.error(`failed to getColorsSync because: ${JSON.stringify(error)}`);
367}
368```
369
370## wallpaper.getMinHeightSync<sup>9+</sup>
371
372getMinHeightSync(): number
373
374获取壁纸的最小高度值。
375
376**系统能力**: SystemCapability.MiscServices.Wallpaper
377
378**系统接口**:此接口为系统接口。
379
380**返回值:**
381
382| 类型 | 说明 |
383| -------- | -------- |
384| number | 返回壁纸的最小高度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的高度值代替。 |
385
386**错误码:**
387
388以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
389
390| **错误码ID** | **错误信息**                                |
391| ------------ | ------------------------------------------- |
392| 202          | permission verification failed, application which is not a system application uses system API.  |
393
394**示例:**
395
396```ts
397let minHeight = wallpaper.getMinHeightSync();
398```
399
400## wallpaper.getMinWidthSync<sup>9+</sup>
401
402getMinWidthSync(): number
403
404获取壁纸的最小宽度值。
405
406**系统能力**: SystemCapability.MiscServices.Wallpaper
407
408**系统接口**:此接口为系统接口。
409
410**返回值:**
411
412| 类型 | 说明 |
413| -------- | -------- |
414| number | 壁纸的最小宽度值,单位是像素。如果返回值等于0,说明没有设置壁纸,调用者应该使用默认显示的宽度值代替。 |
415
416**错误码:**
417
418以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
419
420| **错误码ID** | **错误信息**                                |
421| ------------ | ------------------------------------------- |
422| 202          | permission verification failed, application which is not a system application uses system API.  |
423
424**示例:**
425
426```ts
427let minWidth = wallpaper.getMinWidthSync();
428```
429
430## wallpaper.restore<sup>9+</sup>
431
432restore(wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
433
434移除指定类型的壁纸,恢复为默认显示的壁纸。使用callback异步回调。
435
436**需要权限**:ohos.permission.SET_WALLPAPER
437
438**系统能力**: SystemCapability.MiscServices.Wallpaper
439
440**系统接口**:此接口为系统接口。
441
442**参数:**
443
444| 参数名 | 类型 | 必填 | 说明 |
445| -------- | -------- | -------- | -------- |
446| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
447| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,移除壁纸成功,error为undefined,否则返回error信息。 |
448
449**错误码:**
450
451以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
452
453| **错误码ID** | **错误信息**                                |
454| ------------ | ------------------------------------------- |
455| 201          | permission denied.                                                                              |
456| 202          | permission verification failed, application which is not a system application uses system API.  |
457| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
458
459**示例:**
460
461```ts
462import { BusinessError } from '@kit.BasicServicesKit';
463
464wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
465    if (error) {
466        console.error(`failed to restore because: ${JSON.stringify(error)}`);
467        return;
468    }
469    console.log(`success to restore.`);
470});
471```
472
473## wallpaper.restore<sup>9+</sup>
474
475restore(wallpaperType: WallpaperType): Promise&lt;void&gt;
476
477移除指定类型的壁纸,恢复为默认显示的壁纸。使用promise异步回调。
478
479**需要权限**:ohos.permission.SET_WALLPAPER
480
481**系统能力**: SystemCapability.MiscServices.Wallpaper
482
483**系统接口**:此接口为系统接口。
484
485**参数:**
486
487| 参数名 | 类型 | 必填 | 说明 |
488| -------- | -------- | -------- | -------- |
489| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
490
491**返回值:**
492
493| 类型 | 说明 |
494| -------- | -------- |
495| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
496
497**错误码:**
498
499以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
500
501| **错误码ID** | **错误信息**                                |
502| ------------ | ------------------------------------------- |
503| 201          | permission denied.                                                                              |
504| 202          | permission verification failed, application which is not a system application uses system API.  |
505| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
506
507**示例:**
508
509```ts
510import { BusinessError } from '@kit.BasicServicesKit';
511 
512wallpaper.restore(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
513    console.log(`success to restore.`);
514  }).catch((error: BusinessError) => {
515    console.error(`failed to restore because: ${JSON.stringify(error)}`);
516});
517```
518
519## wallpaper.setImage<sup>9+</sup>
520
521setImage(source: string | image.PixelMap, wallpaperType: WallpaperType, callback: AsyncCallback&lt;void&gt;): void
522
523将指定资源设置为指定类型的壁纸。使用callback异步回调。
524
525**需要权限**:ohos.permission.SET_WALLPAPER
526
527**系统能力**: SystemCapability.MiscServices.Wallpaper
528
529**系统接口**:此接口为系统接口。
530
531**参数:**
532
533| 参数名 | 类型 | 必填 | 说明 |
534| -------- | -------- | -------- | -------- |
535| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
536| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
537| callback | AsyncCallback&lt;void&gt; | 是 | 回调函数,设置壁纸成功,error为undefined,否则返回error信息。 |
538
539**错误码:**
540
541以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
542
543| **错误码ID** | **错误信息**                                |
544| ------------ | ------------------------------------------- |
545| 201          | permission denied.                                                                              |
546| 202          | permission verification failed, application which is not a system application uses system API.  |
547| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
548
549**示例:**
550
551```ts
552import { BusinessError } from '@kit.BasicServicesKit';
553import { image } from '@kit.ImageKit';
554
555// source类型为string
556let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
557wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
558    if (error) {
559        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
560        return;
561     }
562    console.log(`success to setImage.`);
563});
564  
565// source类型为image.PixelMap
566let imageSource = image.createImageSource("file://" + wallpaperPath);
567let opts: image.DecodingOptions = {
568    desiredSize: {
569        height: 3648,
570        width: 2736
571    }
572};
573imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
574    wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError) => {
575        if (error) {
576            console.error(`failed to setImage because: ${JSON.stringify(error)}`);
577            return;
578        }
579        console.log(`success to setImage.`);
580    });
581}).catch((error: BusinessError) => {
582    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
583});
584```
585
586## wallpaper.setImage<sup>9+</sup>
587
588setImage(source: string | image.PixelMap, wallpaperType: WallpaperType): Promise&lt;void&gt;
589
590将指定资源设置为指定类型的壁纸。使用promise异步回调。
591
592**需要权限**:ohos.permission.SET_WALLPAPER
593
594**系统能力**: SystemCapability.MiscServices.Wallpaper
595
596**系统接口**:此接口为系统接口。
597
598**参数:**
599
600| 参数名 | 类型 | 必填 | 说明 |
601| -------- | -------- | -------- | -------- |
602| source | string \| [image.PixelMap](../apis-image-kit/js-apis-image.md) | 是 | JPEG或PNG文件的Uri路径,或者PNG格式文件的位图。 |
603| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
604
605**返回值:**
606
607| 类型 | 说明 |
608| -------- | -------- |
609| Promise&lt;void&gt; | 无返回结果的Promise对象。 |
610
611**错误码:**
612
613以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
614
615| **错误码ID** | **错误信息**                                |
616| ------------ | ------------------------------------------- |
617| 201          | permission denied.                                                                              |
618| 202          | permission verification failed, application which is not a system application uses system API.  |
619| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
620
621**示例:**
622
623```ts
624import { BusinessError } from '@kit.BasicServicesKit';
625import { image } from '@kit.ImageKit';
626
627// source类型为string
628let wallpaperPath = "/data/storage/el2/base/haps/entry/files/js.jpeg";
629wallpaper.setImage(wallpaperPath, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
630    console.log(`success to setImage.`);
631}).catch((error: BusinessError) => {
632    console.error(`failed to setImage because: ${JSON.stringify(error)}`);
633});
634
635// source类型为image.PixelMap
636let imageSource = image.createImageSource("file://" + wallpaperPath);
637let opts: image.DecodingOptions = {
638    desiredSize: {
639        height: 3648,
640        width: 2736
641    }
642};
643imageSource.createPixelMap(opts).then((pixelMap: image.PixelMap) => {
644    wallpaper.setImage(pixelMap, wallpaper.WallpaperType.WALLPAPER_SYSTEM).then(() => {
645        console.log(`success to setImage.`);
646    }).catch((error: BusinessError) => {
647        console.error(`failed to setImage because: ${JSON.stringify(error)}`);
648    });
649}).catch((error: BusinessError) => {
650    console.error(`failed to createPixelMap because: ${JSON.stringify(error)}`);
651});
652```
653
654## wallpaper.getImage<sup>9+</sup>
655
656getImage(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
657
658获取壁纸图片的像素图,且只能获取使用setImage设置的静态壁纸。使用callback异步回调。
659
660**需要权限**:ohos.permission.GET_WALLPAPER
661
662**系统能力**: SystemCapability.MiscServices.Wallpaper
663
664**系统接口**:此接口为系统接口。
665
666**参数:**
667
668| 参数名 | 类型 | 必填 | 说明 |
669| -------- | -------- | -------- | -------- |
670| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
671| callback | AsyncCallback&lt;[image.PixelMap](../apis-image-kit/js-apis-image.md)&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
672
673**错误码:**
674
675以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
676
677| **错误码ID** | **错误信息**                                |
678| ------------ | ------------------------------------------- |
679| 201          | permission denied.                                                                              |
680| 202          | permission verification failed, application which is not a system application uses system API.  |
681| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
682
683**示例:**
684
685```ts
686import { BusinessError } from '@kit.BasicServicesKit';
687import { image } from '@kit.ImageKit';
688
689wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => {
690    if (error) {
691        console.error(`failed to getImage because: ${JSON.stringify(error)}`);
692        return;
693    }
694    console.log(`success to getImage: ${JSON.stringify(data)}`);
695});
696```
697
698
699## wallpaper.getImage<sup>9+</sup>
700
701getImage(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
702
703获取壁纸图片的像素图,且只能获取使用setImage设置的静态壁纸。使用promise异步回调。
704
705**需要权限**:ohos.permission.GET_WALLPAPER
706
707**系统能力**: SystemCapability.MiscServices.Wallpaper
708
709**系统接口**:此接口为系统接口。
710
711**参数:**
712
713| 参数名 | 类型 | 必填 | 说明 |
714| -------- | -------- | -------- | -------- |
715| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
716
717**返回值:**
718
719| 类型 | 说明 |
720| -------- | -------- |
721| Promise&lt;[image.PixelMap](../apis-image-kit/js-apis-image.md)&gt; | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
722
723**错误码:**
724
725以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
726
727| **错误码ID** | **错误信息**                                |
728| ------------ | ------------------------------------------- |
729| 201          | permission denied.                                                                              |
730| 202          | permission verification failed, application which is not a system application uses system API.  |
731| 401          | 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types; 3.Parameter verification failed.  |
732
733**示例:**
734
735```ts
736import { BusinessError } from '@kit.BasicServicesKit';
737import { image } from '@kit.ImageKit';
738
739wallpaper.getImage(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => {
740    console.log(`success to getImage: ${JSON.stringify(data)}`);
741  }).catch((error: BusinessError) => {
742    console.error(`failed to getImage because: ${JSON.stringify(error)}`);
743});
744```
745
746
747
748## wallpaper.getPixelMap<sup>(deprecated)</sup>
749
750getPixelMap(wallpaperType: WallpaperType, callback: AsyncCallback&lt;image.PixelMap&gt;): void;
751
752获取壁纸图片的像素图。
753
754> **说明:**
755>
756> 从 API version 7开始支持,从API version 9开始废弃。
757
758**需要权限**:ohos.permission.GET_WALLPAPER
759
760**系统能力**: SystemCapability.MiscServices.Wallpaper
761
762**系统接口**:此接口为系统接口。
763
764**参数:**
765
766| 参数名 | 类型 | 必填 | 说明 |
767| -------- | -------- | -------- | -------- |
768| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
769| callback | AsyncCallback&lt;image.PixelMap&gt; | 是 | 回调函数,调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
770
771**示例:**
772
773```ts
774import { BusinessError } from '@kit.BasicServicesKit';
775import { image } from '@kit.ImageKit';
776
777wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM, (error: BusinessError, data: image.PixelMap) => {
778    if (error) {
779        console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
780        return;
781    }
782    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
783  });
784```
785
786## wallpaper.getPixelMap<sup>(deprecated)</sup>
787
788getPixelMap(wallpaperType: WallpaperType): Promise&lt;image.PixelMap&gt;
789
790获取壁纸图片的像素图。
791
792> **说明:**
793>
794> 从 API version 7开始支持,从API version 9开始废弃。
795
796**需要权限**:ohos.permission.GET_WALLPAPER
797
798**系统能力**: SystemCapability.MiscServices.Wallpaper
799
800**系统接口**:此接口为系统接口。
801
802**参数:**
803
804| 参数名 | 类型 | 必填 | 说明 |
805| -------- | -------- | -------- | -------- |
806| wallpaperType | [WallpaperType](js-apis-wallpaper.md#wallpapertype7) | 是 | 壁纸类型。 |
807
808**返回值:**
809
810| 类型 | 说明 |
811| -------- | -------- |
812| Promise&lt;image.PixelMap&gt; | 调用成功则返回壁纸图片的像素图对象,调用失败则返回error信息。 |
813
814**示例:**
815
816```ts
817import { BusinessError } from '@kit.BasicServicesKit';
818import { image } from '@kit.ImageKit';
819
820wallpaper.getPixelMap(wallpaper.WallpaperType.WALLPAPER_SYSTEM).then((data: image.PixelMap) => {
821    console.log(`success to getPixelMap : ${JSON.stringify(data)}`);
822  }).catch((error: BusinessError) => {
823    console.error(`failed to getPixelMap because: ${JSON.stringify(error)}`);
824});
825```