index.vue 1.3 KB

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