news 2026/6/11 17:32:50

uniapp打包为apk 接入百度OCR识别功能,利用ocr识别车牌号码(小程序与APP 都可用,亲测)。

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
uniapp打包为apk 接入百度OCR识别功能,利用ocr识别车牌号码(小程序与APP 都可用,亲测)。

1、首先注册百度ocr账号,注意要实名,才能获取免费调用额度。网址百度AI开放平台-全球领先的人工智能服务平台

2、在应用列表中创建应用

3、实名认证之后可以看到API调用次数 百度智能云控制台

4、接下来就是写代码了

首先根据API Key 和Secret Key 获取token

5、const getAccessToken = async () => {
try {
const response = await uni.request({
url: 'https://aip.baidubce.com/oauth/2.0/token',
method: 'GET',
data: {
grant_type: 'client_credentials',
client_id: 'zYEnn1E1aozKgYYyJwMVv2uD', // 替换为你自己的API Key
client_secret: 'm8DG1uWgTYFIJwGxQS2Uq9wJ5oDyNSCZ' // 替换为你自己的Secret Key
}
});
console.log('返回的数据--', response.data)
return response.data.access_token;
} catch (error) {
errorMessage.value = '获取 Access Token 失败';
console.error(error);
}
};

6、 调用ocr车辆识别方法,将拍照获取的图片上传

// 识别车牌
const recognizeLicensePlate = async (imgBase64) => {
const accessToken = await getAccessToken();
if (!accessToken) return;

try {
const response = await uni.request({
url: `https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate?access_token=${accessToken}`,
method: 'POST',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
image: imgBase64
}
});
console.log('response', response.data)

if (response.data && response.data.words_result) {
plateNumber.value = response.data.words_result.number;
} else {
errorMessage.value = '未能识别到车牌';
}
} catch (error) {
errorMessage.value = '车牌识别请求失败';
console.error(error);
}
};

7、重点来了、亲测小程序和app调用相机的方法不一样

小程序这样写:const takePhoto1 = async () => {
try {
// 选择图片,sourceType 设置为 camera 限制只能拍照
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['camera'],
success: function (res) {
console.log('res---', res.tempFilePaths[0])
//核心代码
uni.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0],
encoding: 'base64', //编码格式
success(ans) {
console.log('图片', ans.data)
// uni.showLoading({ title: '识别中' })
recognizeLicensePlate(ans.data)
}
})
}
})

} catch (error) {
errorMessage.value = '拍照失败';
console.error(error);
}
};

8\APP这样写
// 调用相机拍照
const takePhoto = async () => {
try {
// 选择图片,sourceType 设置为 camera 限制只能拍照
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['camera'],
success: function (res) {
console.log('res---', res.tempFilePaths[0]);

// 获取文件信息
uni.getFileInfo({
filePath: res.tempFilePaths[0],
success: (fileInfo) => {
console.log('文件信息:', fileInfo);
// 读取文件内容并转换为 Base64
plus.io.resolveLocalFileSystemURL(res.tempFilePaths[0], (entry) => {
entry.file((file) => {
const reader = new plus.io.FileReader();
reader.onloadend = (event) => {
const base64Data = event.target.result.split(',')[1];
console.log('图片 Base64:', base64Data);
uni.showLoading({
title:'识别中'
})
// 调用识别车牌的函数
recognizeLicensePlate(base64Data);
uni.hideLoading()
};
reader.readAsDataURL(file);
}, (error) => {
console.error('读取文件失败:', error);
errorMessage.value = '读取文件失败';
});
}, (error) => {
console.error('解析文件路径失败:', error);
errorMessage.value = '解析文件路径失败';
});
},
fail: (error) => {
console.error('获取文件信息失败:', error);
errorMessage.value = '获取文件信息失败';
}
});
},
fail: (error) => {
console.error('选择图片失败:', error);
errorMessage.value = '选择图片失败';
}
});
} catch (error) {
errorMessage.value = '拍照失败';
console.error(error);
}
};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/11 17:31:51

CC Switch终极指南:AI编程工具统一管理的完整解决方案

CC Switch终极指南:AI编程工具统一管理的完整解决方案 【免费下载链接】cc-switch A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode, OpenClaw, Gemini CLI & Hermes Agent. Only official website: ccswitch.io 项目地址: …

作者头像 李华
网站建设 2026/6/11 17:31:51

阿里云LLM算法一面实录:这些问题你能接住几个?

技术面试从来不是走过场,每一道题都在考验你的知识深度和思维韧性。下面这份来自阿里云大模型算法岗的一面复盘,或许能给你带来一些启发。 一、项目深挖 面试官对你的项目经历展开了细致追问,这不是简单的“你做了什么”,而是要…

作者头像 李华
网站建设 2026/6/11 17:28:26

百度网盘秒传技术终极指南:告别文件分享等待的革命性方案

百度网盘秒传技术终极指南:告别文件分享等待的革命性方案 【免费下载链接】rapid-upload-userscript-doc 秒传链接提取脚本 - 文档&教程 项目地址: https://gitcode.com/gh_mirrors/ra/rapid-upload-userscript-doc 你是否曾因百度网盘分享链接突然失效而…

作者头像 李华
网站建设 2026/6/11 17:27:59

浏览器3D雕塑入门:5分钟掌握SculptGL免费WebGL雕刻工具

浏览器3D雕塑入门:5分钟掌握SculptGL免费WebGL雕刻工具 【免费下载链接】sculptgl DEVELOPMENT STOPPED Im now working on Nomad Sculpt instead 项目地址: https://gitcode.com/gh_mirrors/sc/sculptgl 你是否想尝试3D雕刻却苦于专业软件的高门槛&#xff…

作者头像 李华
网站建设 2026/6/11 17:24:37

深入理解mGPT-openmind训练数据:4880亿字符的多语言语料库构建

深入理解mGPT-openmind训练数据:4880亿字符的多语言语料库构建 【免费下载链接】mGPT-openmind 项目地址: https://ai.gitcode.com/hf_mirrors/jeffding/mGPT-openmind mGPT-openmind是一个支持61种语言的多语言GPT模型,拥有13亿参数&#xff0c…

作者头像 李华
网站建设 2026/6/11 17:24:33

Nintendo Switch游戏活动分析利器:NX Activity Log完整指南

Nintendo Switch游戏活动分析利器:NX Activity Log完整指南 【免费下载链接】NX-Activity-Log Homebrew application for the Nintendo Switch which displays play activity with more accuracy 项目地址: https://gitcode.com/gh_mirrors/nx/NX-Activity-Log …

作者头像 李华