百度ocr识别身份证信息

357 阅读1分钟
`import qs from 'qs'
`ocrIdCard(access_token,query={}) {
		const headers = {
			"Content-Type": "application/x-www-form-urlencoded"
		}
		return http.post('/baiduOCR/rest/2.0/ocr/v1/idcard?access_token='+access_token , query,{
			header:headers
		})
	},
	getOcrAcdessTokean() {
		const param = qs.stringify({
		    'grant_type': 'client_credentials',
		    'client_id': '看文档',
		    'client_secret': '看文档'
		});
		return http.post('/baiduOCR/oauth/2.0/token?' +param)
	},
        ChooseImage(access_token) {
				uni.chooseImage({
					count: this.limit, //默认9
					sizeType: this.sizeType,
					sourceType: this.sourceType, //从相册选择
					success: (res) => {
						this.loding = true
						this.imgToBase64(res,access_token);
						uni.showLoading({
							title: '上传中...'
						});
						uni.hideLoading();
					}
				});
			},
       imgToBase64(data,access_token) {
				// let that =this
				//#ifdef APP-PLUS
				console.log('1111')
				plus.zip.compressImage({
						src: data.tempFilePaths[0],
						dst: "_doc/a.jpg",
						overwrite: true,
						width: '1920px',
						height: '1080px',
						format: 'jpg',
						quality: 100
					},
				(e)=>{
					let reader = new plus.io.FileReader();
					reader.readAsDataURL(e.target);
					reader.onloadend = (e) =>{
					//这是转成功的base64文件,需要正则去一下换行
		   let base64 = e.target.result.split(',')[1].replace(/[\r\n]/g, "")
					this.OCR(base64,access_token)
						};
				},
				function(err) {
						plus.nativeUI.alert('未知错误!', function(){});
					}
				);
				//#endif
			},
      OCR(img,access_token){
				this.$http.ocrIdCard(access_token, {
					image:img,
					id_card_side: 'front',
				}).then(res => {
					const result = res.words_result
					if (result) {
						this.model.idCard = result['公民身份号码'].words
						this.model.name = result['姓名'].words
						this.model.nationalName = result['民族'].words
					}
					console.log(res, 'res++结果--------++++++++++')
				}).catch(error => {
					console.log(error, 'error++结果--------++++++++++')
				})
	},