scan-frame.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <template>
  2. <view class="cameraBg">
  3. <!-- position: absolute;left: 100000px; 隐藏canvas画布 -->
  4. <canvas style="width: 100%; height: 100vh;position: absolute;left: 100000px;" canvas-id="canvas" id="canvas"></canvas>
  5. <!-- 相机组件 -->
  6. <camera device-position="back" @initdone="cameraInit" flash="auto" style="width: 100%; height: 100vh">
  7. <cover-image :src="userInfo.sysUserFlag == '0'?'/static/scan-frame/cameraBg1.png':'/static/scan-frame/cameraBg.png'" class="cover-img"> </cover-image>
  8. <cover-view class="noAuth" v-if="showAuth" @click="openAuth">
  9. 您未开启摄像机权限,<cover-view class="btns">请点击开启权限</cover-view>
  10. </cover-view>
  11. <!-- <cover-view class="tips" v-if="userInfo.sysUserFlag == '0'">个人用户限10次/天</cover-view> -->
  12. <cover-view class="scanBtn" v-if="scanShow">
  13. <cover-view class="beat" @click="scan">
  14. <cover-image class="beatImg" src="@/static/scan-frame/album.png"></cover-image>
  15. <cover-view> 相册 </cover-view>
  16. </cover-view>
  17. <cover-view class="album" @click="takePhoto">
  18. <cover-image class="albumImg" src="@/static/scan-frame/beat.png"></cover-image>
  19. <cover-view> 拍照 </cover-view>
  20. </cover-view>
  21. <cover-view class="inputs" @click="inputs">
  22. <cover-image class="beatImg" src="@/static/scan-frame/inputs.png"></cover-image>
  23. <cover-view> 手动输入 </cover-view>
  24. </cover-view>
  25. </cover-view>
  26. </camera>
  27. </view>
  28. </template>
  29. <script>
  30. import {
  31. mapState,
  32. mapMutations,
  33. } from 'vuex'
  34. import { vinCode } from '@/api/car.js'
  35. export default {
  36. data() {
  37. return {
  38. scanShow: true, // 显示操作按钮
  39. sysinfo: null, // 设备信息
  40. imageInfo: null ,// 拍照图片信息
  41. showAuth: false
  42. }
  43. },
  44. onReady() {
  45. const sysinfo = uni.getSystemInfoSync()
  46. this.sysinfo = sysinfo
  47. },
  48. onShow() {
  49. const _this = this
  50. uni.getSetting({
  51. success(res) {
  52. _this.showAuth = !res.authSetting['scope.camera']
  53. }
  54. })
  55. },
  56. computed: {
  57. ...mapState(['hasLogin','vuex_vinScanNums','vuex_scanMaxNums']),
  58. userInfo(){
  59. return this.$store.state.vuex_userInfo
  60. }
  61. },
  62. methods: {
  63. cameraInit(e){
  64. this.showAuth = false
  65. },
  66. openAuth(){
  67. uni.openSetting({
  68. success(res) {
  69. if(res.authSetting['scope.camera']){
  70. uni.navigateBack()
  71. }
  72. }
  73. })
  74. },
  75. // 手动输入
  76. inputs(){
  77. uni.redirectTo({
  78. url: "/pages/vinInput/confirmVin?verifyCode=" + "&type=input"
  79. })
  80. },
  81. // 相册
  82. scan() {
  83. // 选择图片
  84. uni.chooseImage({
  85. count: 1,
  86. sizeType: ['original', 'compressed'],
  87. sourceType: ['album'],
  88. success: (res) => {
  89. this.compress(res.tempFilePaths[0],false)
  90. }
  91. })
  92. },
  93. delImgs(filePath){
  94. // #ifdef MP-WEIXIN
  95. const fs = wx.getFileSystemManager();
  96. try {
  97. const res = fs.unlinkSync(filePath)
  98. console.log(res)
  99. } catch(e) {
  100. console.error(e)
  101. }
  102. // #endif
  103. },
  104. // 裁剪图片
  105. cutImg(fileUrl){
  106. const vm = this
  107. const ctx = uni.createCanvasContext('canvas')
  108. // 将拍照的图片绘制到canvas,宽高等于窗口宽高和手机窗口保持一致
  109. ctx.drawImage(fileUrl,0,0,this.sysinfo.windowWidth,this.sysinfo.windowHeight)
  110. ctx.draw(true,()=>{
  111. const cutW = 650 // 裁剪框宽度
  112. const cutH = 160 // 裁剪框高度
  113. const cutT = 400 // 裁剪框距离顶部距离
  114. const cutL = 50 // 裁剪框距离左侧距离
  115. // 以上裁剪框宽高及坐标是相对于实际蒙板图片的位置
  116. const canvasW=cutW/750*this.sysinfo.windowWidth; // canvas 画布实际宽度
  117. const canvasH=cutH/1624*this.sysinfo.windowHeight; // canvas 画布实际高度
  118. const canvasL =cutL/750*this.sysinfo.windowWidth; // canvas 画布开始坐标x
  119. const canvasT= cutT/1624*this.sysinfo.windowHeight; // canvas 画布开始坐标y
  120. // 开始裁剪
  121. uni.canvasToTempFilePath({
  122. x: canvasL,
  123. y: canvasT,
  124. width: canvasW,
  125. height: canvasH,
  126. canvasId: 'canvas',
  127. success: function(res) {
  128. // 在H5平台下,tempFilePath 为 base64
  129. vm.parseImgs(res.tempFilePath)
  130. // 删除临时文件
  131. // vm.delImgs(fileUrl)
  132. }
  133. })
  134. })
  135. },
  136. // 启动图片压缩
  137. compress(tempFilePaths,isCut) {
  138. const vm = this
  139. vm.scanShow = false
  140. uni.showLoading({
  141. title: '智能识别中...'
  142. })
  143. uni.compressImage({
  144. src: tempFilePaths,
  145. quality: 100,
  146. success: (imageRes) => {
  147. // 获取类型
  148. uni.getImageInfo({
  149. src: imageRes.tempFilePath,
  150. success(imageInfo) {
  151. console.log(imageInfo)
  152. vm.imageInfo = imageInfo
  153. if(isCut){
  154. // 裁剪图片
  155. vm.cutImg(imageRes.tempFilePath)
  156. }else{
  157. // 相册选择的图片直接解析,这里如果需要裁剪可以跳转到裁剪图片页面在继续下一步
  158. vm.parseImgs(imageRes.tempFilePath)
  159. }
  160. }
  161. })
  162. }
  163. })
  164. },
  165. // 拿到图片转base64后在开始进行识别
  166. parseImgs(filePath) {
  167. // 图片转base64格式
  168. uni.getFileSystemManager().readFile({
  169. filePath: filePath,
  170. encoding: 'base64',
  171. success: (base) => {
  172. // 拿到base64,不需要base64 就把上层的转换去掉
  173. this.scanShow = true
  174. // 此处为后端接口 传base64图片 进行ocr识别
  175. if(this.userInfo.sysUserFlag == '0'){
  176. if(this.vuex_vinScanNums < this.vuex_scanMaxNums){
  177. this.parseVinNo(base.data)
  178. }else{
  179. uni.showModal({
  180. title: '提示',
  181. content: '个人用户扫描VIN仅限10次,您的次数已用完!',
  182. confirmText: '好的',
  183. showCancel: false,
  184. success(res) {}
  185. })
  186. }
  187. }else{
  188. this.parseVinNo(base.data)
  189. }
  190. },
  191. fail: (err) => {
  192. uni.hideLoading()
  193. console.log(err)
  194. }
  195. })
  196. },
  197. // 点击开始拍照
  198. takePhoto() {
  199. // 获取相机上下文对象
  200. const ctx = uni.createCameraContext()
  201. ctx.takePhoto({
  202. quality: 'high',
  203. success: (res) => {
  204. this.compress(res.tempImagePath,true)
  205. }
  206. })
  207. },
  208. // 识别vin
  209. parseVinNo(data) {
  210. const _this = this
  211. const base64Str = 'data:image/' + this.imageInfo.type + ';base64,' + data
  212. vinCode({
  213. imageByte: data
  214. }).then(res => {
  215. console.log(res, '识别VIN')
  216. if(res.status == 200){
  217. uni.redirectTo({
  218. url: "/pages/vinInput/confirmVin?verifyCode=123456ads11234567&filePath="+base64Str + "&verifyCode="+res.data + "&type=carmen"
  219. })
  220. }else{
  221. uni.showToast({
  222. icon: 'none',
  223. title: res.message
  224. })
  225. }
  226. uni.hideLoading()
  227. }).catch(err => {
  228. uni.hideLoading()
  229. uni.showToast({
  230. icon: 'none',
  231. title: '系统错误或超时,请重试'
  232. })
  233. })
  234. },
  235. error(e) {
  236. console.log(e.detail)
  237. }
  238. }
  239. }
  240. </script>
  241. <style lang="scss" scoped>
  242. .cameraBg {
  243. width: 100%;
  244. height: 100vh;
  245. position: fixed;
  246. .cover-img {
  247. width: 100%;
  248. height: 100vh;
  249. z-index: 1;
  250. }
  251. .noAuth,.tips{
  252. width: 100%;
  253. z-index: 999998;
  254. position: fixed;
  255. bottom: 55vh;
  256. text-align: center;
  257. color: #999;
  258. .btns{
  259. color: dodgerblue;
  260. }
  261. }
  262. .scanBtn {
  263. width: 100%;
  264. z-index: 99999;
  265. position: fixed;
  266. bottom: 100rpx;
  267. display: flex;
  268. flex-direction: column;
  269. justify-content: center;
  270. align-items: center;
  271. .beat {
  272. position: absolute;
  273. bottom: 0rpx;
  274. left: 100rpx;
  275. display: flex;
  276. flex-direction: column;
  277. justify-content: center;
  278. align-items: center;
  279. font-size: 24rpx;
  280. font-weight: 400;
  281. color: #ffffff;
  282. .beatImg {
  283. width: 88rpx;
  284. height: 88rpx;
  285. margin-bottom: 30rpx;
  286. }
  287. }
  288. .inputs{
  289. position: absolute;
  290. bottom: 0rpx;
  291. right: 100rpx;
  292. display: flex;
  293. flex-direction: column;
  294. justify-content: center;
  295. align-items: center;
  296. font-size: 24rpx;
  297. font-weight: 400;
  298. color: #ffffff;
  299. .beatImg {
  300. width: 88rpx;
  301. height: 88rpx;
  302. margin-bottom: 30rpx;
  303. }
  304. }
  305. .album {
  306. display: flex;
  307. flex-direction: column;
  308. justify-content: center;
  309. align-items: center;
  310. font-size: 24rpx;
  311. font-weight: 400;
  312. color: #ffffff;
  313. .albumImg {
  314. width: 120rpx;
  315. height: 120rpx;
  316. margin-bottom: 30rpx;
  317. }
  318. }
  319. }
  320. }
  321. </style>