百度OCR行驶证扫描识别功能

1.准备工作

(1).登陆 百度智能云-管理中心 创建 “文字识别”应用,获取 “API Key ”和 “Secret Key”:

通用参考 - 鉴权认证机制 | 百度AI开放平台 (baidu.com)

(2).根据 API Key 和 Secret Key 获取 AccessToken

文字识别OCR (baidu.com)

2.扫描图片上传

// 扫描
        setScan() {
            uni.chooseImage({
                count: 1, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                sourceType: ['camera', 'album'], //前者唤起相机,后者从相册选择
                success: res => {
                    console.log('img', res);
                    this.uploadImg(res.tempFilePaths); //上传图片
                }
            });
        },

        // 图片上传
        uploadImg(tempFilePaths) {
            let that = this;
            uni.showLoading({
                title: '识别中'
            });
           //调用后端图片上传接口
            uni.uploadFile({
                url: that.api.baseUrl + '/file/upload',
                // header:{},
                filePath: tempFilePaths[0], // 要上传文件资源的路径
                name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
                formData: {
                    user: 'test'
                },
                success(res) {
                    let data = JSON.parse(res.data);
                    console.log(data);
                    that.tui.toast('上传成功');
           //调用ocr识别行驶证方法
                    that.iOCR(data.data.url);
                },
                fail() {
                    uni.hideLoading();
                    that.tui.toast('上传失败,请重试');
                },
                complete() {
                    uni.hideLoading();
                    console.log('结束');
                }
            });
        },

3.OCR识别

iOCR(url) {
            // 生成assets token
            uni.request({
            //client_id:第一步获取到的API Key;client_secret:第一步获取的Secret Key
                url: `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=xxx&client_secret=xxx`,  
                method: 'POST',
                success: res => {
                    console.log('assets_token', res.data.access_token);
                    // 获取到access_token之后,调用ocr接口
                    uni.request({
                        url: `https://aip.baidubce.com/rest/2.0/ocr/v1/vehicle_license?access_token=${res.data.access_token}`,
                        method: 'POST',
                        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
                        data: {
                            url: url //上传之后的行驶证url
                        },
                        success: res => {
                            console.log(res);
                            if (res.data.words_result) {
                                //识别出的数据
                            } else {
                                this.tui.toast('未识别出有效数据,请重新操作');
                            }
                        },
                        fail: res => {
                            this.tui.toast('网络错误,请重试');
                        }
                    });
                }
            });
        },
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容