联系 OCR 产品技术团队申请使用 OCR 服务。技术支持钉钉群:29925041351 ,钉钉群二维码:
说明SLA 默认值保证 1 QPS,生产环境上线之前请联系产研团队评估,以及时进行稳定性保障。
业务后端集成 POP SDK ,引入 SDK POM(以 Java 为例)。详情参见 SDK 版本说明。
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-mpaas</artifactId> <version>3.0.12</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </exclusion> <exclusion> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> </exclusion> </exclusions> <version>4.5.2</version> </dependency>
使用阿里云账号的 AK/SK 和 AppId ,发起 OCR 服务调用,代码示例如下所示。
public void testPopApi() { DefaultProfile.addEndpoint("cn-hangzhou", "mPaaS", "mpaas.cn-hangzhou.aliyuncs.com"); DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "阿里云ak", "阿里云sk"); DefaultAcsClient client = new DefaultAcsClient(profile); MTRSOCRServiceRequest request = new MTRSOCRServiceRequest(); request.setAppId("提供给OCR技术团队开通的AppId"); request.setWorkspaceId("default"); try { String imageRaw = imageToBase64("图片的文件路径"); request.setImageRaw(imageRaw); request.setType("BANK_CARD_OCR"); MTRSOCRServiceResponse response = client.getAcsResponse(request); String res = response.getResult(); // res 是json字符串,可提供其中的关键信息返回给客户端 JSONObject jsonObj = JSON.parseObject(res); // 这里可以自定义模型Model,再使用 JSON.parseObject(res, Model.class) 反序列化为 Model JSONObject attrObj = jsonObj.getJSONObject("attributes"); String resStr = attrObj.getString("result"); System.out.println(resStr); } catch (Exception e) { logger.error("testPopApi call exception", e); } } public static String imageToBase64(String imagePath) throws IOException { File file = new File(imagePath); FileInputStream fis = new FileInputStream(file); byte[] bytes = new byte[(int) file.length()]; fis.read(bytes); fis.close(); return Base64.getEncoder().encodeToString(bytes); }
该文章对您有帮助吗?