ocr.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // pages/ocr/ocr.js
  2. const tip_en = require("../../international/ocr_scence/index")
  3. const app = getApp()
  4. const upload_image = require("../../utils/upload/upload_image")
  5. const {
  6. getPassportDetail, BaiDuFaceComparison
  7. } = require("../../utils/api/make_appointment")
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. // 中英文
  14. isChinese: true,
  15. // 翻译包
  16. tip: tip_en,
  17. // 证件类型
  18. type_list: [{
  19. name_ch: '护照',
  20. name_en: 'passport',
  21. type: 13
  22. },
  23. // {name_ch: '往来港澳通行证', name_en: 'Hong Kong and Macao Travel Permit', type: 22},
  24. // {name_ch: '往来台湾通行证', name_en: 'Taiwan Travel Permit', type: 29},
  25. // {name_ch: '香港居民身份证', name_en: 'Hong Kong Identity Card', type: 1001},
  26. ],
  27. type_index: 0,
  28. type: 13,
  29. needPhoto: false,
  30. },
  31. // 选择证件类型
  32. chooseType(e) {
  33. this.setData({
  34. type_index: e.detail.value,
  35. type: this.data.type_list[e.detail.value].type
  36. })
  37. },
  38. // 提交护照验证
  39. doOCR() {
  40. var that = this;
  41. if (!this.data.type) {
  42. wx.showToast({
  43. title: this.data.isChinese ? '请先选择证件类型~' : 'Please select the id type~',
  44. icon: 'none'
  45. });
  46. return;
  47. }
  48. wx.chooseMedia({
  49. count: 1,
  50. mediaType: ['image'],
  51. sourceType: ['album', 'camera'],
  52. sizeType: ['original'],
  53. success(res) {
  54. var image = {
  55. url: res.tempFiles[0].tempFilePath
  56. }
  57. wx.showLoading({
  58. title: that.data.isChinese ? '上传中~' : 'uploading...',
  59. })
  60. //上传照片
  61. upload_image(image).then((img) => {
  62. wx.hideLoading()
  63. var data = {
  64. passportUrl: img.url,
  65. type: that.data.type
  66. }
  67. that.checkInfo(data)
  68. }).catch(() => {
  69. wx.hideLoading()
  70. })
  71. }
  72. })
  73. },
  74. //处理结果
  75. checkInfo(data) {
  76. let that = this;
  77. wx.showLoading({
  78. title: that.data.isChinese ? '识别中...' : 'recognizing..',
  79. })
  80. //ocr 识别
  81. getPassportDetail(data).then(res => {
  82. wx.hideLoading();
  83. //接口层错误
  84. if (res.code != 200) {
  85. wx.showModal({
  86. content: res.msg,
  87. showCancel: false
  88. });
  89. return;
  90. }
  91. //数据识别不完整
  92. if (!res.data.avatar) {
  93. wx.showModal({
  94. content: 'Sorry, OCR Failed. Please make sure the photo is clear and complete, then try again。',
  95. showCancel: false,
  96. });
  97. return;
  98. }
  99. //识别成功,暂存识别信息
  100. that.data.ocrInfo = res.data;
  101. that.data.ocrInfo.passportUrl = data.passportUrl;
  102. //需要抓拍人像(实名一定需要抓拍人像)
  103. if (that.data.needRealname || that.data.needPhoto) {
  104. that.data.flag = 3;
  105. wx.navigateTo({
  106. url: '/pages/faceCheck/faceCheck'
  107. });
  108. return;
  109. }
  110. //无需进行人像抓拍
  111. app.data.ocrInfo = that.data.ocrInfo;
  112. wx.navigateBack({
  113. delta: 1
  114. })
  115. }).catch(err => {
  116. wx.hideLoading()
  117. })
  118. },
  119. //比对照片
  120. comparePhoto() {
  121. const that = this;
  122. let img = app.data.catchFaceUrl;
  123. app.data.catchFaceUrl = null;
  124. //拍摄人脸照片失败
  125. if(!img) {
  126. wx.showModal({
  127. content: that.data.isChinese ? '抓拍失败, 再试一次吧' : 'Sorry,failed to capture a profile picture,pleas try again。',
  128. showCancel: false,
  129. complete: (res) => {}
  130. })
  131. return;
  132. }
  133. //拍摄人脸照片成功
  134. //无需 1:1 比对
  135. if(!this.data.needRealname) {
  136. that.data.ocrInfo.faceImage = img
  137. app.data.ocrInfo = that.data.ocrInfo;
  138. wx.navigateBack({
  139. delta: 1
  140. })
  141. return;
  142. }
  143. //需要 1:1 比对
  144. let data = {
  145. faceImage: img,
  146. cardImage: that.data.ocrInfo.avatar || '',
  147. }
  148. BaiDuFaceComparison(data).then(res => {
  149. //1:1比对成功
  150. if(res.data) {
  151. that.data.ocrInfo.faceImage = img
  152. app.data.ocrInfo = that.data.ocrInfo;
  153. wx.navigateBack({
  154. delta: 1
  155. })
  156. //1;1 比对失败
  157. } else {
  158. wx.showModal({
  159. content: that.data.isChinese ? '比对失败, 请保持光线良好,护照照片清晰,再试一次吧' : 'Sorry,the comparison failed,keep the light good and the passport clear,then try again。',
  160. showCancel: false,
  161. complete: (res) => {}
  162. })
  163. }
  164. })
  165. },
  166. /**
  167. * 生命周期函数--监听页面加载
  168. */
  169. onLoad: function (options) {
  170. this.setData({
  171. needPhoto: options.needPhoto === 'true' ? true:false,
  172. needRealname: options.needRealname === 'true'? true:false
  173. })
  174. if (app.data.language == 'ch') {
  175. this.setData({
  176. isChinese: true
  177. })
  178. } else {
  179. this.setData({
  180. isChinese: false
  181. })
  182. }
  183. // (过审专用,禁止删除)- start
  184. app.data.fake = false
  185. // (过审专用,禁止删除)- end
  186. },
  187. /**
  188. * 生命周期函数--监听页面初次渲染完成
  189. */
  190. onReady: function () {
  191. },
  192. /**
  193. * 生命周期函数--监听页面显示
  194. */
  195. onShow: function () {
  196. //抓拍照片完成返回
  197. if(this.data.flag == 3) {
  198. this.data.flag = null;
  199. this.comparePhoto()
  200. }
  201. },
  202. /**
  203. * 生命周期函数--监听页面隐藏
  204. */
  205. onHide: function () {
  206. },
  207. /**
  208. * 生命周期函数--监听页面卸载
  209. */
  210. onUnload: function () {
  211. },
  212. /**
  213. * 页面相关事件处理函数--监听用户下拉动作
  214. */
  215. onPullDownRefresh: function () {
  216. },
  217. /**
  218. * 页面上拉触底事件的处理函数
  219. */
  220. onReachBottom: function () {
  221. },
  222. /**
  223. * 用户点击右上角分享
  224. */
  225. onShareAppMessage: function () {
  226. }
  227. })