OcrClientService.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.qmth.ops.biz.service;
  2. import com.qmth.boot.core.ai.model.ocr.ImageType;
  3. import com.qmth.boot.core.ai.model.ocr.OcrType;
  4. import com.qmth.boot.core.ai.model.ocr.ParseDocTask;
  5. import com.qmth.boot.core.ai.model.ocr.ParseDocTaskResult;
  6. import com.qmth.ops.biz.ai.client.DocApiClient;
  7. import com.qmth.ops.biz.ai.client.OcrApiClient;
  8. import com.qmth.ops.biz.ai.client.OcrApiConfig;
  9. import com.qmth.ops.biz.ai.exception.OcrClientNotFound;
  10. import com.qmth.ops.biz.domain.OcrSupplier;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.stereotype.Service;
  14. import javax.annotation.PostConstruct;
  15. import javax.annotation.Resource;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. @Service
  20. public class OcrClientService {
  21. private static final Logger log = LoggerFactory.getLogger(OcrClientService.class);
  22. private OcrApiClient defaultClient;
  23. private Map<Long, OcrApiClient> clientMap;
  24. @Resource
  25. private OcrSupplierService ocrSupplierService;
  26. @PostConstruct
  27. public synchronized void init() {
  28. defaultClient = null;
  29. clientMap = new HashMap<>();
  30. List<OcrSupplier> list = ocrSupplierService.list();
  31. for (OcrSupplier supplier : list) {
  32. initApiClient(supplier);
  33. }
  34. }
  35. private void initApiClient(OcrSupplier supplier) {
  36. try {
  37. String className = OcrApiClient.class.getName().replace("OcrApiClient", supplier.getClientClass());
  38. OcrApiConfig config = new OcrApiConfig(supplier);
  39. Class<?> clientClass = Class.forName(className);
  40. Object clientInstance = clientClass.getConstructor(OcrApiConfig.class).newInstance(config);
  41. if (clientInstance instanceof OcrApiClient) {
  42. OcrApiClient client = (OcrApiClient) clientInstance;
  43. clientMap.put(supplier.getId(), client);
  44. // 取第一个enable=true的为默认客户端
  45. if (supplier.getEnable() && defaultClient == null) {
  46. defaultClient = client;
  47. }
  48. } else {
  49. log.warn("DocApiClient supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
  50. }
  51. } catch (Exception e) {
  52. log.error("OcrApiClient init error, supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
  53. }
  54. }
  55. public String forImage(Long id, OcrType type, byte[] imageData, ImageType imageType) throws Exception {
  56. OcrApiClient client = clientMap.get(id);
  57. if (client == null) {
  58. throw new OcrClientNotFound(id);
  59. }
  60. return client.forImage(type, imageData, imageType);
  61. }
  62. public String forImage(OcrType type, byte[] imageData, ImageType imageType) throws Exception {
  63. if (defaultClient == null) {
  64. throw new OcrClientNotFound();
  65. }
  66. return defaultClient.forImage(type, imageData, imageType);
  67. }
  68. public ParseDocTask parseDocTask(byte[] fileData, String fileName) throws Exception {
  69. DocApiClient client = this.getDefaultDocApiClient();
  70. return client.parseDocTask(fileData, fileName);
  71. }
  72. public ParseDocTaskResult parseDocTaskQuery(String taskId) throws Exception {
  73. DocApiClient client = this.getDefaultDocApiClient();
  74. return client.parseDocTaskQuery(taskId);
  75. }
  76. private DocApiClient getDefaultDocApiClient() {
  77. OcrSupplier supplier = ocrSupplierService.getById(3L);//todo
  78. try {
  79. String className = DocApiClient.class.getName().replace(DocApiClient.class.getSimpleName(), supplier.getClientClass());
  80. OcrApiConfig config = new OcrApiConfig(supplier);
  81. Class<?> clientClass = Class.forName(className);
  82. return (DocApiClient) clientClass.getConstructor(OcrApiConfig.class).newInstance(config);
  83. } catch (Exception e) {
  84. log.error("DocApiClient init error, supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
  85. throw new OcrClientNotFound();
  86. }
  87. }
  88. }