index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <view>
  3. <nav-bar title="身份证"></nav-bar>
  4. <div class="camera-wrap">
  5. <camera ref="camera" resolution="high" class="camera" frame-size="small" device-position="back" flash="off" @error="error"></camera>
  6. <image class="avatar" :src="face ? '../static/avatar.png' : '../static/back.png' "></image>
  7. </div>
  8. <canvas class="canvas" canvas-id="canvas1"></canvas>
  9. <canvas class="canvas" canvas-id="canvas"></canvas>
  10. <view>精度{{point}}</view>
  11. <view>人脸面符合次数{{frontIsCard}}</view>
  12. <view>国徽面合次数{{backIsCard}}</view>
  13. <!-- <image class="img" :src="src" mode="widthFix"></image> -->
  14. <button @click="takePhoto(true)">人脸面</button>
  15. <button @click="takePhoto(false)">国徽面</button>
  16. </view>
  17. </template>
  18. <script>
  19. import { mapGetters } from 'vuex';
  20. import { templateFront, templateBack } from '../../common/isCardTemplate.js'
  21. export default {
  22. data() {
  23. return {
  24. face:true,
  25. point:0,
  26. context:null,
  27. src:"",
  28. isCompare:false,
  29. frontIsCard:0,
  30. backIsCard:0,
  31. frontData:templateFront, //身份证人脸面模板特征值
  32. backData:templateBack //身份证国徽面模板特征值
  33. };
  34. },
  35. computed: { ...mapGetters(['userInfo','goodsAuditionConfigIdList','playSectionId']) },
  36. onLoad(option) {
  37. },
  38. onUnload(option) {
  39. },
  40. mounted() {
  41. this.context = uni.createCanvasContext('canvas')
  42. const ctx = uni.createCameraContext();
  43. const listener = ctx.onCameraFrame(frame => {
  44. if(!this.isCompare) {
  45. this.isCompare = true;
  46. uni.getSystemInfo({
  47.   success: (res) => { // res - 各种参数
  48.    let info = uni.createSelectorQuery().select(".canvas");
  49.      info.boundingClientRect((data1) => { //data - 各种参数
  50. var buffer = new Uint8ClampedArray(frame.data);
  51. //获取是实时拍照原图
  52. uni.canvasPutImageData({
  53. canvasId: 'canvas',
  54. x: 0,
  55. y: 0,
  56. width: frame.width,
  57. height: frame.height,
  58. data: buffer,
  59. success:(res) => {
  60. //生成图片
  61. uni.canvasToTempFilePath({
  62. quality:1,
  63. x: 0,
  64. y: 0,
  65. width: frame.width,
  66. height: frame.height,
  67. destWidth:data1.width,
  68. destHeight:data1.height,
  69. canvasId: 'canvas',
  70. success: (res) => {
  71. // 在H5平台下,tempFilePath 为 base64
  72. //绘制原图到canvas上
  73. this.context.drawImage(res.tempFilePath,0,0,data1.width,data1.height)
  74. this.context.draw();
  75. let avatarInfo = uni.createSelectorQuery().select(".avatar");
  76. //获取原图扫描框大小
  77. avatarInfo.boundingClientRect((data) => {
  78. // //data - 各种参数
  79. let left = data.left;
  80. let top = data.top;
  81. let width = data.width;
  82. let height = data.height;
  83. //获取原图扫描框内容
  84. uni.canvasGetImageData({
  85. canvasId: 'canvas',
  86. x: left,
  87. y: top,
  88. width: width,
  89. height: height,
  90. success:(res) => {
  91. // this.isCompare = false;
  92. //绘制扫描图片
  93. uni.canvasPutImageData({
  94. canvasId: 'canvas',
  95. x: 0,
  96. y: 0,
  97. width: width,
  98. height: height,
  99. data: res.data,
  100. success:(res) => {
  101. //生成图片
  102. uni.canvasToTempFilePath({
  103. quality:1,
  104. x: 0,
  105. y: 0,
  106. destWidth:width,
  107. destHeight:height,
  108. width: width,
  109. height: height,
  110. canvasId: 'canvas',
  111. success:async (res1) => {
  112. //绘制并压缩
  113. this.context.drawImage(res1.tempFilePath,0,0,width,height,0,0,200,200)
  114. this.context.draw();
  115. let res = await this.canvasGetImageData('canvas');
  116. let newData = Array(res.data.length)
  117. newData.fill(0)
  118. res.data.forEach((_data, index) => {
  119. if ((index + 1) % 4 === 0) {
  120. const R = res.data[index - 3]
  121. const G = res.data[index - 2]
  122. const B = res.data[index - 1]
  123. const gray = ~~((R + G + B) / 3)
  124. res.data[index - 3] = gray
  125. res.data[index - 2] = gray
  126. res.data[index - 1] = gray
  127. res.data[index] = 255 // Alpha 值固定为255
  128. }
  129. })
  130. uni.canvasPutImageData({
  131. canvasId: 'canvas1',
  132. x: 0,
  133. y: 0,
  134. width: res.width,
  135. height: res.height,
  136. data: res.data,
  137. success:(res) => {
  138. // this.isCompare = false;
  139. uni.canvasToTempFilePath({
  140. destWidth:res.width,
  141. destHeight:res.height,
  142. quality:1,
  143. x: 0,
  144. y: 0,
  145. width: width,
  146. height: height,
  147. canvasId: 'canvas1',
  148. success: (res2) => {
  149. this.src = res2.tempFilePath
  150. },
  151. })
  152. },
  153. fail:err => {
  154. console.log(err,'err')
  155. }
  156. })
  157. // for (let i = 0; i < newData.length; i += 4) {
  158. // let R = newData[i]
  159. // let G = newData[i + 1]
  160. // let B = newData[i + 2]
  161. // let Alpha = newData[i + 3]
  162. // }
  163. let getData = this.getHashFingerprint(res)
  164. let similarty = this.cosineSimilarity(this.face ? this.frontData : this.backData,getData)
  165. // let distance = this.hammingDistance(this.frontData,result)
  166. // let similarty = (this.frontData.length - distance) / this.frontData.length
  167. if(similarty >= 0.8) {
  168. this.src = res1.tempFilePath
  169. if(this.face) {
  170. this.frontIsCard++;
  171. } else {
  172. this.backIsCard++;
  173. }
  174. }
  175. this.point = similarty
  176. setTimeout(() => {
  177. this.isCompare = false;
  178. },200)
  179. console.log(similarty,'similarty')
  180. },
  181. })
  182. },
  183. })
  184. }
  185. })
  186. }).exec()
  187. }
  188. })
  189. },
  190. fail(err) {
  191. console.log(err)
  192. }
  193. })
  194.    }).exec()
  195. }
  196. });
  197. }
  198. })
  199. listener.start()
  200. },
  201. methods: {
  202. error(err) {
  203. console.log(err)
  204. },
  205. canvasGetImageData(id) {
  206. return new Promise(resolve => {
  207. uni.canvasGetImageData({
  208. canvasId: id,
  209. x: 0,
  210. y: 0,
  211. width: 200,
  212. height: 200,
  213. success:(res) => {
  214. resolve(res)
  215. },
  216. })
  217. })
  218. },
  219. hammingDistance (str1, str2) {
  220. let distance = 0
  221. const str1str1Arr = str1.split('')
  222. const str2str2Arr = str2.split('')
  223. str1str1Arr.forEach((letter, index) => {
  224. if (letter !== str2str2Arr[index]) {
  225. distance++
  226. }
  227. })
  228. return distance
  229. } ,
  230. cosineSimilarity (sampleFingerprint, targetFingerprint) {
  231. // cosθ = ∑n, i=1(Ai × Bi) / (√∑n, i=1(Ai)^2) × (√∑n, i=1(Bi)^2) = A · B / |A| × |B|
  232. const length = sampleFingerprint.length
  233. let innerProduct = 0
  234. for (let i = 0; i < length; i++) {
  235. innerProduct += sampleFingerprint[i] * targetFingerprint[i]
  236. }
  237. let vecA = 0
  238. let vecB = 0
  239. for (let i = 0; i < length; i++) {
  240. vecA += sampleFingerprint[i] ** 2
  241. vecB += targetFingerprint[i] ** 2
  242. }
  243. const outerProduct = Math.sqrt(vecA) * Math.sqrt(vecB)
  244. return innerProduct / outerProduct
  245. } ,
  246. getHashFingerprint (imgData) {
  247. const grayList = imgData.data.reduce((pre, cur, index) => {
  248. if ((index + 1) % 4 === 0) {
  249. pre.push(imgData.data[index - 1])
  250. }
  251. return pre
  252. }, [])
  253. const length = grayList.length
  254. const grayAverage = grayList.reduce((pre, next) => (pre + next), 0) / length
  255. return grayList.map(gray => (gray >= grayAverage ? 1 : 0)).join('')
  256. } ,
  257. takePhoto(bo) {
  258. this.face = bo
  259. // const ctx = uni.createCameraContext();
  260. // ctx.takePhoto({
  261. // quality: 'high',
  262. // success: res => {
  263. // this.src = res.tempImagePath;
  264. // },
  265. // fail: err => {
  266. // console.log(err);
  267. // }
  268. // });
  269. }
  270. }
  271. };
  272. </script>
  273. <style >
  274. page{
  275. background-color: #EAEEF1;
  276. }
  277. </style>
  278. <style scope lang="scss">
  279. .camera-wrap {
  280. position: relative;
  281. .camera {
  282. width: 750rpx;
  283. height: 916.67rpx;
  284. }
  285. .avatar {
  286. opacity: 0.3;
  287. position:absolute;
  288. left:50%;
  289. top:40rpx;
  290. transform: translateX(-50%);
  291. width:633rpx;
  292. height:400rpx;
  293. }
  294. }
  295. .canvas {
  296. position:fixed;
  297. top:9999999%;
  298. left:9999999%; //canvas藏起来
  299. width: 750rpx;
  300. height: 916.67rpx;
  301. }
  302. .img {
  303. display: block;
  304. margin:0 auto;
  305. width:633rpx;
  306. height:400rpx;
  307. }
  308. </style>