選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

viewfinder.vue 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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="fileURL + 'image/index/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="fileURL + 'image/index/etc_bd_ocr_id_card_locator_back.png'" style="height: 160rpx;"
  11. mode="heightFix">
  12. </image>
  13. </view>
  14. <view v-if="phoneType == 3"
  15. style="margin: -30rpx 0 0 30rpx;display: flex;height: 100%;align-self: flex-end;flex-direction: column;justify-content: flex-end;">
  16. <view style="border: 1px solid #fff;height: 160rpx;width: 160rpx;">
  17. </view>
  18. </view>
  19. <view v-if="phoneType == 4"
  20. style="margin: -30rpx 30rpx 0 0;display: flex;height: 100%;justify-content: flex-end;align-items: flex-end;">
  21. <view style="border: 1px solid #fff;height: 60rpx;width: 300rpx;">
  22. </view>
  23. </view>
  24. <canvas canvas-id="canvasbg" id="canvasbg" style="width: 100%;height: 100%;"></canvas>
  25. </view>
  26. <!-- camera -->
  27. <camera v-if="startPhoto" 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="!startPhoto" style="height: 70vh;background-color: black;">
  30. <canvas canvas-id="canvasId" id="canvasId" style="height: 100vh;width: 100vh;"
  31. @touchstart="handleTouchStart" @touchmove="handleTouchMove" @touchend="handleTouchEnd"
  32. :style="{ transform: `translate(${offsetX}px, ${offsetY}px) rotate(${rotateDegree}deg) scale(${scale})`, transition: 'transform ' + transitionDuration + 's' }"></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 @click="camera" :src="fileURL + 'image/index/etc_bd_ocr_cancel.png'"
  38. style="width: 50rpx;height: 50rpx;"></image>
  39. <view style="flex: 1;"></view>
  40. <image @click="takePhoto" :src="fileURL + 'image/index/etc_bd_ocr_take_photo_normal.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="fileURL + 'image/index/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. import {
  58. fileURL
  59. } from "@/datas/fileURL.js";
  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. emit('confirmReturn', { tempImagePath: prop.images })
  105. }
  106. })
  107. const handleTouchStart = (event : TouchEvent) => {
  108. console.log('手柄触摸启动', '==1')
  109. if (event.touches.length === 1) {
  110. startX.value = event.changedTouches[0].pageX - offsetX.value;
  111. startY.value = event.changedTouches[0].pageY - offsetY.value;
  112. dragging.value = true;
  113. transitionDuration.value = 0.15
  114. } else if (event.touches.length === 2) {
  115. touchStartDistance.value = getTouchDistance(event.touches);
  116. transitionDuration.value = 0.5
  117. }
  118. };
  119. const handleTouchMove = (event : TouchEvent) => {
  120. console.log('手柄触摸启动', '==2')
  121. if (event.touches.length === 2) {
  122. touchMoveDistance.value = getTouchDistance(event.touches);
  123. updateScale();
  124. } else {
  125. if (dragging.value) {
  126. offsetX.value = event.changedTouches[0].pageX - startX.value;
  127. offsetY.value = event.changedTouches[0].pageY - startY.value;
  128. }
  129. }
  130. };
  131. function getTouchDistance(touches) {
  132. const x = touches[0].clientX - touches[1].clientX;
  133. const y = touches[0].clientY - touches[1].clientY;
  134. return Math.sqrt(x * x + y * y);
  135. }
  136. const handleTouchEnd = () => {
  137. console.log('手柄触摸启动', '==3')
  138. touchStartDistance.value = 0;
  139. touchMoveDistance.value = 0;
  140. dragging.value = false;
  141. };
  142. function updateScale() {
  143. // 计算缩放的增量
  144. const scales = (touchMoveDistance.value - touchStartDistance.value) * scaleStep;
  145. scale.value = Math.max(0.5, Math.min(scale.value + scales, 1)); // 设置缩放的范围,这里设置最小为1,最大为3
  146. }
  147. function takePhoto() {
  148. // #ifdef MP-ALIPAY
  149. const appAuthorizeSetting = my.getAppAuthorizeSetting();
  150. console.log("---",appAuthorizeSetting,appAuthorizeSetting['cameraAuthorized']);
  151. if(appAuthorizeSetting['cameraAuthorized']=='authorized'){
  152. if (prop.showStartPhoto) {
  153. let cameraContext = null
  154. // #ifdef MP-WEIXIN
  155. cameraContext = uni.createCameraContext();
  156. // #endif
  157. // #ifdef MP-ALIPAY
  158. cameraContext = uni.createCameraContext('camera');
  159. // #endif
  160. // 调用拍照方法
  161. cameraContext.takePhoto({
  162. quality: 'normal',
  163. success: (res : any) => {
  164. // 在这里处理拍照成功后的逻辑,res.tempImagePath 为拍照的图片路径
  165. // srcImg.value = res.tempImagePath
  166. startPhoto.value = false
  167. cameraContext.stopRecord();
  168. emit('confirmReturn', res)
  169. // getViewPosition(res.tempImagePath)
  170. return
  171. },
  172. fail: (error) => {
  173. console.error('拍照失败:', error);
  174. emit('failReturn', error);
  175. },
  176. });
  177. } else {
  178. rotateDegree.value += 90
  179. }
  180. }else{
  181. uni.showModal({
  182. title: '提示',
  183. content: '请打开摄像头权限',
  184. success: function (res) {
  185. if (res.confirm) {
  186. uni.openSetting({
  187. success(res) {
  188. console.log("2222")
  189. console.log(res.authSetting)
  190. }
  191. });
  192. } else if (res.cancel) {
  193. console.log('用户点击取消');
  194. }
  195. }
  196. });
  197. }
  198. // #endif
  199. // #ifdef MP-WEIXIN
  200. uni.authorize({
  201. scope: 'scope.camera',
  202. complete(res) {
  203. console.log("res===",res)
  204. // 打开了拍照权限
  205. if(res.errMsg=="authorize:ok"){
  206. console.log('输出内容', rotateDegree.value)
  207. if (prop.showStartPhoto) {
  208. let cameraContext = null
  209. // #ifdef MP-WEIXIN
  210. cameraContext = uni.createCameraContext();
  211. // #endif
  212. // #ifdef MP-ALIPAY
  213. cameraContext = uni.createCameraContext('camera');
  214. // #endif
  215. // 调用拍照方法
  216. cameraContext.takePhoto({
  217. quality: 'normal',
  218. success: (res : any) => {
  219. // 在这里处理拍照成功后的逻辑,res.tempImagePath 为拍照的图片路径
  220. // srcImg.value = res.tempImagePath
  221. startPhoto.value = false
  222. cameraContext.stopRecord();
  223. emit('confirmReturn', res)
  224. // getViewPosition(res.tempImagePath)
  225. return
  226. },
  227. fail: (error) => {
  228. console.error('拍照失败:', error);
  229. emit('failReturn', error);
  230. },
  231. });
  232. } else {
  233. rotateDegree.value += 90
  234. }
  235. }else{
  236. uni.showModal({
  237. title: '提示',
  238. content: '请打开摄像头权限',
  239. success: function (res) {
  240. if (res.confirm) {
  241. uni.openSetting({
  242. success(res) {
  243. console.log("2222")
  244. console.log(res.authSetting)
  245. }
  246. });
  247. } else if (res.cancel) {
  248. console.log('用户点击取消');
  249. }
  250. }
  251. });
  252. }
  253. }
  254. })
  255. // #endif
  256. }
  257. const getViewPosition = (src : any) => {
  258. const context = uni.createCanvasContext(canvasId, currentInstance.value)
  259. const query = uni.createSelectorQuery().in(currentInstance.value)
  260. // query.in(this)
  261. query.select('.container').boundingClientRect((rect : any) => {
  262. if (rect) {
  263. uni.getSystemInfo({
  264. success: (res) => {
  265. console.log(res, '===============', res.windowHeight, res.windowWidth)
  266. // 获取图片信息
  267. uni.getImageInfo({
  268. src: src,
  269. success: (imgS) => {
  270. //((res.windowHeight - res.statusBarHeight) * 0.33)
  271. console.log('====', rect, '输出内容======', imgS.width, imgS.height) //140 1.8(res.windowHeight * 0.23)
  272. // canvas.drawImage(img, 起始 x 坐标, 起始 y 坐标, 宽度, 高度, 绘制到 Canvas 的起始 x 坐标, 绘制到 Canvas 的起始 y 坐标, 裁剪后的宽度, 裁剪后的高度);
  273. 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);
  274. context.draw(false, () => {
  275. uni.canvasToTempFilePath({
  276. canvasId: canvasId,
  277. x: rect.left,
  278. y: rect.top,
  279. width: rect.width,
  280. height: rect.height,
  281. destWidth: rect.width,
  282. destHeight: rect.height,
  283. success(res : any) {
  284. // res.tempFilePath 是转换后的图片路径
  285. res.tempImagePath = res.tempFilePath
  286. emit('confirmReturn', res)
  287. },
  288. fail(err) {
  289. console.error('canvasToTempFilePath failed', err);
  290. }
  291. }, currentInstance.value)
  292. });
  293. },
  294. fail: (error) => {
  295. console.error('获取图片信息失败', error);
  296. },
  297. });
  298. }
  299. })
  300. //把当前画布指定区域的内容导出生成指定大小的图片,并返回文件路径。在自定义组件下,第二个参数传入自定义组件实例,以操作组件内 <canvas> 组件。
  301. console.log('视图的位置信息:', rect);
  302. console.log('视图的 x 坐标:', rect.left);
  303. console.log('视图的 y 坐标:', rect.top);
  304. console.log('视图的宽度:', rect.width);
  305. console.log('视图的高度:', rect.height);
  306. }
  307. }).exec();
  308. };
  309. //成功
  310. function success() {
  311. }
  312. //获取节点信息
  313. function boundingClientRect(fun : any) {
  314. const query = uni.createSelectorQuery().in(currentInstance.value)
  315. query.select('.container').boundingClientRect((rect : any) => {
  316. if (rect) {
  317. //把当前画布指定区域的内容导出生成指定大小的图片,并返回文件路径。在自定义组件下,第二个参数传入自定义组件实例,以操作组件内 <canvas> 组件。
  318. console.log('视图的位置信息:', rect);
  319. console.log('视图的 x 坐标:', rect.left);
  320. console.log('视图的 y 坐标:', rect.top);
  321. console.log('视图的宽度:', rect.width);
  322. console.log('视图的高度:', rect.height);
  323. fun(rect)
  324. }
  325. }).exec();
  326. }
  327. //重新拍摄
  328. function camera() {
  329. emit('camera')
  330. }
  331. function cameraStop() {
  332. // 相机停止时的回调
  333. console.log('相机已停止');
  334. }
  335. function cameraError(e) {
  336. // 相机出错时的回调
  337. console.error('相机错误:', e.detail.errMsg);
  338. }
  339. </script>
  340. <style>
  341. .viewfinder {
  342. pointer-events: none;
  343. position: absolute;
  344. top: 180px;
  345. left: 40px;
  346. width: 80%;
  347. height: 200px;
  348. border: 2px solid #fff;
  349. box-sizing: border-box;
  350. border-radius: 16rpx;
  351. z-index: 2;
  352. }
  353. </style>