自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OpenHarmony 分布式相機(jī)(中)

系統(tǒng) OpenHarmony
實(shí)現(xiàn)分布式相機(jī)其實(shí)很簡單,正如官方介紹的一樣,當(dāng)被控端相機(jī)被連接成功后,可以像使用本地設(shè)備一樣使用遠(yuǎn)程相機(jī)。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

接上一篇??OpenHarmony 分布式相機(jī)(上)??,今天我們來說下如何實(shí)現(xiàn)分布式相機(jī)。

實(shí)現(xiàn)分布式相機(jī)其實(shí)很簡單,正如官方介紹的一樣,當(dāng)被控端相機(jī)被連接成功后,可以像使用本地設(shè)備一樣使用遠(yuǎn)程相機(jī)。

我們先看下效果

??視頻地址??

OpenHarmony 分布式相機(jī)(中)-開源基礎(chǔ)軟件社區(qū)

上一篇已經(jīng)完整的介紹了如何開發(fā)一個(gè)本地相機(jī),對(duì)于分布式相機(jī)我們需要完成以下幾個(gè)步驟:

前置條件

1、兩臺(tái)帶攝像頭的設(shè)備
2、建議使用相同版本的OH系統(tǒng),本案例使用OpenHarmony 3.2 beta5
3、連接在同一個(gè)網(wǎng)絡(luò)

開發(fā)步驟

1、引入設(shè)備管理(@ohos.distributedHardware.deviceManager)
2、通過deviceManager發(fā)現(xiàn)周邊設(shè)備
3、通過pin碼完成設(shè)備認(rèn)證
4、獲取和展示可信設(shè)備
5、在可信設(shè)備直接選擇切換不同設(shè)備的攝像頭
6、在主控端查看被控端的攝像頭圖像

以上描述的功能在應(yīng)用開發(fā)時(shí)可以使用一張草圖來表示,草圖中切換設(shè)備->彈窗顯示設(shè)備列表的過程,草圖如下:

OpenHarmony 分布式相機(jī)(中)-開源基礎(chǔ)軟件社區(qū)

代碼

RemoteDeviceModel.ts

說明: 遠(yuǎn)程設(shè)備業(yè)務(wù)處理類,包括獲取可信設(shè)備列表、獲取周邊設(shè)備列表、監(jiān)聽設(shè)備狀態(tài)(上線、下線、狀態(tài)變化)、監(jiān)聽設(shè)備連接失敗、設(shè)備授信認(rèn)證、卸載設(shè)備狀態(tài)監(jiān)聽等

代碼如下:

import deviceManager from '@ohos.distributedHardware.deviceManager'
import Logger from './util/Logger'
const TAG: string = 'RemoteDeviceModel'
let subscribeId: number = -1
export class RemoteDeviceModel {
private deviceList: Array<deviceManager.DeviceInfo> = []
private discoverList: Array<deviceManager.DeviceInfo> = []
private callback: () => void
private authCallback: () => void
private deviceManager: deviceManager.DeviceManager
constructor() {
}
public registerDeviceListCallback(bundleName : string, callback) {
if (typeof (this.deviceManager) !== 'undefined') {
this.registerDeviceListCallbackImplement(callback)
return
}
Logger.info(TAG, `deviceManager.createDeviceManager begin`)
try {
deviceManager.createDeviceManager(bundleName, (error, value) => {
if (error) {
Logger.info(TAG, `createDeviceManager failed.`)
return
}
this.deviceManager = value
this.registerDeviceListCallbackImplement(callback)
Logger.info(TAG, `createDeviceManager callback returned, error= ${error},value= ${value}`)
})
} catch (err) {
Logger.error(TAG, `createDeviceManager failed, code is ${err.code}, message is ${err.message}`)
}
Logger.info(TAG, `deviceManager.createDeviceManager end`)
}
private deviceStateChangeActionOffline(device) {
if (this.deviceList.length <= 0) {
this.callback()
return
}
for (let j = 0; j < this.deviceList.length; j++) {
if (this.deviceList[j ].deviceId === device.deviceId) {
this.deviceList[j] = device
break
}
}
Logger.info(TAG, `offline, device list= ${JSON.stringify(this.deviceList)}`)
this.callback()
}
private registerDeviceListCallbackImplement(callback) {
Logger.info(TAG, `registerDeviceListCallback`)
this.callback = callback
if (this.deviceManager === undefined) {
Logger.info(TAG, `deviceManager has not initialized`)
this.callback()
return
}
Logger.info(TAG, `getTrustedDeviceListSync begin`)
try {
let list = this.deviceManager.getTrustedDeviceListSync()
Logger.info(TAG, `getTrustedDeviceListSync end, deviceList= ${JSON.stringify(list)}`)
if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') {
this.deviceList = list
}
} catch (err) {
Logger.error(`getTrustedDeviceListSync failed, code is ${err.code}, message is ${err.message}`)
}
this.callback()
Logger.info(TAG, `callback finished`)
this.deviceManager.on('deviceStateChange', (data) => {
if (data === null) {
return
}
Logger.info(TAG, `deviceStateChange data= ${JSON.stringify(data)}`)
switch (data.action) {
case deviceManager.DeviceStateChangeAction.READY:
this.discoverList = []
this.deviceList.push(data.device)
try {
let list = this.deviceManager.getTrustedDeviceListSync()
if (typeof (list) !== 'undefined' && typeof (list.length) !== 'undefined') {
this.deviceList = list
}
this.callback()
} catch (err) {
Logger.error(TAG, `getTrustedDeviceListSync failed, code is ${err.code}, message is ${err.message}`)
}
break
case deviceManager.DeviceStateChangeAction.OFFLINE:
case deviceManager.DeviceStateChangeAction.CHANGE:
this.deviceStateChangeActionOffline(data.device)
break
default:
break
}
})
this.deviceManager.on('deviceFound', (data) => {
if (data === null) {
return
}
Logger.info(TAG, `deviceFound data= ${JSON.stringify(data)}`)
this.deviceFound(data)
})
this.deviceManager.on('discoverFail', (data) => {
Logger.info(TAG, `discoverFail data= ${JSON.stringify(data)}`)
})
this.deviceManager.on('serviceDie', () => {
Logger.info(TAG, `serviceDie`)
})
this.startDeviceDiscovery()
}
private deviceFound(data) {
for (var i = 0;i < this.discoverList.length; i++) {
if (this.discoverList[i].deviceId === data.device.deviceId) {
Logger.info(TAG, `device founded ignored`)
return
}
}
this.discoverList[this.discoverList.length] = data.device
Logger.info(TAG, `deviceFound self.discoverList= ${this.discoverList}`)
this.callback()
}
private startDeviceDiscovery() {
if (subscribeId >= 0) {
Logger.info(TAG, `started DeviceDiscovery`)
return
}
subscribeId = Math.floor(65536 * Math.random())
let info = {
subscribeId: subscribeId,
mode: deviceManager.DiscoverMode.DISCOVER_MODE_ACTIVE,
medium: deviceManager.ExchangeMedium.COAP,
freq: deviceManager.ExchangeFreq.HIGH,
isSameAccount: false,
isWakeRemote: true,
capability: deviceManager.SubscribeCap.SUBSCRIBE_CAPABILITY_DDMP
}
Logger.info(TAG, `startDeviceDiscovery ${subscribeId}`)
try {
// todo 多次啟動(dòng)發(fā)現(xiàn)周邊設(shè)備有什么影響嗎?
this.deviceManager.startDeviceDiscovery(info)
} catch (err) {
Logger.error(TAG, `startDeviceDiscovery failed, code is ${err.code}, message is ${err.message}`)
}
}
public unregisterDeviceListCallback() {
Logger.info(TAG, `stopDeviceDiscovery $subscribeId}`)
this.deviceList = []
this.discoverList = []
try {
this.deviceManager.stopDeviceDiscovery(subscribeId)
} catch (err) {
Logger.error(TAG, `stopDeviceDiscovery failed, code is ${err.code}, message is ${err.message}`)
}
this.deviceManager.off('deviceStateChange')
this.deviceManager.off('deviceFound')
this.deviceManager.off('discoverFail')
this.deviceManager.off('serviceDie')
}
public authenticateDevice(device, extraInfo, callBack) {
Logger.info(TAG, `authenticateDevice ${JSON.stringify(device)}`)
for (let i = 0; i < this.discoverList.length; i++) {
if (this.discoverList[i].deviceId !== device.deviceId) {
continue
}
let authParam = {
'authType': 1,
'appIcon': '',
'appThumbnail': '',
'extraInfo': extraInfo
}
try {
this.deviceManager.authenticateDevice(device, authParam, (err, data) => {
if (err) {
Logger.error(TAG, `authenticateDevice error: ${JSON.stringify(err)}`)
this.authCallback = null
return
}
Logger.info(TAG, `authenticateDevice succeed: ${JSON.stringify(data)}`)
this.authCallback = callBack
})
} catch (err) {
Logger.error(TAG, `authenticateDevice failed, code is ${err.code}, message is ${err.message}`)
}
}
}
/**
* 已認(rèn)證設(shè)備列表
*/
public getDeviceList(): Array<deviceManager.DeviceInfo> {
return this.deviceList
}
/**
* 發(fā)現(xiàn)設(shè)備列表
*/
public getDiscoverList(): Array<deviceManager.DeviceInfo> {
return this.discoverList
}
}
  • getDeviceList() : 獲取已認(rèn)證的設(shè)備列表。
  • getDiscoverList :發(fā)現(xiàn)周邊設(shè)備的列表。

DeviceDialog.ets

說明: 通過RemoteDeviceModel.getDiscoverList()和通過RemoteDeviceModel.getDeviceList()獲取到所有周邊設(shè)備列表,用戶通過點(diǎn)擊"切換設(shè)備"按鈕彈窗顯示所有設(shè)備列表信息。

import deviceManager from '@ohos.distributedHardware.deviceManager';
const TAG = 'DeviceDialog'
// 分布式設(shè)備選擇彈窗
@CustomDialog
export struct DeviceDialog {
private controller?: CustomDialogController // 彈窗控制器
@Link deviceList: Array<deviceManager.DeviceInfo> // 設(shè)備列表
@Link selectIndex: number // 選中的標(biāo)簽
build() {
Column() {
List() {
ForEach(this.deviceList, (item: deviceManager.DeviceInfo, index) => {
ListItem() {
Row() {
Text(item.deviceName)
.fontSize(22)
.width(350)
.fontColor(Color.Black)
Image(index === this.selectIndex ? $r('app.media.checked') : $r('app.media.uncheck'))
.width(35)
.objectFit(ImageFit.Contain)
}
.height(55)
.onClick(() => {
console.info(`${TAG} select device ${item.deviceId}`)
if (index === this.selectIndex) {
console.info(`${TAG} device not change`)
} else {
this.selectIndex = index
}
this.controller.close()
})
}
}, item => item.deviceName)
}
.width('100%')
.height(150)
Button() {
Text($r('app.string.cancel'))
.width('100%')
.height(45)
.fontSize(18)
.fontColor(Color.White)
.textAlign(TextAlign.Center)
}.onClick(() => {
this.controller.close()
})
.backgroundColor('#ed3c13')
}
.width('100%')
.padding(20)
.backgroundColor(Color.White)
.border({
color: Color.White,
radius: 20
})
}
}

打開設(shè)備列表彈窗

說明: 在index.ets頁面中,點(diǎn)擊“切換設(shè)備”按鈕即可以開啟設(shè)備列表彈窗,通過@Watch(‘selectedIndexChange’)監(jiān)聽用戶選擇的設(shè)備標(biāo)簽,在devices中獲取到具體的DeviceInfo對(duì)象。
代碼如下:

@State @Watch('selectedIndexChange') selectIndex: number = 0
// 設(shè)備列表
@State devices: Array<deviceManager.DeviceInfo> = []
// 設(shè)備選擇彈窗
private dialogController: CustomDialogController = new CustomDialogController({
builder: DeviceDialog({
deviceList: $devices,
selectIndex: $selectIndex,
}),
autoCancel: true,
alignment: DialogAlignment.Center
})
showDialog() {
console.info(`${TAG} RegisterDeviceListCallback begin`)
distributed.registerDeviceListCallback(BUNDLE_NAME, () => {
console.info(`${TAG} RegisterDeviceListCallback callback entered`)
this.devices = []
// 添加本地設(shè)備
this.devices.push({
deviceId: Constant.LOCAL_DEVICE_ID,
deviceName: Constant.LOCAL_DEVICE_NAME,
deviceType: 0,
networkId: '',
range: 1 // 發(fā)現(xiàn)設(shè)備的距離
})
let discoverList = distributed.getDiscoverList()
let deviceList = distributed.getDeviceList()
let discoveredDeviceSize = discoverList.length
let deviceSize = deviceList.length
console.info(`${TAG} discoveredDeviceSize:${discoveredDeviceSize} deviceSize:${deviceSize}`)
let deviceTemp = discoveredDeviceSize > 0 ? discoverList : deviceList
for (let index = 0; index < deviceTemp.length; index++) {
this.devices.push(deviceTemp[index])
}
})
this.dialogController.open()
console.info(`${TAG} RegisterDeviceListCallback end`)
}
async selectedIndexChange() {
console.info(`${TAG} select device index ${this.selectIndex}`)
let discoverList: Array<deviceManager.DeviceInfo> = distributed.getDiscoverList()
if (discoverList.length <= 0) {
this.mCurDeviceID = this.devices[this.selectIndex].deviceId
await this.switchDevice()
this.devices = []
return
}
let selectDeviceName = this.devices[this.selectIndex].deviceName
let extraInfo = {
'targetPkgName': BUNDLE_NAME,
'appName': APP_NAME,
'appDescription': APP_NAME,
'business': '0'
}
distributed.authenticateDevice(this.devices[this.selectIndex], extraInfo, async () => {
// 獲取到相關(guān)的設(shè)備ID,啟動(dòng)遠(yuǎn)程應(yīng)用
for (var index = 0; index < distributed.getDeviceList().length; index++) {
let deviceName = distributed.getDeviceList()[index].deviceName
if (deviceName === selectDeviceName) {
this.mCurDeviceID = distributed.getDeviceList()[index].deviceId
await this.switchDevice()
}
}
})
this.devices = []
}

重新加載相機(jī)

說明: 根據(jù)用戶選擇的設(shè)備標(biāo)簽獲取到當(dāng)前用戶需要切換的相機(jī)設(shè)備對(duì)象,重新加載相機(jī),重新加載需要釋放原有的相機(jī)資源,然后重新構(gòu)建createCameraInput、createPreviewOutput、createSession??赡苣阕⒁獾竭@里好像沒有執(zhí)行createPhotoOutput,這是因?yàn)樵趯?shí)踐過程中發(fā)現(xiàn),添加了一個(gè)當(dāng)前設(shè)備所支持的拍照配置到會(huì)話管理(CaptureSession.addOutput())時(shí),系統(tǒng)會(huì)返回當(dāng)前拍照配置流不支持,并關(guān)閉相機(jī),導(dǎo)致相機(jī)預(yù)覽黑屏,所以這里沒有添加。issues:??遠(yuǎn)程相機(jī)拍照失敗 not found in supported streams??

代碼如下:

/**
* 切換攝像頭
* 同一臺(tái)設(shè)備上切換不同攝像頭
*/
async switchCamera() {
console.info(`${TAG} switchCamera`)
let cameraList = this.mCameraService.getDeviceCameras(this.mCurDeviceID)
if (cameraList && cameraList.length > 1) {
let cameraCount: number = cameraList.length
console.info(`${TAG} camera list ${cameraCount}}`)
if (this.mCurCameraIndex < cameraCount - 1) {
this.mCurCameraIndex += 1
} else {
this.mCurCameraIndex = 0
}
await this.reloadCamera()
} else {
this.showToast($r('app.string.only_one_camera_hint'))
}
}

/**
* 重新加載攝像頭
*/
async reloadCamera() {
// 顯示切換loading
this.isSwitchDeviceing = true
// 先關(guān)閉當(dāng)前攝像機(jī),再切換新的攝像機(jī)
await this.mCameraService.releaseCamera()
await this.startPreview()
}
private async startPreview() {
console.info(`${TAG} startPreview`)
await this.mCameraService.createCameraInput(this.mCurCameraIndex, this.mCurDeviceID)
await this.mCameraService.createPreviewOutput(this.surfaceId, this.previewImpl)
if (this.mCurDeviceID === Constant.LOCAL_DEVICE_ID) {
// fixme xjs 如果是遠(yuǎn)程相機(jī),則不支持拍照,添加拍照輸出流會(huì)導(dǎo)致相機(jī)黑屏
await this.mCameraService.createPhotoOutput(this.functionBackImpl)
}
await this.mCameraService.createSession(this.surfaceId)
}

加載過度動(dòng)畫

說明: 在相機(jī)切換中會(huì)需要釋放原相機(jī)的資源,在重啟新相機(jī),在通過軟總線通道同步遠(yuǎn)程相機(jī)的預(yù)覽數(shù)據(jù),這里需要一些時(shí)間,根據(jù)目前測試,在網(wǎng)絡(luò)穩(wěn)定狀態(tài)下,切換時(shí)間3~5s,網(wǎng)絡(luò)不穩(wěn)定狀態(tài)下,切換最長需要13s,當(dāng)然有時(shí)候會(huì)出現(xiàn)無法切換成功,這種情況可能是遠(yuǎn)程設(shè)備已經(jīng)下線,無法再獲取到數(shù)據(jù)。
代碼如下:

@State isSwitchDeviceing: boolean = false // 是否正在切換相機(jī)

if (this.isSwitchDeviceing) {
Column() {
Image($r('app.media.load_switch_camera'))
.width(400)
.height(306)
.objectFit(ImageFit.Fill)
Text($r('app.string.switch_camera'))
.width('100%')
.height(50)
.fontSize(16)
.fontColor(Color.White)
.align(Alignment.Center)
}
.width('100%')
.height('100%')
.backgroundColor(Color.Black)
.justifyContent(FlexAlign.Center)
.alignItems(HorizontalAlign.Center)
.onClick(() => {
})
}

至此,分布式相機(jī)的整體流程就已實(shí)現(xiàn)完成。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

責(zé)任編輯:jianghua 來源: 51CTO 開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2023-02-20 15:29:14

分布式相機(jī)鴻蒙

2023-02-21 16:41:41

分布式相機(jī)鴻蒙

2022-04-08 11:08:17

分布式數(shù)據(jù)接口同步機(jī)制

2022-06-20 15:32:55

Stage模型分布式開發(fā)

2022-04-24 16:00:03

Ability鴻蒙

2024-01-10 08:02:03

分布式技術(shù)令牌,

2021-11-10 16:10:18

鴻蒙HarmonyOS應(yīng)用

2019-10-10 09:16:34

Zookeeper架構(gòu)分布式

2023-05-29 14:07:00

Zuul網(wǎng)關(guān)系統(tǒng)

2017-09-01 05:35:58

分布式計(jì)算存儲(chǔ)

2019-06-19 15:40:06

分布式鎖RedisJava

2022-02-17 18:08:04

OpenHarmon應(yīng)用開發(fā)鴻蒙

2021-12-14 10:16:00

鴻蒙HarmonyOS應(yīng)用

2022-06-15 16:16:21

分布式數(shù)據(jù)庫鴻蒙

2018-12-14 10:06:22

緩存分布式系統(tǒng)

2017-10-27 08:40:44

分布式存儲(chǔ)剪枝系統(tǒng)

2022-03-21 19:44:30

CitusPostgreSQ執(zhí)行器

2023-10-26 18:10:43

分布式并行技術(shù)系統(tǒng)

2024-03-01 09:53:34

2018-07-17 08:14:22

分布式分布式鎖方位
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)