You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viewfinder.vue 8.6KB

1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <template>
  2. <view style="width: 100%;height: 100vh;position: fixed;left: 0;top:0;z-index: 111111;">
  3. <view ref="targetView" class="container viewfinder">
  4. <view v-if="phoneType == 1"
  5. style="display: flex;align-items: center;justify-content: flex-end;width: 100%;height: 100%;">
  6. <image src="/static/image/etc_bd_ocr_id_card_locator_front.png"
  7. style="height: 220rpx;margin-right: 30rpx;" mode="heightFix"></image>
  8. </view>
  9. <view v-if="phoneType == 2" style="margin: 30rpx 0 0 30rpx;">
  10. <image src="/static/image/etc_bd_ocr_id_card_locator_back.png" style="height: 160rpx;" mode="heightFix">
  11. </image>
  12. </view>
  13. <view v-if="phoneType == 3"
  14. style="margin: -30rpx 0 0 30rpx;display: flex;height: 100%;align-self: flex-end;flex-direction: column;justify-content: flex-end;">
  15. <view style="border: 1px solid #fff;height: 160rpx;width: 160rpx;">
  16. </view>
  17. </view>
  18. <view v-if="phoneType == 4"
  19. style="margin: -30rpx 30rpx 0 0;display: flex;height: 100%;justify-content: flex-end;align-items: flex-end;">
  20. <view style="border: 1px solid #fff;height: 60rpx;width: 300rpx;">
  21. </view>
  22. </view>
  23. </view>
  24. <!-- camera -->
  25. <camera v-if="showStartPhoto" id="camera" style="height: 70vh;width: 100vh;background-color: black;width: 100%;"
  26. mode="normal" :device-position="cameraPosition" :flash="flash" @stop="cameraStop" @error="cameraError" />
  27. <view v-if="!startPhoto" style="height: 70vh;background-color: black;">
  28. <!-- <image style="position: absolute;top: 20%;left: 10%;touch-action: none;" :src="srcImg"
  29. @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"
  30. :style="{ transform: `translate(${offsetX}px, ${offsetY}px) rotate(${rotateDegree}deg) scale(${scale})`, transition: 'transform ' + transitionDuration + 's' }">
  31. </image> -->
  32. <canvas canvas-id="canvasId" id="canvasId" style="height: 98vh;width: 100vh;"></canvas>
  33. </view>
  34. <view style="background-color: #A5A5A5;top: 60%;width: 100%;height: 40%;position: absolute;">
  35. <view style="display: flex;flex-direction: row;justify-content: center;align-items: center;height: 100%;">
  36. <view style="flex: 1;"></view>
  37. <image v-if="!showStartPhoto" @click="camera" src="/static/image/etc_bd_ocr_cancel.png"
  38. style="width: 50rpx;height: 50rpx;"></image>
  39. <view style="flex: 1;"></view>
  40. <image @click="takePhoto"
  41. :src="showStartPhoto ? '/static/image/etc_bd_ocr_take_photo_normal.png': '/static/etc_bd_ocr_rotate.png'"
  42. :style="showStartPhoto ? 'width: 140rpx;height: 140rpx;' : 'width: 80rpx;height: 80rpx;'"></image>
  43. <view style="flex: 1;"></view>
  44. <image v-if="!showStartPhoto" @click="success" src="/static/image/etc_bd_ocr_confirm.png"
  45. style="width: 50rpx;height: 50rpx;"></image>
  46. <view style="flex: 1;"></view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script setup lang="ts">
  52. import {
  53. ref
  54. } from 'vue'
  55. let prop = defineProps({
  56. phoneType: {
  57. type: Number,
  58. default: function () {
  59. return 3 //1 身份证正面 2 身份证反面 3 行驶证正面 4 行驶证反面
  60. }
  61. },
  62. showStartPhoto: {
  63. type: Boolean,
  64. default: function () {
  65. return true
  66. }
  67. },
  68. images: {
  69. type: String
  70. }
  71. });
  72. console.log('输出内容', prop.phoneType)
  73. const emit = defineEmits<{
  74. (e : "confirmReturn", content : any) : void;
  75. (e : "failReturn", content : any) : void;
  76. (e : "camera") : void;
  77. }>();
  78. let cameraPosition = 'back' // 'back'为后置摄像头,'front'为前置摄像头
  79. let flash = 'off' // 'off'为关闭闪光灯,'on'为打开闪光灯
  80. let scaleStep = 0.5 //每次缩放的最大增量
  81. let srcImg = ref(prop.images)
  82. let rotateDegree = ref(0)
  83. let touchStartDistance = ref(0)
  84. let touchMoveDistance = ref(0)
  85. let scale = ref(1)
  86. let transitionDuration = ref(0)
  87. let startPhoto = ref(prop.showStartPhoto)
  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. const canvasId = 'canvasId';
  94. const targetView = ref();
  95. const handleTouchStart = (event : TouchEvent) => {
  96. console.log('手柄触摸启动', '==1')
  97. if (event.touches.length === 1) {
  98. startX.value = event.changedTouches[0].pageX - offsetX.value;
  99. startY.value = event.changedTouches[0].pageY - offsetY.value;
  100. dragging.value = true;
  101. transitionDuration.value = 0.15
  102. } else if (event.touches.length === 2) {
  103. touchStartDistance.value = getTouchDistance(event.touches);
  104. transitionDuration.value = 0.5
  105. }
  106. };
  107. const handleTouchMove = (event : TouchEvent) => {
  108. console.log('手柄触摸启动', '==2')
  109. if (event.touches.length === 2) {
  110. touchMoveDistance.value = getTouchDistance(event.touches);
  111. updateScale();
  112. } else {
  113. if (dragging.value) {
  114. offsetX.value = event.changedTouches[0].pageX - startX.value;
  115. offsetY.value = event.changedTouches[0].pageY - startY.value;
  116. }
  117. }
  118. };
  119. function getTouchDistance(touches) {
  120. const x = touches[0].clientX - touches[1].clientX;
  121. const y = touches[0].clientY - touches[1].clientY;
  122. return Math.sqrt(x * x + y * y);
  123. }
  124. const handleTouchEnd = () => {
  125. console.log('手柄触摸启动', '==3')
  126. touchStartDistance.value = 0;
  127. touchMoveDistance.value = 0;
  128. dragging.value = false;
  129. };
  130. function updateScale() {
  131. // 计算缩放的增量
  132. const scales = (touchMoveDistance.value - touchStartDistance.value) * scaleStep;
  133. scale.value = Math.max(0.5, Math.min(scale.value + scales, 1)); // 设置缩放的范围,这里设置最小为1,最大为3
  134. }
  135. function takePhoto() {
  136. console.log('输出内容', rotateDegree.value)
  137. if (prop.showStartPhoto) {
  138. let cameraContext = null
  139. // #ifdef MP-WEIXIN
  140. cameraContext = uni.createCameraContext();
  141. // #endif
  142. // #ifdef MP-ALIPAY
  143. cameraContext = uni.createCameraContext('camera');
  144. // #endif
  145. // 调用拍照方法
  146. cameraContext.takePhoto({
  147. quality: 'normal',
  148. success: (res : any) => {
  149. // 在这里处理拍照成功后的逻辑,res.tempImagePath 为拍照的图片路径
  150. // srcImg.value = res.tempImagePath
  151. startPhoto.value = false
  152. cameraContext.stopRecord();
  153. getViewPosition(res.tempImagePath)
  154. console.log('拍照成功:', res.tempImagePath);
  155. },
  156. fail: (error) => {
  157. console.error('拍照失败:', error);
  158. emit('failReturn', error);
  159. },
  160. });
  161. } else {
  162. rotateDegree.value += 90
  163. }
  164. // console.log('输出内容', '===123')
  165. }
  166. const getViewPosition = (src : any) => {
  167. const context = uni.createCanvasContext(canvasId)
  168. const query = uni.createSelectorQuery();
  169. console.log('输出内容===========123')
  170. // query.in(this)
  171. query.select('.container').boundingClientRect((rect : any) => {
  172. console.log(rect, '==========', targetView.value)
  173. if (rect) {
  174. // showStartPhoto.value = false
  175. // canvas.drawImage(img, 起始 x 坐标, 起始 y 坐标, 宽度, 高度, 绘制到 Canvas 的起始 x 坐标, 绘制到 Canvas 的起始 y 坐标, 裁剪后的宽度, 裁剪后的高度);
  176. context.drawImage(src, rect.left, rect.top, rect.width, rect.height, rect.left, rect.top, rect.width, rect.height);
  177. context.draw(false, () => {
  178. uni.canvasToTempFilePath({
  179. canvasId: canvasId,
  180. success(res : any) {
  181. console.log('成功', res)
  182. // res.tempFilePath 是转换后的图片路径
  183. console.log(res.tempFilePath);
  184. srcImg.value = res.tempImagePath
  185. // showStartPhoto.value = false
  186. emit('confirmReturn', res)
  187. },
  188. fail(err) {
  189. console.error('canvasToTempFilePath failed', err);
  190. }
  191. })
  192. });
  193. //把当前画布指定区域的内容导出生成指定大小的图片,并返回文件路径。在自定义组件下,第二个参数传入自定义组件实例,以操作组件内 <canvas> 组件。
  194. console.log('视图的位置信息:', rect);
  195. console.log('视图的 x 坐标:', rect.left);
  196. console.log('视图的 y 坐标:', rect.top);
  197. console.log('视图的宽度:', rect.width);
  198. console.log('视图的高度:', rect.height);
  199. }
  200. }).exec();
  201. };
  202. //成功
  203. function success() {
  204. console.log('图片地址', srcImg.value);
  205. emit('confirmReturn', {
  206. tempImagePath: srcImg.value
  207. })
  208. }
  209. //重新拍摄
  210. function camera() {
  211. emit('camera')
  212. }
  213. function cameraStop() {
  214. // 相机停止时的回调
  215. console.log('相机已停止');
  216. }
  217. function cameraError(e) {
  218. // 相机出错时的回调
  219. console.error('相机错误:', e.detail.errMsg);
  220. }
  221. </script>
  222. <style>
  223. .viewfinder {
  224. pointer-events: none;
  225. position: absolute;
  226. top: 20%;
  227. left: 10%;
  228. width: 80%;
  229. height: 400rpx;
  230. border: 2px solid #fff;
  231. box-sizing: border-box;
  232. border-radius: 16rpx;
  233. z-index: 2;
  234. }
  235. /* .content {
  236. position: absolute;
  237. top: 50%;
  238. left: 50%;
  239. transform: translate(-50%, -50%);
  240. }
  241. */
  242. </style>