该模块提供了组件createCollaborationServiceMenuItems和CollaborationServiceFilter,两者需要配合使用,完成分布式跨端能力,如在PC/2in1端跨端调用Phone端拍照。
通过createCollaborationServiceMenuItems组件,可以获取组网内具有对应能力的设备列表。用户选择对应的设备后,拉起应用。调用CollaborationServiceStateDialog,应用将弹出提示框,提示对端应用状态。
起始版本: 5.0.0(12)
import { CollaborationServiceStateDialog, createCollaborationServiceMenuItems, CollaborationServiceFilter} from '@kit.ServiceCollaborationKit'; 能力类型枚举值。
模型约束: 此模块的接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.Service
起始版本: 5.0.0(12)
| 名称 | 值 | 说明 |
|---|---|---|
| ALL | 0 | 匹配跨端拍照、文档扫描和图库选择器。 |
| TAKE_PHOTO | 1 | 匹配跨端拍照。 |
| SCAN_DOCUMENT | 2 | 匹配文档扫描。 |
| IMAGE_PICKER | 3 | 匹配图库选择器。 |
| VIDEO_PICKER | 5 | 匹配视频选择器。 起始版本: 6.0.0(20) |
| IMAGE_VIDEO_PICKER | 6 | 匹配图片和视频选择器。 起始版本: 6.0.0(20) |
示例:
@Builder
myTestMenu() {
Menu() {
createCollaborationServiceMenuItems([CollaborationServiceFilter.ALL], 30);
}
} 设备类型枚举值。
模型约束: 此模块的接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.Service
起始版本: 6.1.0(23)
| 名称 | 值 | 说明 |
|---|---|---|
| PHONE | 1 | Phone。 |
| TABLET | 2 | Tablet。 |
| PC_2IN1 | 3 | PC/2in1。 |
示例:
@Builder
myTestMenu() {
Menu() {
createCollaborationServiceMenuItems([CollaborationServiceFilter.ALL], 30, [CollaborationDeviceFilterType.PHONE, CollaborationDeviceFilterType.TABLET]);
}
} 弹窗组件,用于提示对端应用状态。
您需要实现onState方法,并且在页面中定义这个组件,在业务开始后,此方法将被协同框架调用。
该组件为自定义组件,开发者在使用前需要先了解@Component。
模型约束: 此模块的接口仅可在Stage模型下使用。
装饰器类型: @Component
系统能力: SystemCapability.Collaboration.Service
起始版本: 5.0.0(12)
onState: (stateCode: number, bufferType: string, buffer: ArrayBuffer) => void
接收数据的回调函数,其中传入的stateCode是完成状态,bufferType是回传的数据类型,buffer是回传的数据内容,开发者可通过状态和数据结合自身的业务逻辑实现onState方法。
模型约束: 此模块的接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.Service
起始版本: 5.0.0(12)
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| stateCode | number | 是 | 标识业务完成状态,含义详见下面状态码。 |
| bufferType | string | 是 | 标识回传数据类型。 "general.image":图片类型。 "general.fileName":文件名称。 "general.video":视频,从6.1.0(23)开始支持此类型。 |
| buffer | ArrayBuffer | 是 | 当bufferType为"general.image",成功则返回对应数据,失败则返回空。 当bufferType为"general.video",成功则返回对应存在应用沙箱视频uri路径,失败则返回空。 |
状态码:
以下部分错误状态的详细介绍请参见错误码。
| 状态码ID | 错误信息 |
|---|---|
| 0 | Success. |
| 1001202001 | The peer end cancels the operation. |
| 1001202002 | An error occurred within the collaborative framework. |
| 1001202003 | The local end cancels the operation. |
| 1001202004 | The device interconnectivity has been established. |
| 1001202005 | All images have been successfully sent back. |
| 1001202006 | Multiple images are being sent back. |
| 1001202007 | The number of input custom images is less than or equal to 0. |
| 1001202015 | All videos have been successfully sent back. |
| 1001202016 | Multiple videos are being sent back. |
| 1001202017 | Out of memory, video send back failed. |
build(): void
struct的默认构造函数,开发者无法直接调用此方法。
模型约束: 此模块的接口仅可在Stage模型下使用。
系统能力: SystemCapability.Collaboration.Service
起始版本: 5.0.0(12)
示例1:
6.0.0(20)之前的版本,参考以下示例,可以完成一次调用对端应用的获取图片操作。
跨设备互通详细介绍可参考跨设备互通特性简介。
import {
createCollaborationServiceMenuItems,
CollaborationServiceStateDialog,
CollaborationServiceFilter
} from '@kit.ServiceCollaborationKit';
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
@Entry
@Component
struct Index {
@State picture: PixelMap | undefined = undefined;
@Builder
myTestMenu() {
Menu() {
createCollaborationServiceMenuItems([CollaborationServiceFilter.ALL], 30);
}
}
build() {
Column({ space: 20 }) {
CollaborationServiceStateDialog({
onState: (stateCode: number, bufferType: string, buffer: ArrayBuffer): void => this.doInsertPicture(stateCode,
bufferType, buffer)
})
Button('使用远端设备进行拍照')
.type(ButtonType.Normal)
.borderRadius(10)
.bindMenu(this.myTestMenu)
if (this.picture) {
Image(this.picture)
.borderStyle(BorderStyle.Dotted)
.borderWidth(1)
.objectFit(ImageFit.Contain)
.height('80%')
.onComplete((event) => {
if (event != undefined) {
hilog.info(0, 'MEMOMOCK', 'onComplete ' + event.loadingStatus);
}
})
}
}
.padding(20)
.width('100%')
.alignItems(HorizontalAlign.Center)
}
doInsertPicture(stateCode: number, bufferType: string, buffer: ArrayBuffer): void {
if (stateCode != 0) {
return;
}
if (bufferType == 'general.image') {
let imageSource = image.createImageSource(buffer);
imageSource.createPixelMap().then((pixelMap) => {
this.picture = pixelMap;
}).catch((error: Error) => {
hilog.error(0, 'MEMOMOCK', 'Create pixel map failed: ' + error);
}).finally(() => {
imageSource.release();
});
}
}
} 示例2:
6.0.0(20)及之后的版本,参考以下示例,可以完成一次调用对端应用的获取图片和视频操作。
跨设备互通详细介绍可参考跨设备互通特性简介。
import {
createCollaborationServiceMenuItems, CollaborationServiceStateDialog
} from '@kit.ServiceCollaborationKit';
import { image } from '@kit.ImageKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
class VideoItem {
id: string;
src: string;
constructor(id: string, src: string) {
this.id = id;
this.src = src;
}
}
@Entry
@Component
struct Index {
@State picture: PixelMap | undefined = undefined;
controller: RichEditorController = new RichEditorController();
options: RichEditorOptions = { controller: this.controller };
@State videoSrc: string = '';
@State videoItems: VideoItem[] = [];
refreshId: number = 0;
@Builder
myToolBarMenu() {
Menu() {
// create collaboration camera menuItems to show camera devices.
createCollaborationServiceMenuItems([0, 5], 50);
}
}
aboutToAppear(): void {
hilog.info(0, 'MEMOMOCK', 'aboutToAppear ');
}
aboutToDisappear(): void {
hilog.info(0, 'MEMOMOCK', 'aboutToDisappear ');
}
build() {
Column({ space: 20 }) {
CollaborationServiceStateDialog({
onState: (stateCode: number, dataType?: string, buffer?: ArrayBuffer): void =>
this.doInsertPicture(stateCode, dataType, buffer)
})
Button('使用远端设备进行拍照')
.type(ButtonType.Normal)
.borderRadius(10)
.bindMenu(this.myToolBarMenu)
RichEditor(this.options)
.onReady(() => {
hilog.info(0x0000, 'MEMOMOCK', 'RichEditor');
}
)
Column() {
List() {
ForEach(this.videoItems, (item: VideoItem) => {
ListItem() {
Video({
src: item.src,
controller: new VideoController()
})
.objectFit(ImageFit.Auto)
.width('20%')
.height('20%')
.margin({ top: 10, right: 10 })
.onError((err) => {
hilog.error(0, 'MEMOMOCK', `code is ${err.code}, message is ${err.message}`);
})
}
}, (item: VideoItem) => {
hilog.info(0, 'MEMOMOCK', 'item id ' + item.id);
hilog.info(0, 'MEMOMOCK', 'item src' + item.src);
return item.id;
});
}
}.padding({ bottom: '120vp' })
}
.width('100%')
.alignItems(HorizontalAlign.Center);
}
doInsertPicture(stateCode: number, dataType?: string, buffer?: ArrayBuffer): void {
hilog.info(0, 'MEMOMOCK', 'doInsertPicture is ' + stateCode)
if (dataType == 'general.video') {
let decoder = util.TextDecoder.create('utf-8');
let uriStr = decoder.decodeToString(new Uint8Array(buffer));
hilog.info(0, 'MEMOMOCK', 'Received URI: ' + uriStr);
const newId = String(++this.refreshId);
this.videoItems.push(new VideoItem(newId, uriStr));
} else if (dataType == 'general.image') {
let imageSource = image.createImageSource(buffer);
imageSource && imageSource.createPixelMap().then((pixelMap) => {
this.picture = pixelMap;
if (this.controller) {
this.controller.addImageSpan(pixelMap,
{
imageStyle:
{
size: ['200px', '200px']
}
});
}
}).catch((error: Error) => {
hilog.error(0, 'MEMOMOCK', 'Create pixel map failed: ' + error);
}).finally(() => {
imageSource.release();
});
}
}
}