Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

viewfinder.vue 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view style="width: 100%;height: 100vh;position: fixed;left: 0;top:0;z-index: 111111;">
  3. <!-- @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd" -->
  4. <view class="viewfinder">
  5. <view v-if="phoneType == 1"
  6. style="display: flex;align-items: center;justify-content: flex-end;width: 100%;height: 100%;">
  7. <image :src="fileURL + 'image/index/etc_bd_ocr_id_card_locator_back.png'"
  8. style="height: 220rpx;margin-right: 30rpx;" mode="heightFix"></image>
  9. </view>
  10. <view v-if="phoneType == 2" style="margin: 30rpx 0 0 30rpx;">
  11. <image :src="fileURL + 'image/index/etc_bd_ocr_id_card_locator_front.png'" style="height: 160rpx;"
  12. mode="heightFix">
  13. </image>
  14. </view>
  15. <view v-if="phoneType == 3"
  16. style="margin: -30rpx 0 0 30rpx;display: flex;height: 100%;align-self: flex-end;flex-direction: column;justify-content: flex-end;">
  17. <view style="border: 1px solid #fff;height: 160rpx;width: 160rpx;">
  18. </view>
  19. </view>
  20. <view v-if="phoneType == 4"
  21. style="margin: -30rpx 30rpx 0 0;display: flex;height: 100%;justify-content: flex-end;align-items: flex-end;">
  22. <view style="border: 1px solid #fff;height: 60rpx;width: 300rpx;">
  23. </view>
  24. </view>
  25. </view>
  26. <!-- camera -->
  27. <camera v-if="showStartPhoto" id="camera" style="height: 70vh;width: 100vh;background-color: black;width: 100%;"
  28. mode="normal" :device-position="cameraPosition" :flash="flash" @stop="cameraStop" @error="cameraError" />
  29. <view v-if="!showStartPhoto" style="height: 70vh;background-color: black;">
  30. <view class="container">
  31. <image style="position: absolute;top: 20%;left: 10%;touch-action: none;" :src="srcImg"
  32. @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"
  33. :style="{ transform: `translate(${offsetX}px, ${offsetY}px) rotate(${rotateDegree}deg)` }"></image>
  34. </view>
  35. </view>
  36. <view style="background-color: #A5A5A5;top: 60%;width: 100%;height: 40%;position: absolute;">
  37. <view style="display: flex;flex-direction: row;justify-content: center;align-items: center;height: 100%;">
  38. <view style="flex: 1;"></view>
  39. <image v-if="!showStartPhoto" @click="camera" :src="fileURL + 'image/index/etc_bd_ocr_cancel.png'"
  40. style="width: 50rpx;height: 50rpx;"></image>
  41. <view style="flex: 1;"></view>
  42. <image @click="takePhoto"
  43. :src="showStartPhoto ? fileURL + 'image/index/etc_bd_ocr_take_photo_normal.png' : fileURL + '/static/etc_bd_ocr_rotate.png'"
  44. :style="showStartPhoto ? 'width: 140rpx;height: 140rpx;' : 'width: 80rpx;height: 80rpx;'"></image>
  45. <view style="flex: 1;"></view>
  46. <image v-if="!showStartPhoto" @click="success" :src="fileURL + 'image/index/etc_bd_ocr_confirm.png'"
  47. style="width: 50rpx;height: 50rpx;"></image>
  48. <view style="flex: 1;"></view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script setup lang="ts">
  54. import {
  55. fileURL
  56. } from "@/datas/fileURL.js";
  57. import {
  58. ref
  59. } from 'vue'
  60. let prop = defineProps({
  61. phoneType: {
  62. type: Number,
  63. default: function () {
  64. return 3 //1 身份证正面 2 身份证反面 3 行驶证正面 4 行驶证反面
  65. }
  66. },
  67. showStartPhoto: {
  68. type: Boolean,
  69. default: function () {
  70. return true
  71. }
  72. },
  73. images: {
  74. type: String
  75. }
  76. });
  77. console.log('输出内容', prop.phoneType)
  78. const emit = defineEmits<{
  79. (e : "confirmReturn", content : any) : void;
  80. (e : "failReturn", content : any) : void;
  81. (e : "camera") : void;
  82. }>();
  83. let cameraPosition = 'back' // 'back'为后置摄像头,'front'为前置摄像头
  84. let flash = 'off' // 'off'为关闭闪光灯,'on'为打开闪光灯
  85. let srcImg = ref(prop.images)
  86. let resItem = ref(null);
  87. let rotateDegree = ref(0)
  88. const startX = ref(0);
  89. const startY = ref(0);
  90. const offsetX = ref(0);
  91. const offsetY = ref(0);
  92. const dragging = ref(false);
  93. function takePhoto() {
  94. console.log('输出内容', rotateDegree.value)
  95. if (prop.showStartPhoto) {
  96. let cameraContext = null
  97. // #ifdef MP-WEIXIN
  98. cameraContext = uni.createCameraContext();
  99. // #endif
  100. // #ifdef MP-ALIPAY
  101. cameraContext = my.createCameraContext('camera');
  102. // #endif
  103. // 调用拍照方法
  104. cameraContext.takePhoto({
  105. quality: 'normal',
  106. success: (res) => {
  107. // 在这里处理拍照成功后的逻辑,res.tempImagePath 为拍照的图片路径
  108. console.log('拍照成功:', res.tempImagePath);
  109. srcImg.value = res.tempImagePath
  110. emit('confirmReturn', res)
  111. // showStartPhoto.value = false
  112. // resItem.value = res
  113. // // #ifdef MP-ALIPAY
  114. cameraContext.stopRecord();
  115. // // #endif
  116. },
  117. fail: (error) => {
  118. console.error('拍照失败:', error);
  119. emit('failReturn', error);
  120. },
  121. });
  122. } else {
  123. rotateDegree.value += 90
  124. }
  125. }
  126. //成功
  127. function success() {
  128. console.log('成功:', srcImg.value);
  129. emit('confirmReturn', {
  130. tempImagePath: srcImg.value
  131. })
  132. }
  133. //重新拍摄
  134. function camera() {
  135. emit('camera')
  136. }
  137. function cameraStop() {
  138. // 相机停止时的回调
  139. console.log('相机已停止');
  140. }
  141. function cameraError(e) {
  142. // 相机出错时的回调
  143. console.error('相机错误:', e.detail.errMsg);
  144. }
  145. const handleTouchStart = (event : TouchEvent) => {
  146. console.log('手柄触摸启动', '==1')
  147. startX.value = event.changedTouches[0].pageX - offsetX.value;
  148. startY.value = event.changedTouches[0].pageY - offsetY.value;
  149. dragging.value = true;
  150. };
  151. const handleTouchMove = (event : TouchEvent) => {
  152. console.log('手柄触摸启动', '==2')
  153. if (dragging.value) {
  154. offsetX.value = event.changedTouches[0].pageX - startX.value;
  155. offsetY.value = event.changedTouches[0].pageY - startY.value;
  156. }
  157. };
  158. const handleTouchEnd = () => {
  159. console.log('手柄触摸启动', '==3')
  160. dragging.value = false;
  161. };
  162. </script>
  163. <style>
  164. .viewfinder {
  165. pointer-events: none;
  166. position: absolute;
  167. top: 20%;
  168. left: 10%;
  169. width: 80%;
  170. height: 400rpx;
  171. border: 2px solid #fff;
  172. box-sizing: border-box;
  173. border-radius: 16rpx;
  174. z-index: 2;
  175. }
  176. .container {
  177. position: relative;
  178. height: 100vh;
  179. }
  180. </style>