index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <uni-popup ref="popupRef" type="dialog" :animation="false" :is-mask-click="false"
  3. mask-background-color="rgba(0, 0, 0, 0.4);">
  4. <uni-popup-dialog mode="input" class="phone-camera my-dialog-cc" :title="title" :duration="2000" :before-close="true"
  5. @close="handleClose" @confirm="handleConfirm">
  6. <view class="phone-camera-box" style="overflow: hidden;">
  7. <view v-show="!showConfirmBtn" class="sxt-tip-box" style="text-align: center;">{{textMess}}</view>
  8. <video v-show="showConfirmBtn" ref="videoRef" style="width:100%; height: 320rpx;" id="videoConfirm"
  9. :controls="false" :enable-progress-gesture="false"></video>
  10. <!-- 隐藏抓拍绘制图片 -->
  11. <canvas id="canvasConfirm" style="width: auto; height: 320rpx;visibility: hidden;position: absolute;"></canvas>
  12. <!-- <canvas id="canvasConfirm" style="width: auto; height: 320rpx;"></canvas> -->
  13. <!-- 测试抓拍使用 -->
  14. <!-- <button @click="handleZhua">抓拍</button> -->
  15. </view>
  16. </uni-popup-dialog>
  17. </uni-popup>
  18. </template>
  19. <script setup>
  20. import {
  21. ref,
  22. onUnmounted,
  23. onMounted,
  24. nextTick
  25. } from "vue"
  26. import {
  27. useH5Camera
  28. } from "./useCamera.js"
  29. defineProps({
  30. title: {
  31. type: String,
  32. default: '摄像头确认'
  33. }
  34. })
  35. const emits = defineEmits(['success', 'error', 'cancel'])
  36. const popupRef = ref(null)
  37. const showConfirmBtn = ref(false);
  38. let zhuapaiFun = null;
  39. let stopCamera = null;
  40. let errorFunCompleteFn = null;
  41. const textMess = ref('摄像头正在初始化...')
  42. function startCamera() {
  43. // 请求摄像头权限并获取流
  44. // #ifdef H5
  45. console.log('navigator', navigator)
  46. const {
  47. startH5Camera,
  48. handlePaiZhao,
  49. stopH5Camera,
  50. errorFunComplete
  51. } = useH5Camera({
  52. elVideoId: '#videoConfirm',
  53. elCanvasId: '#canvasConfirm',
  54. onVideoSuccess,
  55. onVideoError
  56. })
  57. startH5Camera();
  58. zhuapaiFun = handlePaiZhao;
  59. stopCamera = stopH5Camera;
  60. errorFunCompleteFn = errorFunComplete;
  61. // #endif
  62. // #ifdef APP-PLUS
  63. console.log('App端暂时不支持摄像头功能')
  64. // #endif
  65. }
  66. function handleZhua() {
  67. zhuapaiFun()
  68. }
  69. function onVideoSuccess() {
  70. showConfirmBtn.value = true;
  71. }
  72. function onVideoError(e) {
  73. showConfirmBtn.false = true;
  74. textMess.value = '摄像头初始化失败!'
  75. emits('error')
  76. errorFunCompleteFn && errorFunCompleteFn(e)
  77. }
  78. function showDialog() {
  79. textMess.value = '摄像头正在初始化...'
  80. popupRef.value.open();
  81. nextTick(() => {
  82. startCamera()
  83. })
  84. }
  85. function handleClose() {
  86. emits('cancel')
  87. popupRef.value.close();
  88. stopCamera && stopCamera()
  89. }
  90. function handleConfirm() {
  91. emits('success')
  92. popupRef.value.close();
  93. stopCamera && stopCamera()
  94. }
  95. defineExpose({
  96. showDialog
  97. })
  98. onUnmounted(() => {
  99. // 组件销毁时停止摄像头
  100. stopCamera && stopCamera();
  101. })
  102. </script>
  103. <style lang="scss">
  104. .phone-camera-box {
  105. width: 100%;
  106. }
  107. .uni-video-container {
  108. width: 100%;
  109. }
  110. .uni-video-video {
  111. width: 100%;
  112. }
  113. .my-dialog-cc .uni-dialog-button-group .uni-border-left .uni-button-color{
  114. color: #3fd2a1;
  115. }
  116. </style>