• uniapp:使用百度API提取身份证信息(微信小程序适用)


    首先,在百度申请apiKey和SecretKey,参考官网。

    https://ai.baidu.com/ai-doc/OCR/dk3iqnq51

    想要提取身份证信息,一共需要完成三个步骤:

    1、使用apiKey和SecretKey,跟百度发起请求,获取access_token,每次申请的token有30天有效期,为了提高效率,我们只在页面加载的时候获取token,然后保存在本地,以备后面使用。

    2、通过相册或者拍照来获取图片信息,并且保存为base64格式。

    3、使用前面的token和base64,跟百度发起请求,识别图片内容,提取到身份证信息。

    开发中遇到的问题:

    1、相机问题

    关于获取图片,开始我直接用的uni.chooseImage,但是这种做法不能加相框,界面粗陋,并且每次拍照都会在手机上留下照片,后来在网上搜到自定义相机,既可以加相框,而且不会在手机上留下照片,很完美。

    2、识别速度问题

    但是接踵而来的问题是,获取token和获取图片都很快,但是识别图片内容超级慢,开始以为是我的网络问题,提高了网速后,识别速度稍微提高了一点,但依旧觉得无法接受,最后在室友的建议下,尝试修改自定义相机的quality参数,网上抄来的代码,设置的是high,也就是说图片高清,信息量可能太大,导致识别速度慢,改成low,识别依旧很慢,最后设置为normal,识别速度终于可以接受了。

    完整代码:

    注意:

    我使用了uni-icon,这个是扩展组件,这个需要另外引入,具体步骤见官网。

    虽然一直会报promise undefined错误,但其实不影响使用。

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. title: 'Hello',
    6. apiKey: 'OFZtAPc9X743qrIKZcCLEPGZ',
    7. SecretKey: 'X6UKLPcHZwpwIT92zkv6U2oLfR9xFu0k',
    8. }
    9. },
    10. onLoad() {
    11. // 每次加载页面的时候就获取token
    12. this.getAccessToken();
    13. },
    14. methods: {
    15. //打开相册
    16. openAlbum() {
    17. uni.showLoading({
    18. title: '正在获取图片...'
    19. })
    20. let that = this
    21. uni.chooseImage({
    22. count: 1, // 默认9
    23. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
    24. sourceType: ['album'], // 可以指定来源是相册还是相机,默认二者都有
    25. success: res => {
    26. uni.hideLoading()
    27. let tempFilePaths = res.tempFilePaths[0]
    28. // 图片转 base64
    29. uni.getFileSystemManager().readFile({
    30. filePath: tempFilePaths, //选择图片返回的相对路径
    31. encoding: 'base64', //编码格式
    32. success: v=> { //成功的回调
    33. let base64String = v.data
    34. that.getIdInfo(base64String)
    35. }
    36. })
    37. }
    38. })
    39. },
    40. // 点击拍照
    41. takePhoto() {
    42. uni.showLoading({
    43. title: '正在获取图片...'
    44. })
    45. let that = this
    46. const ctx = uni.createCameraContext()
    47. ctx.takePhoto({
    48. quality: 'normal',
    49. success: res => {
    50. uni.hideLoading()
    51. let base64String =
    52. `${wx.getFileSystemManager().readFileSync(res.tempImagePath, "base64")}`
    53. that.getIdInfo(base64String)
    54. }
    55. })
    56. },
    57. // access_token 有效期为 2592000 秒 / 30天,获取token
    58. getAccessToken() {
    59. uni.showLoading({
    60. title: '正在获取令牌...'
    61. })
    62. let that = this
    63. uni.request({
    64. url: 'https://aip.baidubce.com/oauth/2.0/token',
    65. method: 'POST',
    66. data: {
    67. grant_type: 'client_credentials',
    68. client_id: that.apiKey, // 在百度智能云那边创建一个应用后可以获取
    69. client_secret: that.SecretKey // 在百度智能云那边创建一个应用后可以获取
    70. },
    71. header: {
    72. 'Content-Type': 'application/x-www-form-urlencoded'
    73. },
    74. success: res => {
    75. uni.hideLoading()
    76. uni.setStorageSync('accessToken', res.data.access_token)
    77. },
    78. fail(e) {
    79. uni.hideLoading()
    80. uni.showToast({
    81. title: '连接服务出错,请稍后再试!'
    82. })
    83. }
    84. });
    85. },
    86. getIdInfo(base64String) {
    87. let accessToken = uni.getStorageSync('accessToken')
    88. uni.showLoading({
    89. title: '正在解析...'
    90. })
    91. // 开始识别
    92. uni.request({
    93. url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + accessToken,
    94. method: 'POST',
    95. data: {
    96. image: base64String,
    97. id_card_side: 'front' // 身份证 正反面 front:身份证含照片的一面 back:身份证带国徽的一面
    98. },
    99. header: {
    100. 'Content-Type': 'application/x-www-form-urlencoded'
    101. },
    102. success: res => {
    103. uni.hideLoading()
    104. console.log(res.data)
    105. uni.showToast({
    106. title: res.data.words_result.公民身份号码
    107. .words,
    108. duration: 20000
    109. });
    110. },
    111. fail(e) {
    112. uni.hideLoading()
    113. uni.showToast({
    114. title: '连接服务出错,请稍后再试!'
    115. })
    116. }
    117. });
    118. }
    119. }
    120. }
    121. script>
    122. <style>
    123. .container {
    124. background-color: #aaa;
    125. position: absolute;
    126. left: 0;
    127. right: 0;
    128. top: 0;
    129. bottom: 0;
    130. }
    131. .scan-img {
    132. width: 100%;
    133. height: 500upx;
    134. }
    135. .photo {
    136. position: absolute;
    137. bottom: 0;
    138. width: 100%;
    139. display: flex;
    140. justify-content: center;
    141. align-items: center;
    142. }
    143. /* 相册按钮使用绝对定位,保证相机按钮居中显示 */
    144. .album {
    145. position: absolute;
    146. left: 50px;
    147. }
    148. style>

    效果:

     

    相框背景图:

     

    参考文章:

    uniapp识别身份证_mwh_user的博客-CSDN博客_uniapp识别身份证 

    uniapp做微信小程序身份证识别功能(百度云身份证识别api)_Other world的博客-CSDN博客_uniapp微信小程序扫描识别身份证

    uni-app之接入百度OCR识别身份证(微信小程序版本)_iamzhizhang的博客-CSDN博客

  • 相关阅读:
    SQL 优化有哪些技巧?
    若依前端使用
    (选专业)什么性格的人适合法学类专业?mbti性格测试
    Mysql命令增加、修改、删除表字段
    nginx源码分析 -异常处理
    8.13
    【数据分类】基于麻雀搜索算法优化支持向量机的数据分类方法 SSA-SVM分类算法【Matlab代码#61】
    C++学习——函数重载详解
    无人机开发
    srdensenet
  • 原文地址:https://blog.csdn.net/lilycheng1986/article/details/127446617