uniapp實現身份證識別功能


通過調用百度智能雲API實現,要先獲取 access_token

創建應用獲取apiKey和SecretKey

身份證識別接口文檔https://ai.baidu.com/ai-doc/OCR/rk3h7xzck

uniapp微信小程序端調用:

<template>
    <view class="content">
        <!-- <image class="logo" src="/static/logo.png"></image> -->
        <view class="text-area">
            <text class="title">{{title}}</text>
        </view>
        <view class="" @click="getACSS_TOKEN" style="font-size: 36px;">
            gettoken
        </view>
        <view class="" @click="test">
            test
        </view>
        <image :src="base64str" mode=""></image>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                apiKey: '',
                SecretKey: '',
                base64str: ''
            }
        },
        onLoad() {
            // 在百度智能雲那邊創建一個應用后可以獲取到下面兩個參數 api Key  和  Secret Key
            this.apiKey = uni.getStorageSync('apiKey')
            this.SecretKey = uni.getStorageSync('SecretKey')
        },
        methods: {
            test() {
                let that = this
                let access_token = uni.getStorageSync('access_token')
                
                uni.chooseImage({
                    count: 1, //默認9
                    sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認二者都有
                    sourceType: ['album'], //從相冊選擇
                    success: function (res) {
                        
                        let tempFilePaths = res.tempFilePaths[0]
                        // 圖片轉 base64
                        uni.getFileSystemManager().readFile({
                            filePath: tempFilePaths, //選擇圖片返回的相對路徑  
                            encoding: 'base64', //編碼格式  
                            success: v=> { //成功的回調  
                                let base64 =  v.data // 返回的是沒有 'data:image/jpeg;base64,'頭的數據, 需要在頁面顯示圖片可自行追加上  
                                that.base64str = 'data:image/jpeg;base64,' + base64
                                
                                // 開始識別
                                uni.request({
                                    url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token,
                                    method: 'POST',
                                    data: {
                                        image: base64,
                                        id_card_side: 'back'// 身份證 正反面  front:身份證含照片的一面  back:身份證帶國徽的一面
                                    },
                                    header: {
                                        'Content-Type': 'application/x-www-form-urlencoded'
                                    },
                                    success: res => {
                                        console.log(res.data)
                                        console.log(res.data.words_result.簽發機關.words)
                                        console.log(res.data.words_result.簽發日期.words)
                                        console.log(res.data.words_result.失效日期.words)
                                    }
                                });
                             }  
                        }) 
                        
                        
                    }
                });
                
            },
            // access_token 有效期為 2592000 秒 / 30天
            getACSS_TOKEN() {
                let that = this
                uni.request({
                    url: 'https://aip.baidubce.com/oauth/2.0/token',
                    method: 'POST',
                    data: {
                        grant_type: 'client_credentials',
                        client_id: this.apiKey,// 在百度智能雲那邊創建一個應用后可以獲取
                        client_secret: this.SecretKey// 在百度智能雲那邊創建一個應用后可以獲取
                    },
                    header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    success: res => {
                        console.log(res.data)
                        uni.setStorageSync('access_token', res.data.access_token)
                        // console.log(JSON.parse(res.data))
                    }
                });
            }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

uniapp App端調用

<template>
    <view class="content">
        <!-- <image class="logo" src="/static/logo.png"></image> -->
        <view class="text-area">
            <text class="title">{{title}}</text>
        </view>
        <view class="" @click="getACSS_TOKEN" style="font-size: 36px;">
            gettoken
        </view>
        <view class="" @click="test">
            test
        </view>
        <image :src="base64str" mode=""></image>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title: 'Hello',
                apiKey: '',
                SecretKey: '',
                base64str: ''
            }
        },
        onLoad() {
            // 在百度智能雲那邊創建一個應用后可以獲取到下面兩個參數 api Key  和  Secret Key
            this.apiKey = uni.getStorageSync('apiKey')
            this.SecretKey = uni.getStorageSync('SecretKey')
        },
        methods: {
            test() {
                let that = this
                let access_token = uni.getStorageSync('access_token')
                
                uni.chooseImage({
                    count: 1, //默認9
                    sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認二者都有
                    sourceType: ['album'], //從相冊選擇
                    success: function (res) {
                        
                        let tempFilePaths = res.tempFilePaths[0]
                        // 圖片轉 base64
                        plus.io.resolveLocalFileSystemURL(tempFilePaths, function(entry) {
                            entry.file(function(file) {
                                var fileReader = new plus.io.FileReader();
                                fileReader.readAsDataURL(file);
                                fileReader.onloadend = function(evt) {
                                    //console.log(evt.target.result);
                                    that.base64str = evt.target.result// 頁面顯示圖片的src
                                    let base64 = evt.target.result.split(",")[1];// 傳遞給百度識別的 base64 格式的圖片文件
                                    
                                    // 開始識別
                                    uni.request({
                                        url: 'https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=' + access_token,
                                        method: 'POST',
                                        data: {
                                            image: base64,
                                            id_card_side: 'back'// 身份證 正反面  front:身份證含照片的一面  back:身份證帶國徽的一面
                                        },
                                        header: {
                                            'Content-Type': 'application/x-www-form-urlencoded'
                                        },
                                        success: res => {
                                            console.log(res.data)
                                            console.log(res.data.words_result.簽發機關.words)
                                            console.log(res.data.words_result.簽發日期.words)
                                            console.log(res.data.words_result.失效日期.words)
                                        }
                                    });
                                }
                            })
                        })
                        
                        
                        
                    }
                });
                
            },
            // access_token 有效期為 2592000 秒 / 30天
            getACSS_TOKEN() {
                let that = this
                uni.request({
                    url: 'https://aip.baidubce.com/oauth/2.0/token',
                    method: 'POST',
                    data: {
                        grant_type: 'client_credentials',
                        client_id: this.apiKey,// 在百度智能雲那邊創建一個應用后可以獲取
                        client_secret: this.SecretKey// 在百度智能雲那邊創建一個應用后可以獲取
                    },
                    header: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    success: res => {
                        console.log(res.data)
                        uni.setStorageSync('access_token', res.data.access_token)
                        // console.log(JSON.parse(res.data))
                    }
                });
            }
        }
    }
</script>

<style>
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
    }

    .logo {
        height: 200rpx;
        width: 200rpx;
        margin-top: 200rpx;
        margin-left: auto;
        margin-right: auto;
        margin-bottom: 50rpx;
    }

    .text-area {
        display: flex;
        justify-content: center;
    }

    .title {
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM