lzc-OCR.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="box" @click="chooseImage">
  3. <image class="sfsb" mode="widthFix" :src="bSrc"></image>
  4. </view>
  5. </template>
  6. <script>
  7. let convert = require('./common/base64.js')
  8. import {
  9. translate
  10. } from '@/common/image.js'
  11. import config from '@/config.js'
  12. export default {
  13. data() {
  14. return {
  15. preSrc: '',
  16. bSrc: this.src,
  17. size: 0
  18. };
  19. },
  20. props: {
  21. src: {
  22. type: String,
  23. default: '/static/images/sfsb.png'
  24. },
  25. disabled: {
  26. type: Array,
  27. default: () => []
  28. }
  29. },
  30. mounted() {},
  31. methods: {
  32. // 选择本地图片
  33. chooseImage() {
  34. if (this.disabled.length > 0) return;
  35. uni.chooseImage({
  36. count: 1,
  37. mediaType: ['image'],
  38. sizeType: ['compressed'],
  39. sourceType: ['camera'],
  40. success: (res) => {
  41. uni.showLoading({
  42. title: '正在识别中...'
  43. })
  44. this.$emit('start', {
  45. animal: true,
  46. img: res.tempFilePaths[0]
  47. })
  48. let size = res.tempFiles[0].size
  49. let scale = 1
  50. if (size / 1024 / 1024 > 0.8) scale = 0.6
  51. translate(res.tempFilePaths[0], scale, 'blob', this.setSrc)
  52. }
  53. })
  54. },
  55. setSrc(e, blobUrl) {
  56. this.preSrc = blobUrl
  57. uni.getFileInfo({
  58. filePath: blobUrl,
  59. success: (res) => {
  60. this.size = res.size / 1024 / 1024
  61. let size = res.size
  62. let scale = 1
  63. if (size / 1024 / 1024 > 0.8) {
  64. scale = 0.6
  65. translate(this.preSrc, scale, 'blob', this.setSrc)
  66. } else {
  67. e = e.replace('data:image/jpeg;base64,', "");
  68. this.getAccessToken(e)
  69. }
  70. },
  71. fail: (err) => {
  72. console.log(err);
  73. }
  74. })
  75. },
  76. setFace(e) {
  77. },
  78. // 获取AccessToken
  79. getAccessToken(path) {
  80. uni.request({
  81. url: '/baiduApi/oauth/2.0/token',
  82. data: {
  83. grant_type: 'client_credentials',
  84. client_id: config.ocr_client_id,
  85. client_secret: config.ocr_client_secret
  86. },
  87. method: 'POST',
  88. header: {
  89. 'Content-Type': 'application/x-www-form-urlencoded'
  90. },
  91. success: (res) => {
  92. this.bSrc = this.preSrc
  93. this.uploadImage(path, res.data.access_token)
  94. }
  95. })
  96. },
  97. // 身份证识别
  98. uploadImage(path, token) {
  99. uni.request({
  100. url: '/baiduApi/rest/2.0/ocr/v1/idcard',
  101. data: {
  102. image: path,
  103. access_token: token,
  104. id_card_side: 'front',
  105. detect_photo: true,
  106. detect_risk: true,
  107. detect_card: true,
  108. },
  109. timeout: 30000,
  110. method: 'POST',
  111. header: {
  112. 'Content-Type': 'application/x-www-form-urlencoded'
  113. },
  114. success: (res) => {
  115. uni.hideLoading()
  116. this.$emit('end', res.data, this.bSrc)
  117. }
  118. })
  119. },
  120. }
  121. }
  122. </script>
  123. <style>
  124. .box {
  125. width: 100%;
  126. /* height: 100%; */
  127. }
  128. .sfsb {
  129. width: 100%;
  130. /* width: 180px; */
  131. }
  132. </style>