OCR识别
最近作者项目中用到了身份证识别跟营业执照的OCR识别,就研究了一下腾讯云和百度云的OCR产品接口。
腾讯云OCR
收费:身份证OCR和营业执照OCR接口,每个接口每个月各有1000次的免费调用
身份证-OCR
1.引入腾讯的SDK及JSON
1 2 3 4 5 6 7 8 9 10 11 | <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version> 3.0 . 58 </version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version> 2.4 </version> <classifier>jdk15</classifier> </dependency> |
2.前端ocr.html代码
1 2 3 4 5 6 7 8 9 | <form action= "/ocr/uploadFile" method= "POST" enctype= "multipart/form-data" > <input type= "file" name= "file" > <br /> <input type= "radio" name= "card_side" value= "FRONT" > 正面 <input type= "radio" name= "card_side" value= "BACK" > 反面 <br /> <input type= "submit" value= "提交" > </form> |
3.后端代码
说明:new Credential("secretId","secretKey"),这两个参数在腾讯云控制台申请
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | @Controller @RequestMapping ( "/ocr" ) public class OcrController { @GetMapping ( "/index" ) public String index(){ return "ocr" ; } @PostMapping ( "uploadFile" ) @ResponseBody public IDCardOCRResponse OCRIdCardTest( @RequestParam (value = "file" ) MultipartFile file, @RequestParam (value = "card_side" ) String cardSize, Model model){ try { Credential cred = new Credential( "AKIDfxxxx" , "qSq9Fxxxxx" ); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint( "ocr.tencentcloudapi.com" ); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); OcrClient client = new OcrClient(cred, "ap-beijing" , clientProfile); Map<String, String> params = new HashMap<>(); params.put( "ImageBase64" , getBase64FromInputStream(file.getInputStream())); params.put( "CardSide" , cardSize); IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(JSONObject.fromObject(params).toString(), IDCardOCRRequest. class ); IDCardOCRResponse resp = client.IDCardOCR(req); return resp; } catch (Exception e) { e.printStackTrace(); } return null ; }<br> //<strong>getBase64FromInputStream代码,把MultipartFile 转为base64</strong> public static String getBase64FromInputStream(InputStream in) { // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 byte [] data = null ; // 读取图片字节数组 try { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte [] buff = new byte [ 100 ]; int rc = 0 ; while ((rc = in.read(buff, 0 , 100 )) > 0 ) { swapStream.write(buff, 0 , rc); } data = swapStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null ) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return new String(Base64.encodeBase64(data)); } } |
运行前端html码,选择身份证图片,点击提交就可以返回身份证的信息了。
营业执照-OCR
1.前端html代码
1 2 3 4 5 | <form action= "/ocr/bizlicense" method= "POST" enctype= "multipart/form-data" > <input type= "file" name= "file" > <br /> <input type= "submit" value= "提交" > </form> |
2.后端代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @PostMapping ( "bizlicense" ) @ResponseBody public String OCRBizlicenseTest( @RequestParam (value = "file" ) MultipartFile file) throws Exception{ RestTemplate restTemplate = new RestTemplate(); String apiUrl= "https://recognition.image.myqcloud.com/ocr/bizlicense" ; HttpHeaders headers = new HttpHeaders(); headers.set( "host" , "recognition.image.myqcloud.com" ); headers.set( "content-type" , "application/json" ); String authorization=QQOCRSignUtils.appSign(XXXX, "AKIDGQfhYTqEs0DXXX" , "7adThzEEH6mKXXX" , "" , 10L); headers.set( "authorization" ,authorization ); JSONObject params = new JSONObject(); params.put( "appid" , "XXX" ); params.put( "image" , getBase64FromInputStream(file.getInputStream())); HttpEntity<JSONObject> entity = new HttpEntity<JSONObject>(params, headers); HttpEntity<String> response = restTemplate.postForEntity(apiUrl, entity, String. class ); return response.getBody(); } |
3. QQOCRSignUtils.appSign
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * 生成 Authorization 签名字段 */ public static String appSign( long appId, String secretId, String secretKey, String bucketName, long expired) throws Exception { long now = System.currentTimeMillis() / 1000 ; int rdm = Math.abs( new Random().nextInt()); String plainText = String.format( "a=%d&b=%s&k=%s&t=%d&e=%d&r=%d" , appId, bucketName, secretId, now, now + expired, rdm); byte [] hmacDigest = HmacSha1(plainText, secretKey); byte [] signContent = new byte [hmacDigest.length + plainText.getBytes().length]; System.arraycopy(hmacDigest, 0 , signContent, 0 , hmacDigest.length); System.arraycopy(plainText.getBytes(), 0 , signContent, hmacDigest.length, plainText.getBytes().length); return Base64Encode(signContent); } |
运行前端html码,选择营业执照图片,点击提交就可以返回营业执照的信息了。
百度云OCR
通过以下步骤创建OCR应用,作者当时在这一步花了很长时间
创建完之后就可以拿到appId,API Key,Secret Key,就可以调用百度提供的api了
收费:身份证OCR和营业执照OCR接口,每个接口每天各有500次的免费调用
身份证-OCR :
只列出后端的代码,前端代码跟腾讯的一样,只不过前后面身份证枚举值不一样,参考接口文档说明。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | @PostMapping ( "ocridcard" ) @ResponseBody public String OCRIdCardTest( @RequestParam (value = "file" ) MultipartFile file, @RequestParam (value = "card_side" ) String cardSize,Model model){ try { RestTemplate restTemplate = new RestTemplate(); HttpEntity<String> response = restTemplate.postForEntity( "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXXXX&client_secret=XXXXXX" , null ,String. class ); JSONObject jsonObject = JSONObject.fromObject(response.getBody()); System.out.println(response.getBody()); String accessToken = jsonObject.getString( "access_token" ); String apiUrl= "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=" +accessToken; HttpHeaders headers = new HttpHeaders(); headers.set( "content-type" , "application/x-www-form-urlencoded" ); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add( "detect_direction" , "true" ); params.add( "id_card_side" , cardSize); params.add( "image" , Base64Utils.getBase64FromInputStream(file.getInputStream())); params.add( "detect_risk" , "true" ); System.out.println(Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(URLDecoder.decode(URLEncoder.encode(Base64Utils.getBase64FromInputStream(file.getInputStream()), "UTF-8" ), "UTF-8" )); HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(params, headers); response = restTemplate.postForEntity(apiUrl, entity, String. class ); return response.getBody(); } catch (Exception e) { e.printStackTrace(); } return null ; } |
营业执照-OCR:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @PostMapping ( "ocrbusinesslicense" ) @ResponseBody public String OCRBusinessLicenseTest( @RequestParam (value = "file" ) MultipartFile file,Model model){ try { RestTemplate restTemplate = new RestTemplate(); HttpEntity<String> response = restTemplate.postForEntity( "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXXX&client_secret=XXXXXX" , null ,String. class ); JSONObject jsonObject = JSONObject.fromObject(response.getBody()); System.out.println(response.getBody()); String accessToken = jsonObject.getString( "access_token" ); String apiUrl= "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token=" +accessToken; HttpHeaders headers = new HttpHeaders(); headers.set( "content-type" , "application/x-www-form-urlencoded" ); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add( "detect_direction" , "true" ); params.add( "image" , Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(URLDecoder.decode(URLEncoder.encode(Base64Utils.getBase64FromInputStream(file.getInputStream()), "UTF-8" ), "UTF-8" )); HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(params, headers); response = restTemplate.postForEntity(apiUrl, entity, String. class ); return response.getBody(); } catch (Exception e) { e.printStackTrace(); } return null ; } |
多了解百度云和腾讯云官网,官网里面有很多api可以调用,有详细代码!
now ,fight for future
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程