|
|
|
@ -26,6 +26,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import liquibase.pro.packaged.E;
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
@ -38,7 +39,9 @@ import javax.imageio.ImageIO;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.awt.color.ColorSpace;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.net.URLConnection;
|
|
|
|
@ -673,16 +676,26 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
|
|
|
|
|
//2.调整图片地址,补齐域名
|
|
|
|
|
for (OcrPicture picture : ocrPictureList) {
|
|
|
|
|
picture.setCreateTime(System.currentTimeMillis()); //获取创建时间戳
|
|
|
|
|
// picture.setImgurl(interfaceDomain + picture.getImgurl());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean saveBatch = this.saveBatch(ocrPictureList);
|
|
|
|
|
try {
|
|
|
|
|
boolean saveBatch = false;
|
|
|
|
|
synchronized(lock) {
|
|
|
|
|
// 同步的代码块
|
|
|
|
|
saveBatch = this.saveBatch(ocrPictureList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//3.添加图片信息
|
|
|
|
|
if (saveBatch) {
|
|
|
|
|
this.savePicturesInfo(ocrPictureList);
|
|
|
|
|
// this.savePicturesInfo(ocrPictureList);
|
|
|
|
|
this.convertImage(ocrPictureList);
|
|
|
|
|
return ocrPictureList.size();
|
|
|
|
|
}
|
|
|
|
|
log.error("存储图片失败:{}",JSONObject.toJSON(ocrPictureList));
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
log.error("存储图片异常:{}",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -690,7 +703,7 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
|
|
|
|
|
List<OcrPictureInfo> list = new ArrayList<>();
|
|
|
|
|
for (OcrPicture ocrPicture : ocrPictureList) {
|
|
|
|
|
if (StringUtils.isNotBlank(ocrPicture.getImgurl())) {
|
|
|
|
|
OcrPictureInfo imageInfo = getImageInfo(ocrPicture.getImgurl());
|
|
|
|
|
OcrPictureInfo imageInfo = getImageInfo(ocrPicture.getImgurl(),true);
|
|
|
|
|
if (ObjectUtil.isNotEmpty(imageInfo)) {
|
|
|
|
|
imageInfo.setPictureId(ocrPicture.getId());
|
|
|
|
|
imageInfo.setImgName(ocrPicture.getImgname());
|
|
|
|
@ -701,18 +714,49 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
|
|
|
|
|
ocrPictureInfoService.saveBatch(list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OcrPictureInfo getImageInfo(String imageUrl) {
|
|
|
|
|
/**
|
|
|
|
|
* 新增图片信息表
|
|
|
|
|
* @param ocrPicture
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void savePicturesInfo(OcrPicture ocrPicture) {
|
|
|
|
|
boolean isConnection = true;
|
|
|
|
|
String url = ocrPicture.getImgurl();
|
|
|
|
|
if(!StringUtils.isEmpty(ocrPicture.getLocalpictrueurl())){
|
|
|
|
|
url = ocrPicture.getLocalpictrueurl();
|
|
|
|
|
isConnection = false;
|
|
|
|
|
}
|
|
|
|
|
OcrPictureInfo imageInfo = getImageInfo(url,isConnection);
|
|
|
|
|
imageInfo.setPictureId(ocrPicture.getId());
|
|
|
|
|
imageInfo.setImgName(ocrPicture.getImgname());
|
|
|
|
|
ocrPictureInfoService.save(imageInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取图片信息
|
|
|
|
|
* @param imageUrl 图片地址 两种 网络或者本地图片
|
|
|
|
|
* @param isConnection 是否是url 开启网络请求
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private OcrPictureInfo getImageInfo(String imageUrl,boolean isConnection) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
BufferedImage image = null;
|
|
|
|
|
Integer imageSize = 0;
|
|
|
|
|
if(isConnection){
|
|
|
|
|
// 创建URL对象
|
|
|
|
|
URL url = new URL(imageUrl);
|
|
|
|
|
// 打开连接
|
|
|
|
|
URLConnection urlConnection = url.openConnection();
|
|
|
|
|
// 获取图片大小
|
|
|
|
|
Integer imageSize = urlConnection.getContentLength();
|
|
|
|
|
|
|
|
|
|
imageSize = urlConnection.getContentLength();
|
|
|
|
|
// 使用ImageIO读取图片
|
|
|
|
|
BufferedImage image = ImageIO.read(url);
|
|
|
|
|
image= ImageIO.read(url);
|
|
|
|
|
}else{
|
|
|
|
|
File file = new File(imageUrl);
|
|
|
|
|
image = ImageIO.read(file);
|
|
|
|
|
imageSize =(int)file.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (image != null) {
|
|
|
|
|
OcrPictureInfo info = new OcrPictureInfo();
|
|
|
|
@ -751,12 +795,14 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
|
|
|
|
|
log.info("Cannot read image from URL.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("Error: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void convertImage(List<OcrPicture> list) {
|
|
|
|
|
for (OcrPicture ocrPicture : list) {
|
|
|
|
|
PictureImgToLocalEntity entity = new PictureImgToLocalEntity();
|
|
|
|
@ -764,7 +810,22 @@ public class OcrPictureServiceImpl extends BaseServiceImpl<OcrPictureMybatisDao,
|
|
|
|
|
entity.setImgUrl(ocrPicture.getImgurl());
|
|
|
|
|
String imgurl = ocrPicture.getImgurl();
|
|
|
|
|
entity.setLocalPath(localImagePath + imgurl.substring(imgurl.lastIndexOf("/")));
|
|
|
|
|
TaskQueue.pictureImgToLocalPushData(entity);
|
|
|
|
|
|
|
|
|
|
boolean addedToQueue = false;
|
|
|
|
|
int count = 1;
|
|
|
|
|
while (!addedToQueue && count<=5){
|
|
|
|
|
try {
|
|
|
|
|
addedToQueue = TaskQueue.pictureImgToLocalPushData(entity);
|
|
|
|
|
if(addedToQueue) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Thread.sleep(30000);
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
log.error("放入图片下载队列,等待休眠异常:{}",e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|