|
|
@ -1,15 +1,20 @@ |
|
|
|
package com.ccsens.tcm.service; |
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64; |
|
|
|
import cn.hutool.core.collection.CollectionUtil; |
|
|
|
import cn.hutool.core.img.Img; |
|
|
|
import cn.hutool.core.util.ImageUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import com.alibaba.cloudapi.sdk.model.ApiResponse; |
|
|
|
import com.alibaba.fastjson.JSONArray; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.ccsens.tcm.bean.dto.QuestionOcrDto; |
|
|
|
import com.ccsens.tcm.bean.po.CommonFile; |
|
|
|
import com.ccsens.tcm.bean.po.ReportOcr; |
|
|
|
import com.ccsens.tcm.bean.po.ReportOcrExample; |
|
|
|
import com.ccsens.tcm.bean.vo.QuestionOcrVo; |
|
|
|
import com.ccsens.tcm.persist.dao.QuestionOcrDao; |
|
|
|
import com.ccsens.tcm.persist.mapper.CommonFileMapper; |
|
|
|
import com.ccsens.tcm.persist.mapper.ReportOcrMapper; |
|
|
|
import com.ccsens.tcm.uitl.Constant; |
|
|
|
import com.ccsens.util.CodeEnum; |
|
|
@ -24,6 +29,14 @@ import org.springframework.transaction.annotation.Propagation; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.MalformedURLException; |
|
|
|
import java.net.URL; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
@ -43,10 +56,12 @@ public class OcrService implements IOcrService { |
|
|
|
private QuestionOcrDao questionOcrDao; |
|
|
|
@Resource |
|
|
|
private ReportOcrMapper reportOcrMapper; |
|
|
|
@Resource |
|
|
|
private CommonFileMapper commonFileMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public List<QuestionOcrVo.GeneralBasic> identifyWords(QuestionOcrDto.GeneralBasic generalBasic, Long userId) { |
|
|
|
public List<QuestionOcrVo.GeneralBasic> identifyWords(QuestionOcrDto.GeneralBasic generalBasic, Long userId) throws IOException { |
|
|
|
long time1 = System.currentTimeMillis(); |
|
|
|
List<QuestionOcrVo.GeneralBasic> vos = new ArrayList<>(); |
|
|
|
// 类型测评规则
|
|
|
@ -69,9 +84,21 @@ public class OcrService implements IOcrService { |
|
|
|
List<String> contents = new ArrayList<>(); |
|
|
|
long time4 = 0; |
|
|
|
|
|
|
|
//图片
|
|
|
|
CommonFile commonFile = commonFileMapper.selectByPrimaryKey(generalBasic.getFileId()); |
|
|
|
if (commonFile == null) { |
|
|
|
throw new BaseException(CodeEnum.PARAM_ERROR); |
|
|
|
} |
|
|
|
File file = new File(commonFile.getLocation()); |
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
ImageUtil.scale(ImageIO.read(file), out, 0.5f); |
|
|
|
// Img.from(new URL(generalBasic.getUrl())).scale(0.5f).write(out);
|
|
|
|
String img = Base64.encode(out.toByteArray()); |
|
|
|
|
|
|
|
if (baidu) { |
|
|
|
BaiDuDto.GeneralBasic basic = new BaiDuDto.GeneralBasic(); |
|
|
|
basic.setUrl(generalBasic.getUrl()); |
|
|
|
// basic.setUrl(generalBasic.getUrl());
|
|
|
|
basic.setImage(img); |
|
|
|
BaiDuVo.GeneralBasic words = BaiDuUtil.accurateBasic(Constant.BaiDu.APP_KEY, Constant.BaiDu.SECRET_KEY, basic); |
|
|
|
log.info("文字识别:{}", words); |
|
|
|
if (words == null || CollectionUtil.isEmpty(words.getWordsResult())) { |
|
|
@ -80,7 +107,8 @@ public class OcrService implements IOcrService { |
|
|
|
words.getWordsResult().forEach(word -> contents.add(word.getWords())); |
|
|
|
} else { |
|
|
|
// 阿里
|
|
|
|
String body = "{\"img\":\"\",\"url\":\""+generalBasic.getUrl()+"\",\"prob\":false,\"charInfo\":false,\"rotate\":false,\"table\":false}"; |
|
|
|
// String body = "{\"img\":\"\",\"url\":\""+generalBasic.getUrl()+"\",\"prob\":false,\"charInfo\":false,\"rotate\":false,\"table\":false}";
|
|
|
|
String body = "{\"img\":\""+img+"\",\"url\":\"\",\"prob\":false,\"charInfo\":false,\"rotate\":false,\"table\":false}"; |
|
|
|
ApiResponse response = AliInstanceUtil.getGeneralAdvancedHttps().ocrAdvancedSyncMode(body.getBytes()); |
|
|
|
time4 = System.currentTimeMillis(); |
|
|
|
log.info("阿里接口调用:{}", time4 -time3); |
|
|
|