|
@@ -9,8 +9,11 @@ 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 {
|
|
@@ -19,15 +22,18 @@ public class OcrClientService {
|
|
|
|
|
|
private OcrApiClient defaultClient;
|
|
|
|
|
|
+ private Map<Long, OcrApiClient> clientMap;
|
|
|
+
|
|
|
@Resource
|
|
|
private OcrSupplierService ocrSupplierService;
|
|
|
|
|
|
+ @PostConstruct
|
|
|
public synchronized void init() {
|
|
|
- //取第一个prior的为默认客户端,没有则取第一个
|
|
|
defaultClient = null;
|
|
|
+ clientMap = new HashMap<>();
|
|
|
List<OcrSupplier> list = ocrSupplierService.list();
|
|
|
- if (!list.isEmpty()) {
|
|
|
- initApiClient(list.stream().filter(OcrSupplier::getPrior).findFirst().orElse(list.get(0)));
|
|
|
+ for (OcrSupplier supplier : list) {
|
|
|
+ initApiClient(supplier);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -37,7 +43,9 @@ public class OcrClientService {
|
|
|
OcrApiConfig config = new OcrApiConfig(supplier);
|
|
|
Class<?> clientClass = Class.forName(className);
|
|
|
OcrApiClient client = (OcrApiClient) clientClass.getConstructor(OcrApiConfig.class).newInstance(config);
|
|
|
- if (defaultClient == null) {
|
|
|
+ clientMap.put(supplier.getId(), client);
|
|
|
+ //取第一个enable=true的为默认客户端
|
|
|
+ if (supplier.getEnable() && defaultClient == null) {
|
|
|
defaultClient = client;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -46,9 +54,17 @@ public class OcrClientService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public String forImage(Long id, OcrType type, byte[] imageData) throws Exception {
|
|
|
+ OcrApiClient client = clientMap.get(id);
|
|
|
+ if (defaultClient == null) {
|
|
|
+ throw new OcrClientNotFound(id);
|
|
|
+ }
|
|
|
+ return client.forImage(type, imageData);
|
|
|
+ }
|
|
|
+
|
|
|
public String forImage(OcrType type, byte[] imageData) throws Exception {
|
|
|
if (defaultClient == null) {
|
|
|
- throw new OcrClientNotFound(type);
|
|
|
+ throw new OcrClientNotFound();
|
|
|
}
|
|
|
return defaultClient.forImage(type, imageData);
|
|
|
}
|