muted="true" :enable-camera="true" :enable-mic="false" :zoom="false" @statechange="statechange" @netstatus="netstatus" @error="error" :style="{ width: windowWidth, height: windowHeight }"></live-pusher>
<view class="menu">
<!--返回键-->
<cover-image class="menu-back" :class="{'selected-btn':selectNum==0}"
src="/static/live-camera/back.png"></cover-image>
<!--快门键-->
<cover-image class="menu-snapshot" :class="{'selected-btn':selectNum==1}"
src="/static/live-camera/shutter.png"></cover-image>
<!--反转键-->
<cover-image class="menu-picture" :class="{'selected-btn':selectNum==2}"
src="/static/live-camera/picture.png"></cover-image>
</view>
</view>
</template>
<script>
let _this = null;
export default {
data() {
return {
eventTime: '',
poenCarmeInterval: null, //打开相机的轮询
aspect: '2:3', //比例
windowWidth: '', //屏幕可用宽度
windowHeight: '', //屏幕可用高度
camerastate: false, //相机准备好了
livePusher: null, //流视频对象
snapshotsrc: null, //快照
selectNum: 0
};
},
onLoad(e) {
_this = this;
this.initCamera();
},
onShow() {
this.listenEvent();
},
onHide() {
this.removeListener();
this.livePusher.stopPreview();
},
onReady() {
this.livePusher = uni.createLivePusherContext('livePusher', this);
var that = this;
setTimeout(function() {
that.startPreview(); //开启预览并设置摄像头
that.poenCarme();
}, 3000)
},
methods: {
listenEvent() {
var that = this;
// 监听键按下事件
uni.$on("keydown", function(res) {
switch (res.data) {
case 21:
if (that.selectNum > 0) {
that.selectNum = that.selectNum - 1;
} else {
that.selectNum = 2;
}
console.log("that.selectNum", that.selectNum)
break;
case 22:
if (that.selectNum < 2) {
that.selectNum = that.selectNum + 1;
} else {
that.selectNum = 0;
}
console.log("that.selectNum", that.selectNum)
break;
}
});
uni.$on("keyup", function(res) {
switch (res.data) {
case 139:
if (that.eventTime) {
var curTime = new Date() - that.eventTime
if (curTime < 500) {
that.eventTime = new Date();
return;
}
}
that.eventTime = new Date();
switch (that.selectNum) {
case 0:
uni.navigateBack();
break;
case 1:
that.snapshot();
break;
case 2:
that.picture();
break;
}
break;
}
});
// uni.$on("longpressed", function(res) {
// uni.navigateBack();
// });
},
removeListener() {
uni.$off('keydown');
uni.$off('keyup');
// uni.$off('longpressed');
},
//轮询打开
poenCarme() {
//#ifdef APP-PLUS
if (plus.os.name == 'Android') {
this.poenCarmeInterval = setInterval(function() {
// console.log("camerastate",_this.camerastate);
if (!_this.camerastate) {
// console.log("startPreview")
_this.startPreview();
}
}, 2500);
}
//#endif
},
//初始化相机
initCamera() {
uni.getSystemInfo({
success: function(res) {
_this.windowWidth = res.windowWidth;
_this.windowHeight = res.windowHeight;
let zcs = _this.aliquot(_this.windowWidth, _this.windowHeight);
_this.aspect = (_this.windowWidth / zcs) + ':' + (_this.windowHeight / zcs);
console.log('画面比例:' + _this.aspect);
}
});
},
//整除数计算
aliquot(x, y) {
if (x % y == 0) return y;
return this.aliquot(y, x % y);
},
//开始预览
startPreview() {
// console.log("preview",JSON.stringify(_this.livePusher))
this.livePusher.start({
success: a => {
console.log('livePusher.start:',JSON.stringify(a));
}
});
this.livePusher.startPreview({
success: (a) => {
// console.log(a)
},
fail: (e) => {
console.log(JSON.stringify(e) + "错误");
},
});
},
//停止预览
stopPreview() {
this.livePusher.stopPreview({
success: (a) => {
_this.camerastate = false;
}
});
},
//状态
statechange(e) {
plus.android.requestPermissions(['android.permission.CAMERA'], function(e) {
// console.log("requestPermissions", e);
})
const appAuthorizeSetting = uni.getAppAuthorizeSetting()
// console.log("cameraAuthorized", appAuthorizeSetting.cameraAuthorized)
//状态改变
console.log("statechange", e);
if (e.detail.code == 1007) {
_this.camerastate = true;
} else if (e.detail.code == -1301) {
_this.camerastate = false;
}
},
netstatus(e) {
console.log("netstatus", e);
},
error(e) {
console.log("error", e);
},
//返回
back() {
uni.navigateBack();
},
//抓拍
snapshot() {
//震动
uni.vibrateShort({
success: function() {
console.log('success');
}
});
//拍照
this.livePusher.snapshot({
success: (e) => {
console.log("--------step1--------")
_this.snapshotsrc = e.message.tempImagePath;
console.log(_this.snapshotsrc)
_this.stopPreview();
_this.setImage();
// uni.uploadFile({
// url: '上传接口的路径',
// filePath: e.path,
// name: 'imageFile',
// success: function(res) {
// //服务器返回的图片地址url
// },
// error: function(err) {
// console.log(err)
// }
// }
uni.navigateBack();
}
});
},
//反转
flip() {
this.livePusher.switchCamera();
},
//相册
picture() {
uni.navigateTo({
url: '/pages/picture/picture'
})
},
//设置
setImage() {
// let pages = getCurrentPages();
// let prevPage = pages[pages.length - 2];
// prevPage.$vm.setImage({
// path: _this.snapshotsrc
// });
}
}
};
</script>
<style lang="less">
.pengke-camera {
height: 100%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
background-color: #000000;
.livePusher {
height: 95%;
width: 200rpx;
}
.menu {
width: 100rpx;
height: 750rpx;
position: absolute;
right: 10rpx;
z-index: 99;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
// background-color: #00aaff;
background-image: url(/static/live-camera/bar.png);
.selected-btn {
border-style: solid;
border-width: 5px;
border-color: #00ffff;
}
.menu-mask {
width: 180rpx;
height: 750rpx;
}
.menu-back {
margin-top: 10rpx;
margin-bottom: 10rpx;
width: 80rpx;
height: 80rpx;
align-items: center;
justify-content: center;
}
.menu-snapshot {
margin-top: 10rpx;
margin-bottom: 10rpx;
width: 80rpx;
height: 80rpx;
}
.menu-picture {
margin-top: 10rpx;
margin-bottom: 10rpx;
width: 80rpx;
height: 80rpx;
align-items: center;
justify-content: center;
}
}
}
</style>
2 个回复
m***@163.com
请问您最后解决这个问题了嘛?
m***@163.com
我使用 live-pusher 切换到后台再返回就是黑屏呢