index.vue 9.9 KB

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