组件中使用
<template>
<view class="cameraPage">
<camera device-position="back" flash="off" style="width: 100%;height: 400upx; position: relative;">
<image src="../../../../static/images/imageFile.webp" class="imageFile" @click="uploadImage" mode="">
</image>
</camera>
<button type="default" @tap="TakePhoto" class="btn_camera">拍照</button>
<!-- <image :src="photoSrc" style="width: 100%;" mode=""></image> -->
</view>
</template>
<script>
import {
pathToBase64
} from '@/pages/infoProvided/componts/cameraPage/pathToBase64.js'
import ajax from 'uni-ajax'
export default {
data() {
return {
photoSrc: '',
accessToken: '', //百度识别的token
cardType: 'front', //身份证正面
IDCardInfo: { //身份证包含的信息
name: '',
ethnic: '',
address: '',
idNumber: '',
birthday: '',
gender: '',
},
}
},
onLoad() { //监听页面加载
this.getAccessToken() //获取身份识别的 Access Token
},
methods: {
TakePhoto: function() {
const _this = this
const camera = uni.createCameraContext() //创建照相机对象
camera.takePhoto({ //实现拍照功能
quality: 'high', //high 高质量成像、 normal 普通质量、row 低质量
success: (res) => {
_this.photoSrc = res.tempImagePath
pathToBase64(_this.photoSrc).then(base64 => {
let imgBase64 = base64
_this.getIdCardInfo(imgBase64)
})
}
})
},
uploadImage: function(ocrtype) { //打开相册
var _this = this
var cardType = ""
if (ocrtype == 1) {
cardType = "front"
} else {
cardType = "back"
}
uni.chooseImage({
count: 1, //默认9
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
so