当前位置:   article > 正文

uni-app之接入百度OCR识别身份证(微信小程序版本)_身份证拍照信息识别小程序demo下载

身份证拍照信息识别小程序demo下载

本文为uni-app接入百度OCR识别身份证号,话不多说,直接上代码:

1. 第一步注册百度智能云账号,选择文字识别,创建应用,获取Api Key 与 Secret Key(下面要用到):

2.第二步,查看百度OCR身份证识别技术文档,如下,共需调用2个接口:

第一接口:获取百度access_token, 上代码:

  1. getBDtoken() {
  2. uni.showLoading({
  3. title: '正在连接服务...'
  4. })
  5. return new Promise(resolve => {
  6. uni.request({
  7. url: 'https://aip.baidubce.com/oauth/2.0/token', // 百度获取token接口地址
  8. data: {
  9. 'grant_type': 'client_credentials', // 固定写死
  10. 'client_id': '你上面申请获取的API Key',
  11. 'client_secret': '你上面申请的Secret Key'
  12. },
  13. method: 'GET',
  14. success(res) {
  15. resolve(res)
  16. },
  17. fail(e) {
  18. uni.hideLoading()
  19. Modal.toast('连接服务出错,请稍后再试!')
  20. }
  21. })
  22. })
  23. }

第二个接口先不要着急,请继续往下看:

3.百度OCR的第二个接口入参需要图像信息,这个图像信息怎么获取呢?上代码:

  1. takePhoto() {
  2. // 直接调用uni-app现有API, 爽翻天
  3. const ctx = uni.createCameraContext()
  4. ctx.takePhoto({
  5. quality: 'high',
  6. success: async res => {
  7. let path = res.tempImagePath // 拍照的临时地址
  8. // 这里用百度ORC身份识别的image参数,需要转成base64格式
  9. let base64String = `${wx.getFileSystemManager().readFileSync(path, "base64")}`
  10. },
  11. fail: err => {
  12. uni.hideLoading()
  13. Modal.toast('生成图片错误, 请稍后重试!')
  14. }
  15. })
  16. },

4.好了,到这里就可以调用百度OCR的第二个接口了,上代码:

  1. // 参数准备
  2. // const params = {
  3. // access_token: data.data.access_token,
  4. // data: {
  5. // image: base64String,
  6. // id_card_side: 'front'
  7. // }
  8. // }
  9. // 这个接口才是真正的识别身份证,上面准备的access_token,base64String都为此准备
  10. getIdCard(params) {
  11. uni.showLoading({
  12. title: '正在解析...'
  13. })
  14. return new Promise(resolve => {
  15. uni.request({
  16. // 身份证识别接口
  17. url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + params.access_token,
  18. data: params.data,
  19. method: 'POST',
  20. header: {
  21. "Content-Type": "application/x-www-form-urlencoded"
  22. },
  23. success(res) {
  24. uni.hideLoading()
  25. resolve(res)
  26. },
  27. fail(e) {
  28. uni.hideLoading()
  29. Modal.toast('解析出错,请稍后再试!')
  30. },
  31. complete() {
  32. }
  33. })
  34. })
  35. },

5.效果图:

6.完整代码,可以直接拿去用,不喜欢样式的自行修改:

  1. <template>
  2. <view class="container">
  3. <camera device-position="back" flash="auto" @error="error" class="camera">
  4. <cover-view class="cover-view">
  5. <cover-view class="cover-view-top"></cover-view>
  6. <cover-view class="scan-img-view">
  7. <cover-image src="../../static/img/idCard/scan-img.png" class="scan-img" id="scanImg"></cover-image>
  8. <cover-view class="tips">
  9. <cover-view>{{tips}}</cover-view>
  10. </cover-view>
  11. </cover-view>
  12. <cover-view class="cover-view-bot">
  13. <cover-image src="../../static/img/idCard/takePhoto.png" class="take-img" @click="takePhoto"></cover-image>
  14. </cover-view>
  15. <cover-view class="id-card-info" v-if="dialogVisible">
  16. <cover-view class="middle">
  17. <cover-view class="title">识别结果</cover-view>
  18. <cover-view class="content">
  19. <cover-view class="item">
  20. <cover-view class="label">姓名: </cover-view>
  21. <cover-view class="value">{{cradInfo.name}}</cover-view>
  22. </cover-view>
  23. <cover-view class="item">
  24. <cover-view class="label">出生日期: </cover-view>
  25. <cover-view class="value">{{cradInfo.birthdate}}</cover-view>
  26. </cover-view>
  27. <view class="item">
  28. <cover-view class="label">性别: </cover-view>
  29. <cover-view class="value">{{cradInfo.sex === '801' ? '男':'女'}}</cover-view>
  30. </view>
  31. <view class="item">
  32. <cover-view class="label">身份证号: </cover-view>
  33. <cover-view class="value">{{cradInfo.idNo}}</cover-view>
  34. </view>
  35. </cover-view>
  36. <cover-view class="footer">
  37. <cover-view class="btn sure">确认信息</cover-view>
  38. <cover-view class="btn cancel" @click="closeDialog">重新识别</cover-view>
  39. </cover-view>
  40. </cover-view>
  41. </cover-view>
  42. </cover-view>
  43. </camera>
  44. </view>
  45. </template>
  46. <script>
  47. let t
  48. export default {
  49. data() {
  50. return {
  51. cradInfo: {},
  52. dialogVisible: false,
  53. tips: '请将身份证置于拍照区域拍照'
  54. }
  55. },
  56. onShow() {
  57. const timer = setTimeout(() => {
  58. this.setTipsStatus()
  59. clearTimeout(timer)
  60. }, 1000)
  61. },
  62. methods: {
  63. setTipsStatus() {
  64. let num = 0
  65. t = setInterval(() => {
  66. num++
  67. if (num > 4) {
  68. num = 0
  69. }
  70. this.tips = '请将身份证置于拍照区域拍照' + '....'.substring(0, num)
  71. }, 1000)
  72. },
  73. takePhoto() {
  74. const ctx = uni.createCameraContext()
  75. ctx.takePhoto({
  76. quality: 'high',
  77. success: async res => {
  78. this.drawCanvas(res.tempImagePath)
  79. },
  80. fail: err => {
  81. uni.hideLoading()
  82. uni.showToast({
  83. title: '生成图片错误, 请稍后重试!'
  84. })
  85. }
  86. })
  87. },
  88. drawCanvas(path) {
  89. uni.showLoading({
  90. title: '正在生成图片...'
  91. })
  92. let base64String = `${wx.getFileSystemManager().readFileSync(path, "base64")}`
  93. this.getBDtoken().then(data => {
  94. const params = {
  95. access_token: data.data.access_token,
  96. data: {
  97. image: base64String,
  98. id_card_side: 'front'
  99. }
  100. }
  101. this.getIdCard(params).then(res => {
  102. if (res) {
  103. const result = res.data.words_result
  104. this.cradInfo = {
  105. idNo: result['公民身份号码'] ? result['公民身份号码'].words : '',
  106. name: result['姓名'] ? result['姓名'].words : '',
  107. sex: result['性别'] ? (result['性别'].words === '男' ? '801' : '802') : '801',
  108. birthdate: result['出生'] ? (result['出生'].words.substring(0, 4) + '-' + result['出生'].words.substring(4, 6) + '-' + result['出生'].words.substring(6, 8)) : '',
  109. }
  110. this.dialogVisible = true
  111. }
  112. })
  113. })
  114. },
  115. getBDtoken() {
  116. uni.showLoading({
  117. title: '正在连接服务...'
  118. })
  119. return new Promise(resolve => {
  120. uni.request({
  121. url: 'https://aip.baidubce.com/oauth/2.0/token',
  122. data: {
  123. 'grant_type': 'client_credentials',
  124. 'client_id': '你申请的API Key',
  125. 'client_secret': '你申请的Secret Key'
  126. },
  127. method: 'GET',
  128. success(res) {
  129. resolve(res)
  130. },
  131. fail(e) {
  132. uni.hideLoading()
  133. uni.showToast({
  134. title: '连接服务出错,请稍后再试!'
  135. })
  136. }
  137. })
  138. })
  139. },
  140. getIdCard(params) {
  141. uni.showLoading({
  142. title: '正在解析...'
  143. })
  144. return new Promise(resolve => {
  145. uni.request({
  146. url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + params.access_token,
  147. data: params.data,
  148. method: 'POST',
  149. header: {
  150. "Content-Type": "application/x-www-form-urlencoded"
  151. },
  152. success(res) {
  153. uni.hideLoading()
  154. resolve(res)
  155. },
  156. fail(e) {
  157. uni.hideLoading()
  158. uni.showToast({
  159. title: '解析出错,请稍后再试!'
  160. })
  161. },
  162. complete() {
  163. }
  164. })
  165. })
  166. },
  167. error(e) {
  168. console.log(e.detail);
  169. },
  170. closeDialog() {
  171. this.dialogVisible = false
  172. this.cradInfo = {}
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. .container{
  179. position: relative;
  180. }
  181. .camera {
  182. position: absolute;
  183. top: 0;
  184. left: 0;
  185. z-index: 99;
  186. width: 750upx;
  187. height: 100%;
  188. }
  189. .cover-view {
  190. width: 750upx;
  191. height: 100%;
  192. box-sizing: border-box;
  193. display: flex;
  194. flex-direction: column;
  195. justify-content: center;
  196. align-items: center;
  197. }
  198. .cover-view-top {
  199. width: 750upx;
  200. height: 200upx;
  201. background-color: rgba(0, 0, 0, .4);
  202. }
  203. .cover-view-bot {
  204. flex: 1;
  205. width: 750upx;
  206. background-color: rgba(0, 0, 0, .4);
  207. display: flex;
  208. justify-content: center;
  209. align-items: center;
  210. .take-img {
  211. width: 120upx;
  212. height: 120upx;
  213. }
  214. }
  215. .canvas{
  216. position: absolute;
  217. top: 240upx;
  218. left: 45upx;
  219. height: 410upx;
  220. width: 660upx;
  221. }
  222. .scan-img-view{
  223. position: relative;
  224. .scan-img {
  225. opacity: 0.4;
  226. width: 100%;
  227. height: 500upx;
  228. }
  229. .tips{
  230. position: absolute;
  231. top: 0;
  232. left: 0;
  233. bottom: 0;
  234. right: 0;
  235. display: flex;
  236. justify-content: center;
  237. align-items: center;
  238. color: #ffffff;
  239. font-size: $uni-font-size-sm;
  240. }
  241. }
  242. .id-card-info{
  243. position: absolute;
  244. top: 0;
  245. left: 0;
  246. bottom: 0;
  247. right: 0;
  248. display: flex;
  249. justify-content: center;
  250. align-items: center;
  251. background-color: rgba(0, 0, 0, .8);
  252. z-index: 999999 !important;
  253. .middle{
  254. width: 550upx;
  255. height: 600upx;
  256. background-color: #ffffff;
  257. border-radius: 20upx;
  258. display: flex;
  259. flex-direction: column;
  260. justify-content: center;
  261. align-items: center;
  262. .title{
  263. font-size: $uni-font-size-maxer;
  264. height: 100upx;
  265. line-height: 100upx;
  266. text-align: center;
  267. font-weight: bold;
  268. }
  269. .content{
  270. flex: 1;
  271. width: 100%;
  272. padding-left: 50upx;
  273. box-sizing: border-box;
  274. display: flex;
  275. flex-direction: column;
  276. justify-content: center;
  277. align-items: flex-start;
  278. .item{
  279. display: flex;
  280. margin-bottom: 20upx;
  281. font-size: $uni-font-size-lger;
  282. .label{
  283. color: $uni-text-color-grey;
  284. }
  285. .value{
  286. margin-left: 10upx;
  287. }
  288. }
  289. }
  290. .footer{
  291. display: flex;
  292. flex-direction: column;
  293. justify-content: center;
  294. align-items: center;
  295. padding: 20upx;
  296. .btn{
  297. width: 450upx;
  298. height: 80upx;
  299. border-radius: 60upx;
  300. line-height: 80upx;
  301. text-align: center;
  302. }
  303. .sure{
  304. color: #ffffff;
  305. background-color: $uni-bg-color-global;
  306. margin-bottom: 20upx;
  307. }
  308. .cancel{
  309. color: grey;
  310. background-color: $uni-bg-color-grey;
  311. }
  312. }
  313. }
  314. }
  315. </style>

有小伙伴私信我要图片,安排:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/313568
推荐阅读