您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

viewfinder.vue 10KB

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