zhuapai.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view id="Drop" :style="style" @touchmove="touchmove($event)" @touchstart="touchstart($event)"
  3. class="dropContainer">
  4. <view class="phone-camera-box-zhuapai" style="overflow: hidden;height: 150rpx;">
  5. <video ref="videoRef" :class="myClass" style="width:100%; height: 150rpx;position: absolute;"
  6. id="videoZhaPai" :controls="false"></video>
  7. <!-- 隐藏抓拍绘制图片 -->
  8. <canvas id="canvasZhuaPai" :class="myClass"
  9. style="width:100%; height: 150rpx;position: absolute;"></canvas>
  10. <!-- 用于抓拍切出去传递固定img-->
  11. <!-- #ifdef H5 -->
  12. <img :src="imgUrl" alt="" ref="gudingImg" v-show="false">
  13. <!-- #endif -->
  14. <!-- 测试抓拍使用 -->
  15. <!-- <button @click="handleZhua">抓拍</button> -->
  16. </view>
  17. <span v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn">开<i></i></span>
  18. <span v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn">关<i></i></span>
  19. </view>
  20. </template>
  21. <script setup>
  22. import {
  23. ref,
  24. onUnmounted,
  25. nextTick,
  26. computed
  27. } from "vue";
  28. import {
  29. useH5Camera
  30. } from "@/components/zhuapaiConfirm/useCamera";
  31. import * as ksApi from "@/api/kaoshi.js"
  32. import {
  33. getStaticUrl
  34. } from "@/utils/common.js"
  35. const imgUrl = getStaticUrl('static/images/exam/nokaoshi.png')
  36. let zhuapaiFun = null;
  37. let stopCamera = null;
  38. const zhuapai = ref(0); // 单位分
  39. const showVideo = ref(true);
  40. const style = ref({
  41. top: "10vh",
  42. right: "0",
  43. });
  44. const myClass = computed(() => {
  45. return {
  46. 'show-video': showVideo.value,
  47. 'hidden-video': !showVideo.value
  48. }
  49. })
  50. const emits = defineEmits(['init', 'success', 'error', 'cancel'])
  51. function noShowVideoBtn() {
  52. showVideo.value = false
  53. }
  54. function showVideoBtn() {
  55. showVideo.value = true
  56. }
  57. function touchmove(event) {
  58. console.log(4444, event)
  59. }
  60. function touchstart(event) {
  61. console.log(3333, event)
  62. }
  63. function init(options) {
  64. zhuapai.value = options.zhuapai;
  65. if (zhuapai.value > 0) {
  66. // 启动摄像头
  67. nextTick(() => {
  68. startCamera()
  69. })
  70. // 设定计时器
  71. // setInterval(() => handleZhua(), zhuapai.value * 60 * 1000 || 6000000)
  72. }
  73. }
  74. function startCamera() {
  75. // 请求摄像头权限并获取流
  76. // #ifdef H5
  77. console.log('navigator', navigator)
  78. const {
  79. startH5Camera,
  80. handlePaiZhao,
  81. stopH5Camera
  82. } = useH5Camera({
  83. elVideoId: '#videoZhaPai',
  84. elCanvasId: '#canvasZhuaPai',
  85. onVideoSuccess,
  86. onVideoError,
  87. zhuapaiHttp: ksApi.getClientZhuaPaiUpdate
  88. })
  89. startH5Camera();
  90. zhuapaiFun = handlePaiZhao;
  91. stopCamera = stopH5Camera;
  92. // #endif
  93. }
  94. function handleZhua() {
  95. zhuapaiFun && zhuapaiFun()
  96. }
  97. function onVideoSuccess() {
  98. setTimeout(() => {
  99. // 首次运行进行抓拍一次
  100. // handleZhua();
  101. }, 3000);
  102. }
  103. function onVideoError() {
  104. emits('error')
  105. }
  106. onUnmounted(() => {
  107. // 组件销毁时停止摄像头
  108. stopCamera && stopCamera();
  109. })
  110. defineExpose({
  111. init
  112. })
  113. </script>
  114. <style lang="scss" scoped>
  115. .dropContainer {
  116. width: 30vw;
  117. height: 150rpx;
  118. margin: 0;
  119. padding: 0;
  120. z-index: 10;
  121. position: absolute;
  122. right: 0;
  123. top: 30%;
  124. ::v-deep .uni-video-container {
  125. position: absolute;
  126. }
  127. ::v-deep .uni-video-video {
  128. position: absolute;
  129. }
  130. ::v-deep .uni-canvas-canvas {
  131. position: absolute;
  132. }
  133. ::v-deep .show-video {
  134. z-index: 10;
  135. .uni-video-video,.uni-canvas-canvas {
  136. z-index: 10;
  137. }
  138. }
  139. ::v-deep .uni-video-container {
  140. }
  141. ::v-deep .hidden-video {
  142. z-index: -10;
  143. .uni-video-video,.uni-canvas-canvas,{
  144. z-index: -10;
  145. }
  146. }
  147. }
  148. </style>