scan-frame.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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" flash="auto" style="width: 100%; height: 100vh">
  7. <cover-image src="@/static/scan-frame/cameraBg.png" class="cover-img"> </cover-image>
  8. <cover-view class="scanBtn" v-if="scanShow">
  9. <cover-view class="beat" @click="scan">
  10. <cover-image class="beatImg" src="@/static/scan-frame/album.png"></cover-image>
  11. <cover-view> 相册 </cover-view>
  12. </cover-view>
  13. <cover-view class="album" @click="takePhoto">
  14. <cover-image class="albumImg" src="@/static/scan-frame/beat.png"></cover-image>
  15. <cover-view> 拍照 </cover-view>
  16. </cover-view>
  17. <cover-view class="inputs" @click="inputs">
  18. <cover-image class="beatImg" src="@/static/scan-frame/inputs.png"></cover-image>
  19. <cover-view> 手动输入 </cover-view>
  20. </cover-view>
  21. </cover-view>
  22. </camera>
  23. </view>
  24. </template>
  25. <script>
  26. import { vinCode } from '@/api/car.js'
  27. export default {
  28. data() {
  29. return {
  30. scanShow: true, // 显示操作按钮
  31. sysinfo: null, // 设备信息
  32. imageInfo: null // 拍照图片信息
  33. }
  34. },
  35. onReady() {
  36. const sysinfo = uni.getSystemInfoSync()
  37. this.sysinfo = sysinfo
  38. },
  39. onLoad() {
  40. uni.getSetting({
  41. success(res) {
  42. console.log(res.authSetting['scope.camera'])
  43. if(!res.authSetting['scope.camera']){
  44. uni.openSetting({
  45. success(res) {
  46. console.log(res.authSetting)
  47. }
  48. })
  49. }
  50. }
  51. })
  52. },
  53. methods: {
  54. // 手动输入
  55. inputs(){
  56. uni.redirectTo({
  57. url: "/pages/vinInput/confirmVin?verifyCode="
  58. })
  59. },
  60. // 相册
  61. scan() {
  62. // 选择图片
  63. uni.chooseImage({
  64. count: 1,
  65. sizeType: ['original', 'compressed'],
  66. sourceType: ['album'],
  67. success: (res) => {
  68. this.compress(res.tempFilePaths[0],false)
  69. }
  70. })
  71. },
  72. // 裁剪图片
  73. cutImg(fileUrl){
  74. const vm = this
  75. const ctx = uni.createCanvasContext('canvas')
  76. // 将拍照的图片绘制到canvas,宽高等于窗口宽高和手机窗口保持一致
  77. ctx.drawImage(fileUrl,0,0,this.sysinfo.windowWidth,this.sysinfo.windowHeight)
  78. ctx.draw(true,()=>{
  79. const cutW = 650 // 裁剪框宽度
  80. const cutH = 160 // 裁剪框高度
  81. const cutT = 400 // 裁剪框距离顶部距离
  82. const cutL = 50 // 裁剪框距离左侧距离
  83. // 以上裁剪框宽高及坐标是相对于实际蒙板图片的位置
  84. const canvasW=cutW/750*this.sysinfo.windowWidth; // canvas 画布实际宽度
  85. const canvasH=cutH/1624*this.sysinfo.windowHeight; // canvas 画布实际高度
  86. const canvasL =cutL/750*this.sysinfo.windowWidth; // canvas 画布开始坐标x
  87. const canvasT= cutT/1624*this.sysinfo.windowHeight; // canvas 画布开始坐标y
  88. // 开始裁剪
  89. uni.canvasToTempFilePath({
  90. x: canvasL,
  91. y: canvasT,
  92. width: canvasW,
  93. height: canvasH,
  94. canvasId: 'canvas',
  95. success: function(res) {
  96. // 在H5平台下,tempFilePath 为 base64
  97. vm.parseImgs(res.tempFilePath)
  98. }
  99. })
  100. })
  101. },
  102. // 启动图片压缩
  103. compress(tempFilePaths,isCut) {
  104. const vm = this
  105. vm.scanShow = false
  106. uni.showLoading({
  107. title: '智能识别中...'
  108. })
  109. uni.compressImage({
  110. src: tempFilePaths,
  111. quality: 100,
  112. success: (imageRes) => {
  113. // 获取类型
  114. uni.getImageInfo({
  115. src: imageRes.tempFilePath,
  116. success(imageInfo) {
  117. console.log(imageInfo)
  118. vm.imageInfo = imageInfo
  119. if(isCut){
  120. // 裁剪图片
  121. vm.cutImg(imageRes.tempFilePath)
  122. }else{
  123. // 相册选择的图片直接解析,这里如果需要裁剪可以跳转到裁剪图片页面在继续下一步
  124. vm.parseImgs(imageRes.tempFilePath)
  125. }
  126. }
  127. })
  128. }
  129. })
  130. },
  131. // 拿到图片转base64后在开始进行识别
  132. parseImgs(filePath) {
  133. // 图片转base64格式
  134. uni.getFileSystemManager().readFile({
  135. filePath: filePath,
  136. encoding: 'base64',
  137. success: (base) => {
  138. // 拿到base64,不需要base64 就把上层的转换去掉
  139. this.scanShow = true
  140. // 此处为后端接口 传base64图片 进行ocr识别
  141. this.parseVinNo(base.data)
  142. },
  143. fail: (err) => {
  144. uni.hideLoading()
  145. console.log(err)
  146. }
  147. })
  148. },
  149. // 点击开始拍照
  150. takePhoto() {
  151. // 获取相机上下文对象
  152. const ctx = uni.createCameraContext()
  153. ctx.takePhoto({
  154. quality: 'high',
  155. success: (res) => {
  156. this.compress(res.tempImagePath,true)
  157. }
  158. })
  159. },
  160. // 识别vin
  161. parseVinNo(data) {
  162. const _this = this
  163. const base64Str = 'data:image/' + this.imageInfo.type + ';base64,' + data
  164. vinCode({
  165. imageByte: data
  166. }).then(res => {
  167. console.log(res, '识别VIN')
  168. uni.hideLoading()
  169. uni.redirectTo({
  170. url: "/pages/vinInput/confirmVin?verifyCode=123456ads11234567&filePath="+base64Str + "&verifyCode="+res.data
  171. })
  172. }).catch(err => {
  173. uni.hideLoading()
  174. uni.showToast({
  175. icon: 'none',
  176. title: '系统错误或超时,请重试'
  177. })
  178. })
  179. },
  180. error(e) {
  181. console.log(e.detail)
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .cameraBg {
  188. width: 100%;
  189. height: 100vh;
  190. position: fixed;
  191. .cover-img {
  192. width: 100%;
  193. height: 100vh;
  194. z-index: 1;
  195. }
  196. .scanBtn {
  197. width: 100%;
  198. z-index: 99999;
  199. position: fixed;
  200. bottom: 100rpx;
  201. display: flex;
  202. flex-direction: column;
  203. justify-content: center;
  204. align-items: center;
  205. .beat {
  206. position: absolute;
  207. bottom: 0rpx;
  208. left: 100rpx;
  209. display: flex;
  210. flex-direction: column;
  211. justify-content: center;
  212. align-items: center;
  213. font-size: 24rpx;
  214. font-weight: 400;
  215. color: #ffffff;
  216. .beatImg {
  217. width: 88rpx;
  218. height: 88rpx;
  219. margin-bottom: 30rpx;
  220. }
  221. }
  222. .inputs{
  223. position: absolute;
  224. bottom: 0rpx;
  225. right: 100rpx;
  226. display: flex;
  227. flex-direction: column;
  228. justify-content: center;
  229. align-items: center;
  230. font-size: 24rpx;
  231. font-weight: 400;
  232. color: #ffffff;
  233. .beatImg {
  234. width: 88rpx;
  235. height: 88rpx;
  236. margin-bottom: 30rpx;
  237. }
  238. }
  239. .album {
  240. display: flex;
  241. flex-direction: column;
  242. justify-content: center;
  243. align-items: center;
  244. font-size: 24rpx;
  245. font-weight: 400;
  246. color: #ffffff;
  247. .albumImg {
  248. width: 120rpx;
  249. height: 120rpx;
  250. margin-bottom: 30rpx;
  251. }
  252. }
  253. }
  254. }
  255. </style>