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 11KB

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