remote-camera.md 2.8 KB
Newer Older
J
jiangminyang 已提交
1 2 3 4 5 6 7
# 分布式相机开发指导

## 场景介绍

分布式相机模块支持相机相关基础功能介绍说明。

## 开发步骤
J
jiangminyang 已提交
8
在计算器中连接分布式设备,在获取相机列表时getCameras(),遍历返回的列表,判断对应Camera对象中的ConnctionType是否等于CAMERA_CONNECTION_REMOTE,若等于则使用此对象创建camerainput,之后调用与本地相机使用一样。参考:[相机开发指导](./camera.md)
J
jiangminyang 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

### 接口说明

详细API含义请参考:[相机管理API文档](../reference/apis/js-apis-camera.md)

#### 连接分布式相机

打开设置->WLAN,将需要连接分布式相机的两台设备连入同一局域网。

打开计算器,点击右上角小图表,出现新的窗口,按提示输入验证码,连接成功。

#### 创建实例

```js
import camera from '@ohos.multimedia.camera'
import image from '@ohos.multimedia.image'
import media from '@ohos.multimedia.media'
import featureAbility from '@ohos.ability.featureAbility'

//创建CameraManager对象
let cameraManager
await camera.getCameraManager(globalThis.Context, (err, manager) => {
    if (err) {
        console.error('Failed to get the CameraManager instance ${err.message}');
        return;
    }
    console.log('Callback returned with the CameraManager instance');
    cameraManager = manager
})

//注册回调函数监听相机状态变化,获取状态变化的相机信息
cameraManager.on('cameraStatus', (cameraStatusInfo) => {
    console.log('camera : ' + cameraStatusInfo.camera.cameraId);
    console.log('status: ' + cameraStatusInfo.status);
})

//获取相机列表
let cameraArray
let remoteCamera
await cameraManager.getCameras((err, cameras) => {
    if (err) {
        console.error('Failed to get the cameras. ${err.message}');
        return;
    }
    console.log('Callback returned with an array of supported cameras: ' + cameras.length);
    cameraArray = cameras
})

for(let cameraIndex = 0; cameraIndex < cameraArray.length; cameraIndex) {
    console.log('cameraId : ' + cameraArray[cameraIndex].cameraId)                          //获取相机ID
    console.log('cameraPosition : ' + cameraArray[cameraIndex].cameraPosition)              //获取相机位置
    console.log('cameraType : ' + cameraArray[cameraIndex].cameraType)                      //获取相机类型
    console.log('connectionType : ' + cameraArray[cameraIndex].connectionType)              //获取相机连接类型
    if (cameraArray[cameraIndex].connectionType == CAMERA_CONNECTION_REMOTE) {
        remoteCamera = cameraArray[cameraIndex].cameraId
    }
}

//创建相机输入流
let cameraInput
await cameraManager.createCameraInput(remoteCamera).then((input) => {
    console.log('Promise returned with the CameraInput instance');
    cameraInput = input
})
J
jiangminyang 已提交
73
```
J
jiangminyang 已提交
74
剩余步骤参照[相机开发指导](./camera.md)