Browse Source

opt: 将物料PDF文件转换成图片展示

master
yanps 2 months ago
parent
commit
594d8265b0
  1. 13
      acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/plc_to_agv/PlcToAgvDeviceDriver.java
  2. 18
      lms/nladmin-system/nlsso-server/pom.xml
  3. 7
      lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/ext/fab/controller/FabController.java
  4. 4
      lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/ext/fab/service/dto/SendVehicleVo.java
  5. 68
      lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch/group/service/impl/SchBaseVehiclematerialgroupServiceImpl.java
  6. 18
      lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch/task_manage/AutoQueryFilePath.java

13
acs/nladmin-system/nlsso-server/src/main/java/org/nl/acs/device_driver/basedriver/plc_to_agv/PlcToAgvDeviceDriver.java

@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.Data; import lombok.Data;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -111,7 +112,12 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
AGVSetOutReqLower = this.itemProtocol.getAGVSetOutReqLower(); AGVSetOutReqLower = this.itemProtocol.getAGVSetOutReqLower();
heartbeat = this.itemProtocol.getHeartbeat(); heartbeat = this.itemProtocol.getHeartbeat();
if (heartbeat == 1) { 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) { } catch (Exception var17) {
log.info("读取信号失败,失败原因:{}", var17.getMessage()); log.info("读取信号失败,失败原因:{}", var17.getMessage());
@ -121,11 +127,10 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
} }
public void writing(String param, String value) { 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() String to_param = this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.getDevice().getDevice_code()
+ "." + param; + "." + param;
Map<String, Object> itemMap = new HashMap<String, Object>(); Map<String, Object> itemMap = new HashMap<>();
itemMap.put(to_param, Integer.parseInt(value)); itemMap.put(to_param, Integer.parseInt(value));
try { try {
@ -134,6 +139,7 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
if (!"heartbeat".equals(param)) {
LuceneLogDto logDto = LuceneLogDto.builder() LuceneLogDto logDto = LuceneLogDto.builder()
.device_code(device_code) .device_code(device_code)
.content("下发多个电气信号" + itemMap) .content("下发多个电气信号" + itemMap)
@ -154,6 +160,7 @@ public class PlcToAgvDeviceDriver extends AbstractOpcDeviceDriver implements Dev
itemMap.put(to_param, json.getString("value")); itemMap.put(to_param, json.getString("value"));
} }
} }
LuceneLogDto logDto = LuceneLogDto.builder() LuceneLogDto logDto = LuceneLogDto.builder()
.device_code(device_code) .device_code(device_code)
.content("下发电气信号:" + itemMap) .content("下发电气信号:" + itemMap)

18
lms/nladmin-system/nlsso-server/pom.xml

@ -34,6 +34,24 @@
<dependencies> <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> <dependency>
<groupId>com.google.code.gson</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId> <artifactId>gson</artifactId>

7
lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/ext/fab/controller/FabController.java

@ -222,7 +222,7 @@ public class FabController {
public ResponseEntity<TableDataInfo> sendMater(@RequestBody SendMaterVo materInfo) { public ResponseEntity<TableDataInfo> sendMater(@RequestBody SendMaterVo materInfo) {
JSONObject toJSON = (JSONObject) JSON.toJSON(materInfo); JSONObject toJSON = (JSONObject) JSON.toJSON(materInfo);
//TODO:待确定 //TODO:待确定
if("1".equals(materInfo.getPoint_code())){ if ("1".equals(materInfo.getPoint_code())) {
List<SendVehicleVo> mater = materInfo.getMater(); List<SendVehicleVo> mater = materInfo.getMater();
if (CollUtil.isEmpty(mater)) throw new BadRequestException("物料信息为空,请确认!"); if (CollUtil.isEmpty(mater)) throw new BadRequestException("物料信息为空,请确认!");
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
@ -260,8 +260,11 @@ public class FabController {
toJSON.put("material_info", materiales); toJSON.put("material_info", materiales);
toJSON.put("vehicle_code", schBasePoint.getVehicle_code()); 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()); 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())) { 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()); toJSON.put("region_code", RegionEnum.NBJG.getRegion_code());
} else if ("3".equals(materInfo.getPoint_code())) { } else if ("3".equals(materInfo.getPoint_code())) {
toJSON.put("region_code", RegionEnum.WXJG.getRegion_code()); toJSON.put("region_code", RegionEnum.WXJG.getRegion_code());

4
lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/ext/fab/service/dto/SendVehicleVo.java

@ -31,6 +31,10 @@ public class SendVehicleVo {
* 载具号 * 载具号
*/ */
private String vehicle_code; private String vehicle_code;
/**
* 物料id
*/
private String material_id;
/** /**
* 优先级 * 优先级
*/ */

68
lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch/group/service/impl/SchBaseVehiclematerialgroupServiceImpl.java

@ -15,6 +15,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; 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.domain.query.PageQuery;
import org.nl.common.enums.GoodsEnum; import org.nl.common.enums.GoodsEnum;
import org.nl.common.exception.BadRequestException; import org.nl.common.exception.BadRequestException;
@ -35,11 +37,14 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import java.io.FileInputStream; import javax.imageio.ImageIO;
import java.io.FileNotFoundException; import javax.imageio.stream.ImageInputStream;
import java.io.IOException; import javax.servlet.ServletOutputStream;
import java.io.InputStream; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -235,35 +240,38 @@ public class SchBaseVehiclematerialgroupServiceImpl extends ServiceImpl<SchBaseV
public String selectMaterialFile(String groupId) { public String selectMaterialFile(String groupId) {
SchBaseVehiclematerialgroup one = this.getOne(Wrappers.lambdaQuery(SchBaseVehiclematerialgroup.class) SchBaseVehiclematerialgroup one = this.getOne(Wrappers.lambdaQuery(SchBaseVehiclematerialgroup.class)
.eq(SchBaseVehiclematerialgroup::getGroup_id, groupId)); .eq(SchBaseVehiclematerialgroup::getGroup_id, groupId));
if (ObjectUtil.isNotEmpty(one)){ if(ObjectUtil.isNotEmpty(one) && StrUtil.isNotEmpty(one.getMaterial_path())) {
if(StrUtil.isNotEmpty(one.getMaterial_path())){ byte[] bytes = pdf2png(one.getMaterial_path(), "png");
InputStream inputStream = null; if (ObjectUtil.isNotEmpty(bytes)) {
byte[] data = null; BASE64Encoder encoder = new BASE64Encoder();
String contentType=""; return "data:image/png;base64," + encoder.encode(bytes).replace("\r\n", "");
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();
} }
BASE64Encoder encoder = new BASE64Encoder(); return null;
return contentType + encoder.encode(data).replace("\r\n", "");
} }
/**
* 使用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; return null;
} }

18
lms/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch/task_manage/AutoQueryFilePath.java

@ -7,10 +7,12 @@ import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; 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.dao.Param;
import org.nl.system.service.param.impl.SysParamServiceImpl; import org.nl.system.service.param.impl.SysParamServiceImpl;
import org.nl.wms.sch.group.service.ISchBaseVehiclematerialgroupService; import org.nl.wms.sch.group.service.ISchBaseVehiclematerialgroupService;
import org.nl.wms.sch.group.service.dao.SchBaseVehiclematerialgroup; 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -32,6 +34,9 @@ public class AutoQueryFilePath {
@Autowired @Autowired
private SysParamServiceImpl sysParamService; private SysParamServiceImpl sysParamService;
@Autowired
private ISysNoticeService noticeService;
private String materialUrl = null; private String materialUrl = null;
private File directory = null; private File directory = null;
@ -46,8 +51,12 @@ public class AutoQueryFilePath {
} }
if (ObjectUtil.isNotEmpty(directory) && CollUtil.isNotEmpty(materials)) { if (ObjectUtil.isNotEmpty(directory) && CollUtil.isNotEmpty(materials)) {
if (directory.exists() && directory.isDirectory()) { if (directory.exists() && directory.isDirectory()) {
noticeService.createNotice("开始查询物料图片路径!", "1",
NoticeTypeEnum.WARN.getCode());
selectMaterialPath(directory, materials); selectMaterialPath(directory, materials);
} }
noticeService.createNotice("结束查询物料图片路径!", "2",
NoticeTypeEnum.WARN.getCode());
} }
} }
@ -63,16 +72,9 @@ public class AutoQueryFilePath {
selectMaterialPath(file,materials); selectMaterialPath(file,materials);
} else if (file.isFile() && CollUtil.contains(materials, file.getName().substring(0, file.getName().lastIndexOf('.')))) { } else if (file.isFile() && CollUtil.contains(materials, file.getName().substring(0, file.getName().lastIndexOf('.')))) {
String material = 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) iSchBaseVehiclematerialgroupService.update(Wrappers.lambdaUpdate(SchBaseVehiclematerialgroup.class)
.eq(SchBaseVehiclematerialgroup::getMaterial_id, material) .eq(SchBaseVehiclematerialgroup::getMaterial_id, material)
.set(SchBaseVehiclematerialgroup::getMaterial_path, parentFile.getPath() + "\\" + name + "_large.png")); .set(SchBaseVehiclematerialgroup::getMaterial_path, file.getPath()));
break;
}
}
} }
} }
} }

Loading…
Cancel
Save