中山外贸营销网站建设千锋教育前端学费多少
绝对能用
1.请不要自己使用开源的OCR,识别率巨低,时间成本、优化成本太大!
2.对比网上的价格,百度的智能云更便宜。我算了下是1.6分每次
直接上代码(包含前端和后端,前端展示2种,图一是elementUI,图二是vantui的移动端。后端就是springboot)
我这里贴出来elementui的代码供你参考。
我会将关键参数抹除,你买完把参数直接放上去就能用
elementUI:
<template>
<div class="id-card-container" style="flex: 1;"><el-card class="id-card-box"><el-uploadclass="upload-demo"drag:action="uploadUrl":headers="headers":data="uploadData":on-success="handleIdCardSuccess":on-error="handleError":before-upload="beforeUpload":show-file-list="false"accept="image/*"><i class="el-icon-upload"></i><div class="el-upload__text">拖拽身份证到此或<em>点击上传</em></div><div class="el-upload__tip" slot="tip">支持JPG/PNG格式,文件大小不超过5MB</div></el-upload><div v-if="idCardLoading" class="loading-container"><el-progress type="circle" :percentage="50" :indeterminate="true"></el-progress><p>正在识别中,请稍候...</p></div><div v-if="result" class="result-container"><el-descriptions :column="1" border><el-descriptions-item label="姓名">{{ result.words_result.姓名.words || '未识别' }}</el-descriptions-item><el-descriptions-item label="身份证号">{{ result.words_result.公民身份号码.words || '未识别' }}</el-descriptions-item></el-descriptions></div></el-card></div>
</template>export default {data() {return {uploadUrl: '请输入你自己的URL/baidu/api/idcard/front',headers: {},uploadData: {},idCardLoading: false,result: null};},methods:{beforeUpload(file) {const isImage = file.type.startsWith('image/');const isLt5M = file.size / 1024 / 1024 < 5;if (!isImage) {this.$message.error('只能上传图片文件!');return false;}if (!isLt5M) {this.$message.error('图片大小不能超过5MB!');return false;}this.idCardLoading = true;this.result = null;return true;},handleIdCardSuccess(response, file, fileList) {this.idCardLoading = false;if (response.error) {this.$message.error(response.error);return;}this.result = response.data;this.optObj.userName = this.result.words_result.姓名.words;this.optObj.idcard = this.result.words_result.公民身份号码.words;this.$message.success('身份证识别成功');},handleError(err, file, fileList) {this.idCardLoading = false;console.error('上传失败:', err);this.$message.error('识别失败,请重试');}},mounted:{}
}<style scoped>
.id-card-container {max-width: 300px;margin: 20px auto;
}.id-card-box {padding: 1px;
}
.loading-container {text-align: center;margin: 3px 0;
}.result-container {margin-top: 3px;
}
</style>
JAVA-controller
@RestController
@RequestMapping("/baidu/api")
public class BaiDuImagesController {@Autowiredprivate BaiDuImagesService baiDuImagesService;/*** 识别身份证正面* @param imageFile 身份证正面图片* @return 识别结果*/@PostMapping("/idcard/front")public ResponseData recognizeFront(@RequestParam("file") MultipartFile imageFile) {JSONObject jb = BaiDuIdCardImgClient.recognizeIdCard(imageFile.getBytes());ResponseData data = new ResponseData();data.setStatus(ResponseDataStatus.SUCCESS);data.setData(jb);return data;}}
JAVA-具体方法接口
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.net.URLEncoder;
/*** @program: ic-client-postgreSQL* @description: 百度身份证识别* @author: 齐威龙* @create: 2025-04-02 14:18**/
public class BaiDuIdCardImgClient {public static final String API_KEY = "请输入你自己的API_KEY";public static final String SECRET_KEY = "请输入你自己的SECRET_KEY";static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();public static void main(String []args) throws IOException{
// recognizeIdCard(imageBytes);}public static JSONObject recognizeIdCard(byte[] imageBytes) throws IOException {MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");// 将图片转换为Base64String imgStr = Base64.getEncoder().encodeToString(imageBytes);String imgParam = URLEncoder.encode(imgStr, "UTF-8");RequestBody body = RequestBody.create(mediaType, "id_card_side=front&image="+imgParam+"&detect_ps=false&detect_risk=false&detect_quality=false&detect_photo=false&detect_card=false&detect_direction=false");Request request = new Request.Builder().url("https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" + getAccessToken()).method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").addHeader("Accept", "application/json").addHeader("Authorization", "Bearer ").build();Response response = HTTP_CLIENT.newCall(request).execute();return JSONObject.parseObject(response.body().string());}/*** 获取文件base64编码** @param path 文件路径* @param urlEncode 如果Content-Type是application/x-www-form-urlencoded时,传true* @return base64编码信息,不带文件头* @throws IOException IO异常*/static String getFileContentAsBase64(String path, boolean urlEncode) throws IOException {byte[] b = Files.readAllBytes(Paths.get(path));String base64 = Base64.getEncoder().encodeToString(b);if (urlEncode) {base64 = URLEncoder.encode(base64, "utf-8");}return base64;}/*** 从用户的AK,SK生成鉴权签名(Access Token)** @return 鉴权签名(Access Token)* @throws IOException IO异常*/static String getAccessToken() throws IOException {MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY+ "&client_secret=" + SECRET_KEY);Request request = new Request.Builder().url("https://aip.baidubce.com/oauth/2.0/token").method("POST", body).addHeader("Content-Type", "application/x-www-form-urlencoded").build();Response response = HTTP_CLIENT.newCall(request).execute();return JSONObject.parseObject(response.body().string()).getString("access_token");}}