html:

<!-- 拍照识别 -->
		<uni-popup ref="photograph" type="center" :animation="false">
			<view class="popup-content photographCenter"
				:style="'height: '+scrollheights+'px;box-sizing: border-box;border-radius:0'">
				<uni-icons @click="$refs.photograph.close()" type="back" color="#fff" size="24"></uni-icons>
				<view class="waper flex-align-center">
					<view class="noticeTXT">请将身份证正面放入框内</view>
					<camera mode="normal" device-position="back" flash="auto" @error="error" @initdone="initdone"
						style="width: 100%; height: 400rpx">
						<cover-view class="controls">
							<cover-image v-show="coverImgFlag" class="img"
								src="@/static/images/20210126144225906.png" />
						</cover-view>
					</camera>
				</view>
				<view class="distinguish_click">
					<view class="distinguish_click_item" @click="openAlbum">
						<uni-icons type="image" color="#fff" size="30"></uni-icons>
						<text class="text">相册</text>
					</view>
					<uni-icons @click="distinguish" type="camera-filled" color="#fff" size="80"></uni-icons>
				</view>
			</view>
		</uni-popup>

css代码: 

.waper {
		width: 100%;
		height: 40vh;
		align-items: center;
		flex-direction: column;
	}

	.controls {
		position: relative;
		top: 0;
		display: flex;
		align-items: center;

		.img {
			width: 100%;
			height: 404rpx;
		}
	}

	.noticeTXT {
		text-align: center;
		margin-bottom: 100rpx;
		color: #fff;
		font-size: 40rpx;
		margin-top: 30rpx;
	}

	.takePhoto {
		width: 90%;
		margin: 0 auto
	}

	.flex-align-center {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.bg-brown {
		color: #fff;
		background: linear-gradient(153deg, rgba(225, 164, 70, 1) 0%, rgba(195, 144, 65, 1) 100%);
	}

	.distinguish_click {
		position: absolute;
		bottom: 4%;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.distinguish_click_item {
		position: absolute;
		left: 6%;
		display: flex;
		flex-direction: column;

		.text {
			color: #fff;
			margin-top: 4rpx;
		}
	}

 js代码:

//打开相册
			openAlbum() {
				uni.chooseImage({
					count: 1, // 默认9  
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有  
					sourceType: ['album'], // 可以指定来源是相册还是相机,默认二者都有  
					success: res => {
						console.log(res.tempFilePaths[0]);
						this.pic = res.tempFilePaths[0]
						uni.showLoading({
							title: '识别中'
						});
						this.isBase64Img(res.tempFilePaths[0])
					}
				})
			},
			//点击拍照
			distinguish() {
				const ctx = wx.createCameraContext()
				ctx.takePhoto({
					quality: 'high',
					success: (res) => {
						//res.tempImagePath为拍取的相片
						this.pic = res.tempImagePath
						uni.showLoading({
							title: '识别中'
						});
						this.isBase64Img(res.tempImagePath)
					}
				})
			},
			//对图片进行base64转码
			isBase64Img(base64) {
				wx.getFileSystemManager().readFile({
					filePath: base64,
					encoding: 'base64', //编码格式
					success: res => {
						// console.log(res.data);
						this.$http({
							url: 'api/travel/usercard_checking',
							data: {
								img_path: res.data
							}
						}).then(res => {
							uni.hideLoading();
							if(res.data.status==200){
								this.userInfo.user_name = res.data.data.data.truename
								this.userInfo.user_card = res.data.data.data.num
								uni.showToast({
									title: '识别成功',
									icon: 'none',
									duration: 2000
								});
							}else{
								uni.showToast({
									title: res.data.message,
									icon: 'none',
									duration: 2000
								});
							}
							this.$refs.photograph.close();
						})
					}
				})
			},

效果图:

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐