写阿里服务辨认车牌号功能碰到的bug【包含使用阿里服务辨认车牌号功能代码 ...

打印 上一主题 下一主题

主题 969|帖子 969|积分 2907

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Exception in thread "main" java.lang.NoSuchMethodError: com.aliyun.credentials.Client.getCredential()Lcom/aliyun/credentials/models/CredentialModel;
  2.         at com.aliyun.teaopenapi.Client.doRequest(Client.java:812)
  3.         at com.aliyun.teaopenapi.Client.callApi(Client.java:1080)
  4.         at com.aliyun.openplatform20191219.Client.authorizeFileUploadWithOptions(Client.java:46)
  5.         at com.aliyun.ocr20191230.Client.recognizeLicensePlateAdvance(Client.java:770)
  6.         at com.utils.RecognizeLicensePlateAli.main(RecognizeLicensePlateAli.java:59)
复制代码
其实解决bug很简单,是缺了个包,而且这个是运行时异常
  1.   <dependency>
  2.             <groupId>com.aliyun</groupId>
  3.             <artifactId>tea-openapi</artifactId>
  4.             <version>0.2.1</version>
  5.         </dependency>
  6.         <!-- 还有这个识别用的包 -->
  7.         <dependency>
  8.             <groupId>com.aliyun</groupId>
  9.             <artifactId>ocr20191230</artifactId>
  10.             <version>2.0.1</version>
  11.         </dependency>
复制代码
  1. package com.utils;
  2. /*
  3. 引入依赖包
  4. <!-- https://mvnrepository.com/artifact/com.aliyun/ocr20191230 -->
  5. <dependency>
  6.       <groupId>com.aliyun</groupId>
  7.       <artifactId>ocr20191230</artifactId>
  8.       <version>${aliyun.ocr.version}</version>
  9. </dependency>
  10. */
  11. import cn.hutool.core.collection.CollUtil;
  12. import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponse;
  13. import com.aliyun.ocr20191230.models.RecognizeLicensePlateResponseBody;
  14. import com.aliyun.tea.TeaException;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import java.io.File;
  17. import java.io.InputStream;
  18. import java.nio.file.Files;
  19. import java.util.List;
  20. import java.util.Optional;
  21. public class RecognizeLicensePlateAli {
  22.     public static String accessKeyId = "ABC123456";
  23.     public static String accessKeySecret = "ABC123456";
  24.     public static com.aliyun.ocr20191230.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
  25.         /*
  26.           初始化配置对象com.aliyun.teaopenapi.models.Config
  27.           Config对象存放 AccessKeyId、AccessKeySecret、endpoint等配置
  28.          */
  29.         com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
  30.                 .setAccessKeyId(accessKeyId)
  31.                 .setAccessKeySecret(accessKeySecret);
  32.         // 访问的域名
  33.         config.endpoint = "ocr.cn-shanghai.aliyuncs.com";
  34.         return new com.aliyun.ocr20191230.Client(config);
  35.     }
  36.     public String licensePlate(String filePath) throws Exception {
  37.         com.aliyun.ocr20191230.Client client = RecognizeLicensePlateAli.createClient(accessKeyId, accessKeySecret);
  38.         File file = new File(filePath);
  39.         // 创建InputStream
  40.         InputStream inputStream = Files.newInputStream(file.toPath());
  41.         com.aliyun.ocr20191230.models.RecognizeLicensePlateAdvanceRequest recognizeLicensePlateAdvanceRequest = new com.aliyun.ocr20191230.models.RecognizeLicensePlateAdvanceRequest()
  42.                 .setImageURLObject(inputStream);
  43.         com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  44.         try {
  45.             RecognizeLicensePlateResponse response = client.recognizeLicensePlateAdvance(recognizeLicensePlateAdvanceRequest, runtime);
  46.             List<RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates> recognizeLicensePlateResponseBodyDataPlates = Optional.of(response).map(RecognizeLicensePlateResponse::getBody).map(RecognizeLicensePlateResponseBody::getData).map(RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData::getPlates).orElse(null);
  47.             if (CollUtil.isNotEmpty(recognizeLicensePlateResponseBodyDataPlates)) {
  48.                 RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates data = recognizeLicensePlateResponseBodyDataPlates.get(0);
  49.                 String plateNumber = data.getPlateNumber();
  50.                 System.out.println(plateNumber);
  51.             }
  52.         } catch (TeaException error) {
  53.             // 获取整体报错信息
  54.             System.out.println(com.aliyun.teautil.Common.toJSONString(error));
  55.             // 获取单个字段
  56.             System.out.println(error.getCode());
  57.         }
  58.         return "";
  59.     }
  60.     public static void main(String[] args) throws Exception {
  61.         // 创建AccessKey ID和AccessKey Secret,请参见:https://help.aliyun.com/document_detail/175144.html
  62.         // 如果您使用的是RAM用户的AccessKey,还需要为子账号授予权限AliyunVIAPIFullAccess,请参见:https://help.aliyun.com/document_detail/145025.html
  63.         // 从环境变量读取配置的AccessKey ID和AccessKey Secret。运行代码示例前必须先配置环境变量。
  64. //        String accessKeyId = System.getenv("LTAI5t6ija1dt5E5vPh4nnDX");
  65. //        String accessKeySecret = System.getenv("brRgmpD7Ch7NUiIiqKJO7kZC5jJZVA");
  66.         com.aliyun.ocr20191230.Client client = RecognizeLicensePlateAli.createClient(accessKeyId, accessKeySecret);
  67.         // 场景一,使用本地文件
  68.         // InputStream inputStream = new FileInputStream(new File("/tmp/RecognizeLicensePlate1.jpg"));
  69.         // 场景二,使用任意可访问的url
  70. //        URL url = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/ocr/RecognizeLicensePlate/cpsb1.jpg");
  71. //        InputStream inputStream = url.openConnection().getInputStream();
  72.         File file = new File("C:\\Users\\Administrator\\Desktop\\123.jpg");
  73.         // 创建InputStream
  74.         InputStream inputStream = Files.newInputStream(file.toPath());
  75.         com.aliyun.ocr20191230.models.RecognizeLicensePlateAdvanceRequest recognizeLicensePlateAdvanceRequest = new com.aliyun.ocr20191230.models.RecognizeLicensePlateAdvanceRequest()
  76.                 .setImageURLObject(inputStream);
  77.         com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
  78.         try {
  79.             // 复制代码运行请自行打印 API 的返回值
  80.             RecognizeLicensePlateResponse response = client.recognizeLicensePlateAdvance(recognizeLicensePlateAdvanceRequest, runtime);
  81.             List<RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates> recognizeLicensePlateResponseBodyDataPlates = Optional.of(response).map(RecognizeLicensePlateResponse::getBody).map(RecognizeLicensePlateResponseBody::getData).map(RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyData::getPlates).orElse(null);
  82.             if (CollUtil.isNotEmpty(recognizeLicensePlateResponseBodyDataPlates)) {
  83.                 RecognizeLicensePlateResponseBody.RecognizeLicensePlateResponseBodyDataPlates data = recognizeLicensePlateResponseBodyDataPlates.get(0);
  84.                 String plateNumber = data.getPlateNumber();
  85.                 System.out.println(plateNumber);
  86.             }
  87.         } catch (TeaException error) {
  88.             // 获取整体报错信息
  89.             System.out.println(com.aliyun.teautil.Common.toJSONString(error));
  90.             // 获取单个字段
  91.             System.out.println(error.getCode());
  92.         }
  93.     }
  94. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表