index.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <template>
  2. <view class="video-container-box">
  3. <view class="video-tip-box"> 摄像头初始化... </view>
  4. <camera class="zp-camera" device-position="front" flash="off" @initdone="onVideoSuccess"></camera>
  5. </view>
  6. </template>
  7. <script setup>
  8. import {
  9. ref,
  10. onUnmounted,
  11. onMounted,
  12. nextTick
  13. } from "vue"
  14. import {
  15. useH5Camera
  16. } from "./useCamera.js"
  17. const emits = defineEmits(['success', 'error', 'cancel'])
  18. const popupRef = ref(null)
  19. const showConfirmBtn = ref(false);
  20. const cameraContext = ref(null);
  21. let zhuapaiFun = null;
  22. let stopCamera = null;
  23. let errorFunCompleteFn = null;
  24. function startCamera() {
  25. // 请求摄像头权限并获取流
  26. cameraContext.value = uni.createCameraContext();
  27. }
  28. function handleZhua() {
  29. zhuapaiFun()
  30. }
  31. function onVideoSuccess() {
  32. }
  33. function onVideoError(e) {
  34. emits('error')
  35. errorFunCompleteFn && errorFunCompleteFn(e)
  36. }
  37. function showDialog() {
  38. setTimeout(() => {
  39. startCamera()
  40. },1000)
  41. }
  42. function handleClose() {
  43. emits('cancel')
  44. stopCamera && stopCamera()
  45. }
  46. function handleConfirm() {
  47. emits('success')
  48. stopCamera && stopCamera()
  49. }
  50. defineExpose({
  51. showDialog
  52. })
  53. onUnmounted(() => {
  54. // 组件销毁时停止摄像头
  55. stopCamera && stopCamera();
  56. })
  57. </script>