123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- package com.qmth.ops.biz.service;
- import com.qmth.boot.core.ai.model.ocr.ImageType;
- import com.qmth.boot.core.ai.model.ocr.OcrType;
- import com.qmth.boot.core.ai.model.ocr.ParseDocTask;
- import com.qmth.boot.core.ai.model.ocr.ParseDocTaskResult;
- import com.qmth.ops.biz.ai.client.DocApiClient;
- import com.qmth.ops.biz.ai.client.OcrApiClient;
- import com.qmth.ops.biz.ai.client.OcrApiConfig;
- import com.qmth.ops.biz.ai.exception.OcrClientNotFound;
- import com.qmth.ops.biz.domain.OcrSupplier;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import javax.annotation.Resource;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Service
- public class OcrClientService {
- private static final Logger log = LoggerFactory.getLogger(OcrClientService.class);
- private OcrApiClient defaultClient;
- private Map<Long, OcrApiClient> clientMap;
- @Resource
- private OcrSupplierService ocrSupplierService;
- @PostConstruct
- public synchronized void init() {
- defaultClient = null;
- clientMap = new HashMap<>();
- List<OcrSupplier> list = ocrSupplierService.list();
- for (OcrSupplier supplier : list) {
- initApiClient(supplier);
- }
- }
- private void initApiClient(OcrSupplier supplier) {
- try {
- String className = OcrApiClient.class.getName().replace("OcrApiClient", supplier.getClientClass());
- OcrApiConfig config = new OcrApiConfig(supplier);
- Class<?> clientClass = Class.forName(className);
- Object clientInstance = clientClass.getConstructor(OcrApiConfig.class).newInstance(config);
- if (clientInstance instanceof OcrApiClient) {
- OcrApiClient client = (OcrApiClient) clientInstance;
- clientMap.put(supplier.getId(), client);
- // 取第一个enable=true的为默认客户端
- if (supplier.getEnable() && defaultClient == null) {
- defaultClient = client;
- }
- } else {
- log.warn("DocApiClient supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
- }
- } catch (Exception e) {
- log.error("OcrApiClient init error, supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
- }
- }
- public String forImage(Long id, OcrType type, byte[] imageData, ImageType imageType) throws Exception {
- OcrApiClient client = clientMap.get(id);
- if (client == null) {
- throw new OcrClientNotFound(id);
- }
- return client.forImage(type, imageData, imageType);
- }
- public String forImage(OcrType type, byte[] imageData, ImageType imageType) throws Exception {
- if (defaultClient == null) {
- throw new OcrClientNotFound();
- }
- return defaultClient.forImage(type, imageData, imageType);
- }
- public ParseDocTask parseDocTask(byte[] fileData, String fileName) throws Exception {
- DocApiClient client = this.getDefaultDocApiClient();
- return client.parseDocTask(fileData, fileName);
- }
- public ParseDocTaskResult parseDocTaskQuery(String taskId) throws Exception {
- DocApiClient client = this.getDefaultDocApiClient();
- return client.parseDocTaskQuery(taskId);
- }
- private DocApiClient getDefaultDocApiClient() {
- OcrSupplier supplier = ocrSupplierService.getById(3L);//todo
- try {
- String className = DocApiClient.class.getName().replace(DocApiClient.class.getSimpleName(), supplier.getClientClass());
- OcrApiConfig config = new OcrApiConfig(supplier);
- Class<?> clientClass = Class.forName(className);
- return (DocApiClient) clientClass.getConstructor(OcrApiConfig.class).newInstance(config);
- } catch (Exception e) {
- log.error("DocApiClient init error, supplier:{} class:{}", supplier.getName(), supplier.getClientClass());
- throw new OcrClientNotFound();
- }
- }
- }
|