startShowCamera() {
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
var size = "small"
if (hf.dataMgr.gameRecordQuality === hf.constants.GAME_RECORD_QUALITY.MID) {
size = "medium"
}
else if (hf.dataMgr.gameRecordQuality === hf.constants.GAME_RECORD_QUALITY.HIGH) {
size = "large"
}
var camera = wx.createCamera({
x: 0,
y: 0,
width: 1,
height: 1,
devicePosition: "front",
size: size,
success: () => {
cc.log("CameraDisplay startShowCamera Success")
this.startListener()
this.m_openCb && this.m_openCb()
},
fail: (err) => {
cc.log("CameraDisplay startShowCamera Fail", err)
}
})
this.m_camera = camera
}
},
closeCamera() {
if (this.m_camera) {
if (cc.sys.platform === cc.sys.WECHAT_GAME) {
this.m_camera.destroy()
this.m_camera = undefined
}
}
this.fullSp.node.active = false
this.quarterSp.node.active = false
this.m_tex = undefined
},
startListener() {
var camera = this.m_camera
camera.onCameraFrame((frameObj) => {
if (this.m_tex) {
var data = new Uint8Array(frameObj.data)
this.m_tex.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, frameObj.width, frameObj.height)
this.m_tex.handleLoadedTexture()
}
})
camera.listenFrameChange()
}
代码如上图所示,我每次调用startShowCamera开启相机后,使用完成会destroy销毁,中间需要调用onCameraFrame监听帧变化。
只有第一次调用后,能监听到onCameraFrame回调函数帧变化,相机销毁后再次创建新的相机再也无法监听到帧数据。
我这边能确认的是,wx.createCamera函数调用后,每次都有success回调,也就是我的startListener函数里面的listenFrameChange这些都是执行了。但是不起作用。而且我能确认摄像头确实是打开的。
https://developers.weixin.qq.com/community/develop/doc/00080caf6c04505dd0da0ad6d5b014
官方说解决了,但实际仍然存在
解决方法是在新相机调Camera.listenFrameChange前先调下Camera.closeFrameChange