index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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" 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"></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. const emits = defineEmits(['success', 'error', 'cancel'])
  30. const popupRef = ref(null)
  31. const showConfirmBtn = ref(false);
  32. let zhuapaiFun = null;
  33. let stopCamera = null;
  34. function startCamera() {
  35. // 请求摄像头权限并获取流
  36. // #ifdef H5
  37. console.log('navigator', navigator)
  38. const {
  39. startH5Camera,
  40. handlePaiZhao,
  41. stopH5Camera
  42. } = useH5Camera({
  43. elVideoId: '#videoConfirm',
  44. elCanvasId: '#canvasConfirm',
  45. onVideoSuccess,
  46. onVideoError
  47. })
  48. startH5Camera();
  49. zhuapaiFun = handlePaiZhao;
  50. stopCamera = stopH5Camera;
  51. // #endif
  52. // #ifdef APP-PLUS
  53. console.log('App端暂时不支持摄像头功能')
  54. // #endif
  55. }
  56. function handleZhua() {
  57. zhuapaiFun()
  58. }
  59. function onVideoSuccess() {
  60. showConfirmBtn.value = true;
  61. }
  62. function onVideoError() {
  63. showConfirmBtn.false = true;
  64. emits('error')
  65. }
  66. function showDialog() {
  67. popupRef.value.open();
  68. nextTick(() => {
  69. startCamera()
  70. })
  71. }
  72. function handleClose() {
  73. emits('cancel')
  74. popupRef.value.close();
  75. stopCamera && stopCamera()
  76. }
  77. function handleConfirm() {
  78. emits('success')
  79. popupRef.value.close();
  80. stopCamera && stopCamera()
  81. }
  82. defineExpose({
  83. showDialog
  84. })
  85. onUnmounted(() => {
  86. // 组件销毁时停止摄像头
  87. stopCamera && stopCamera();
  88. })
  89. </script>
  90. <style lang="scss">
  91. .phone-camera-box {
  92. width: 100%;
  93. }
  94. .uni-video-container {
  95. width: 100%;
  96. }
  97. .uni-video-video {
  98. width: 100%;
  99. }
  100. </style>