index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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;">摄像头正在初始化...</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. function startCamera() {
  42. // 请求摄像头权限并获取流
  43. // #ifdef H5
  44. console.log('navigator', navigator)
  45. const {
  46. startH5Camera,
  47. handlePaiZhao,
  48. stopH5Camera,
  49. errorFunComplete
  50. } = useH5Camera({
  51. elVideoId: '#videoConfirm',
  52. elCanvasId: '#canvasConfirm',
  53. onVideoSuccess,
  54. onVideoError
  55. })
  56. startH5Camera();
  57. zhuapaiFun = handlePaiZhao;
  58. stopCamera = stopH5Camera;
  59. errorFunCompleteFn = errorFunComplete;
  60. // #endif
  61. // #ifdef APP-PLUS
  62. console.log('App端暂时不支持摄像头功能')
  63. // #endif
  64. }
  65. function handleZhua() {
  66. zhuapaiFun()
  67. }
  68. function onVideoSuccess() {
  69. showConfirmBtn.value = true;
  70. }
  71. function onVideoError(e) {
  72. showConfirmBtn.false = true;
  73. emits('error')
  74. errorFunCompleteFn && errorFunCompleteFn(e)
  75. }
  76. function showDialog() {
  77. popupRef.value.open();
  78. nextTick(() => {
  79. startCamera()
  80. })
  81. }
  82. function handleClose() {
  83. emits('cancel')
  84. popupRef.value.close();
  85. stopCamera && stopCamera()
  86. }
  87. function handleConfirm() {
  88. emits('success')
  89. popupRef.value.close();
  90. stopCamera && stopCamera()
  91. }
  92. defineExpose({
  93. showDialog
  94. })
  95. onUnmounted(() => {
  96. // 组件销毁时停止摄像头
  97. stopCamera && stopCamera();
  98. })
  99. </script>
  100. <style lang="scss">
  101. .phone-camera-box {
  102. width: 100%;
  103. }
  104. .uni-video-container {
  105. width: 100%;
  106. }
  107. .uni-video-video {
  108. width: 100%;
  109. }
  110. .my-dialog-cc .uni-dialog-button.uni-border-left {
  111. .uni-dialog-button-text.uni-button-color {
  112. color: #3fd2a1;
  113. }
  114. }
  115. </style>