当前位置:   article > 正文

微信小程序调用摄像头_微信camera绘制到canvas上

微信camera绘制到canvas上

一、调用摄像头与身份证对比验证

    摄像头层级太高,要用cover-view和cover-image写按钮和icon,才能覆盖在camera上

二、代码

  1. <template>
  2. <div class="bmxt container-camera">
  3. <camera
  4. device-position="front"
  5. flash="off"
  6. bindinitdone="initdone"
  7. binderror="error"
  8. ref="canvasDOM"
  9. :style="style"
  10. ></camera>
  11. <cover-view class="camera-top">
  12. <cover-image :src="pic" alt class="camera-pic" />
  13. <cover-view class="box-camera-top">
  14. <cover-view class="text-start">{{ tip }}</cover-view>
  15. <cover-view v-if="nextBtn" class="b-btn" type="info" size="small" @click="next">下一步</cover-view>
  16. <cover-view
  17. v-else
  18. class="b-btn"
  19. type="info"
  20. size="small"
  21. :disabled="phoneDistance"
  22. @click="takePhoto"
  23. >开始验证</cover-view>
  24. <cover-view class="text-end">人脸识别上传的照片将用于身份核实</cover-view>
  25. </cover-view>
  26. <cover-view class="phone-switch phone-left">
  27. <cover-image src="/static/images/cancel.png" @click="backInfo" alt="取消" />
  28. </cover-view>
  29. </cover-view>
  30. </div>
  31. </template>
  32. <script>
  33. import { uploadFile } from '@/utils/wxrequest'
  34. import store from '@/store/store'
  35. import Toast from '@/../static/vant/toast/toast'
  36. export default {
  37. data() {
  38. return {
  39. style: '',
  40. pic: '/static/images/cicle-bg.png',
  41. tip: '请目视前方,进行人脸验证',
  42. nextBtn: false,
  43. showInfo: false,
  44. phoneDistance: false
  45. }
  46. },
  47. created() {
  48. const wh = {}
  49. const self = this
  50. wx.getSystemInfo({
  51. success: res => {
  52. wh.windowHeight = res.windowHeight / (res.windowWidth / 750)
  53. wh.screenHeight = res.screenHeight / (res.screenWidth / 750)
  54. self.cheight = 1.2 * res.screenWidth
  55. self.style = 'width: 100%; height:' + res.windowHeight + 'px; '
  56. }
  57. })
  58. },
  59. methods: {
  60. takePhoto() {
  61. console.log('拍照了')
  62. const ctx = wx.createCameraContext()
  63. const that = this
  64. ctx.takePhoto({
  65. quality: 'high',
  66. success: res => {
  67. that.pic = res.tempImagePath
  68. that.tip = '正在验证中,请稍等'
  69. that.phoneDistance = true
  70. uploadFile('/tencent/idCardFace', res.tempImagePath, 'file', {
  71. path: store.state.idCardUrl
  72. })
  73. .then(result => {
  74. if (result.result == 0) {
  75. console.log('成功')
  76. if (result.data.score > 70) {
  77. that.tip = '验证成功,请点击下一步'
  78. that.nextBtn = true
  79. } else {
  80. that.tip = '验证失败,请继续验证'
  81. that.pic = '/static/images/cicle-bg.png'
  82. }
  83. } else {
  84. console.log('失败')
  85. that.tip = '验证失败,请继续验证'
  86. that.pic = '/static/images/cicle-bg.png'
  87. }
  88. that.phoneDistance = false
  89. })
  90. .catch(err => {
  91. console.log(err)
  92. })
  93. }
  94. })
  95. },
  96. error(e) {
  97. console.log(e.detail)
  98. },
  99. next() {
  100. this.$emit('finish')
  101. },
  102. backInfo() {
  103. this.$emit('toInfo', 0)
  104. }
  105. }
  106. }
  107. </script>
  108. <style scoped>
  109. .container-camera {
  110. position: relative;
  111. }
  112. .camera-top {
  113. background-size: 100%;
  114. text-align: center;
  115. position: fixed;
  116. top: 0;
  117. left: 0px;
  118. right: 0px;
  119. bottom: 0px;
  120. z-index: 10;
  121. }
  122. .box-camera-top {
  123. margin: 430px 15px 15px;
  124. position: fixed;
  125. z-index: 10;
  126. left: 0;
  127. right: 0;
  128. }
  129. .text-start {
  130. color: #fff;
  131. font-size: 16px;
  132. margin-bottom: 30px;
  133. }
  134. .text-end {
  135. color: #ccc;
  136. font-size: 12px;
  137. margin-top: 15px;
  138. }
  139. .camera-pic {
  140. width: 100%;
  141. height: 100%;
  142. position: absolute;
  143. top: 0;
  144. left: 0px;
  145. right: 0px;
  146. bottom: 0px;
  147. z-index: 2;
  148. }
  149. .phone-switch {
  150. position: fixed;
  151. width: 40px;
  152. height: 40px;
  153. top: 15px;
  154. left: 15px;
  155. z-index: 11;
  156. }
  157. .phone-switch img {
  158. width: 100%;
  159. height: 100%;
  160. }
  161. .box-camera-top .b-btn {
  162. background-color: #1f76b3;
  163. color: #fff;
  164. width: 100%;
  165. border-radius: 50px;
  166. height: 40px;
  167. line-height: 40px;
  168. font-size: 18px;
  169. }
  170. </style>

 

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签