opt: 将物料PDF文件转换成图片展示
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -111,7 +112,12 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
|
||||
AGVSetOutReqLower = this.itemProtocol.getAGVSetOutReqLower();
|
||||
heartbeat = this.itemProtocol.getHeartbeat();
|
||||
if (heartbeat == 1) {
|
||||
writing("heartbeat", writing_heartbeat);
|
||||
List list = new ArrayList<>();
|
||||
Map map = new HashMap();
|
||||
map.put("code", "heartbeat");
|
||||
map.put("value", writing_heartbeat);
|
||||
list.add(map);
|
||||
writing(list);
|
||||
}
|
||||
} catch (Exception var17) {
|
||||
log.info("读取信号失败,失败原因:{}", var17.getMessage());
|
||||
@@ -121,11 +127,10 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
|
||||
}
|
||||
|
||||
public void writing(String param, String value) {
|
||||
if (!"heartbeat".equals(param)) {
|
||||
String to_param = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code()
|
||||
+ "." + param;
|
||||
|
||||
Map<String, Object> itemMap = new HashMap<String, Object>();
|
||||
Map<String, Object> itemMap = new HashMap<>();
|
||||
|
||||
itemMap.put(to_param, Integer.parseInt(value));
|
||||
try {
|
||||
@@ -134,6 +139,7 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (!"heartbeat".equals(param)) {
|
||||
LuceneLogDto logDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("下发多个电气信号" + itemMap)
|
||||
@@ -154,6 +160,7 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
|
||||
itemMap.put(to_param, json.getString("value"));
|
||||
}
|
||||
}
|
||||
|
||||
LuceneLogDto logDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("下发电气信号:" + itemMap)
|
||||
|
||||
@@ -34,6 +34,24 @@
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>fontbox</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
|
||||
<dependency>
|
||||
<groupId>org.apache.pdfbox</groupId>
|
||||
<artifactId>pdfbox</artifactId>
|
||||
<version>2.0.9</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
|
||||
<dependency>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<version>1.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
|
||||
@@ -222,7 +222,7 @@ public class FabController {
|
||||
public ResponseEntity<TableDataInfo> sendMater(@RequestBody SendMaterVo materInfo) {
|
||||
JSONObject toJSON = (JSONObject) JSON.toJSON(materInfo);
|
||||
//TODO:待确定
|
||||
if("1".equals(materInfo.getPoint_code())){
|
||||
if ("1".equals(materInfo.getPoint_code())) {
|
||||
List<SendVehicleVo> mater = materInfo.getMater();
|
||||
if (CollUtil.isEmpty(mater)) throw new BadRequestException("物料信息为空,请确认!");
|
||||
JSONObject json = new JSONObject();
|
||||
@@ -260,8 +260,11 @@ public class FabController {
|
||||
toJSON.put("material_info", materiales);
|
||||
toJSON.put("vehicle_code", schBasePoint.getVehicle_code());
|
||||
toJSON.put("region_code", StrUtil.isNotEmpty(mater.get(0).getRegion_code()) ? mater.get(0).getRegion_code() : schBasePoint.getRegion_code());
|
||||
}else{
|
||||
} else {
|
||||
if ("2".equals(materInfo.getPoint_code())) {
|
||||
List<SendVehicleVo> mater = materInfo.getMater();
|
||||
if (CollUtil.isEmpty(mater)) throw new BadRequestException("物料信息为空,请确认!");
|
||||
toJSON.put("material_info", mater);
|
||||
toJSON.put("region_code", RegionEnum.NBJG.getRegion_code());
|
||||
} else if ("3".equals(materInfo.getPoint_code())) {
|
||||
toJSON.put("region_code", RegionEnum.WXJG.getRegion_code());
|
||||
|
||||
@@ -31,6 +31,10 @@ public class SendVehicleVo {
|
||||
* 载具号
|
||||
*/
|
||||
private String vehicle_code;
|
||||
/**
|
||||
* 物料id
|
||||
*/
|
||||
private String material_id;
|
||||
/**
|
||||
* 优先级
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.common.enums.GoodsEnum;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
@@ -35,11 +37,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import sun.misc.BASE64Encoder;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -235,39 +240,42 @@ public class SchBaseVehiclematerialgroupServiceImpl extends ServiceImpl<SchBaseV
|
||||
public String selectMaterialFile(String groupId) {
|
||||
SchBaseVehiclematerialgroup one = this.getOne(Wrappers.lambdaQuery(SchBaseVehiclematerialgroup.class)
|
||||
.eq(SchBaseVehiclematerialgroup::getGroup_id, groupId));
|
||||
if (ObjectUtil.isNotEmpty(one)){
|
||||
if(StrUtil.isNotEmpty(one.getMaterial_path())){
|
||||
InputStream inputStream = null;
|
||||
byte[] data = null;
|
||||
String contentType="";
|
||||
try {
|
||||
String fileName = one.getMaterial_path().substring(one.getMaterial_path().lastIndexOf("/") + 1, one.getMaterial_path().length());
|
||||
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
|
||||
if ("jpg".equalsIgnoreCase(suffix) || "jpeg".equalsIgnoreCase(suffix)) {
|
||||
contentType = "data:image/jpeg;base64,";
|
||||
} else if ("png".equalsIgnoreCase(suffix)) {
|
||||
contentType = "data:image/png;base64,";
|
||||
} else if("aac".equalsIgnoreCase(suffix)){
|
||||
contentType = "data:audio/aac;base64,";
|
||||
} else if("mp3".equalsIgnoreCase(suffix)){
|
||||
contentType = "data:audio/x-mpeg;base64,";
|
||||
} else if("wav".equalsIgnoreCase(suffix)){
|
||||
contentType = "data:audio/x-wav;base64,";
|
||||
}
|
||||
inputStream = new FileInputStream(one.getMaterial_path());
|
||||
data = new byte[inputStream.available()];
|
||||
inputStream.read(data);
|
||||
inputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if(ObjectUtil.isNotEmpty(one) && StrUtil.isNotEmpty(one.getMaterial_path())) {
|
||||
byte[] bytes = pdf2png(one.getMaterial_path(), "png");
|
||||
if (ObjectUtil.isNotEmpty(bytes)) {
|
||||
BASE64Encoder encoder = new BASE64Encoder();
|
||||
return contentType + encoder.encode(data).replace("\r\n", "");
|
||||
return "data:image/png;base64," + encoder.encode(bytes).replace("\r\n", "");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用pdfbox将整个pdf转换成图片
|
||||
*
|
||||
* @param fileAddress 文件地址 如:C:\\Users\\user\\Desktop\\test
|
||||
* @param filename PDF文件名不带后缀名
|
||||
* @param type 图片类型 png 和jpg
|
||||
*/
|
||||
public byte[] pdf2png(String fileAddress, String type) {
|
||||
File file = new File(fileAddress);
|
||||
try (PDDocument doc = PDDocument.load(file)) {
|
||||
PDFRenderer renderer = new PDFRenderer(doc);
|
||||
int pageCount = doc.getNumberOfPages();
|
||||
for (int i = 0; i < pageCount; i++) {
|
||||
// dpi为144,越高越清晰,转换越慢
|
||||
BufferedImage image = renderer.renderImageWithDPI(i, 144);
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
ImageIO.write(image, type, bos);
|
||||
return bos.toByteArray();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to convert PDF to image: {}", fileAddress, e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Long getTime(String dateString) {
|
||||
String pattern = "yyyy-MM-dd HH:mm:ss";
|
||||
java.util.Date date = DateUtil.parse(dateString, pattern);
|
||||
|
||||
@@ -7,10 +7,12 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.system.service.notice.ISysNoticeService;
|
||||
import org.nl.system.service.param.dao.Param;
|
||||
import org.nl.system.service.param.impl.SysParamServiceImpl;
|
||||
import org.nl.wms.sch.group.service.ISchBaseVehiclematerialgroupService;
|
||||
import org.nl.wms.sch.group.service.dao.SchBaseVehiclematerialgroup;
|
||||
import org.nl.wms.sch.task_manage.enums.NoticeTypeEnum;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -32,6 +34,9 @@ public class AutoQueryFilePath {
|
||||
@Autowired
|
||||
private SysParamServiceImpl sysParamService;
|
||||
|
||||
@Autowired
|
||||
private ISysNoticeService noticeService;
|
||||
|
||||
private String materialUrl = null;
|
||||
|
||||
private File directory = null;
|
||||
@@ -46,8 +51,12 @@ public class AutoQueryFilePath {
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(directory) && CollUtil.isNotEmpty(materials)) {
|
||||
if (directory.exists() && directory.isDirectory()) {
|
||||
noticeService.createNotice("开始查询物料图片路径!", "1",
|
||||
NoticeTypeEnum.WARN.getCode());
|
||||
selectMaterialPath(directory, materials);
|
||||
}
|
||||
noticeService.createNotice("结束查询物料图片路径!", "2",
|
||||
NoticeTypeEnum.WARN.getCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,16 +72,9 @@ public class AutoQueryFilePath {
|
||||
selectMaterialPath(file,materials);
|
||||
} else if (file.isFile() && CollUtil.contains(materials, file.getName().substring(0, file.getName().lastIndexOf('.')))) {
|
||||
String material = file.getName().substring(0, file.getName().lastIndexOf('.'));
|
||||
File parentFile = file.getParentFile();
|
||||
if(parentFile != null) {
|
||||
String name = parentFile.getName();
|
||||
if (StrUtil.isNotEmpty(name)) {
|
||||
iSchBaseVehiclematerialgroupService.update(Wrappers.lambdaUpdate(SchBaseVehiclematerialgroup.class)
|
||||
.eq(SchBaseVehiclematerialgroup::getMaterial_id, material)
|
||||
.set(SchBaseVehiclematerialgroup::getMaterial_path, parentFile.getPath() + "\\" + name + "_large.png"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
iSchBaseVehiclematerialgroupService.update(Wrappers.lambdaUpdate(SchBaseVehiclematerialgroup.class)
|
||||
.eq(SchBaseVehiclematerialgroup::getMaterial_id, material)
|
||||
.set(SchBaseVehiclematerialgroup::getMaterial_path, file.getPath()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user