Browse Source

ftp上传图,将图片同步到华软ftp

master
wangxun 8 months ago
parent
commit
875d745648
3 changed files with 216 additions and 19 deletions
  1. +7
    -1
      src/main/java/com/inspect/simulator/controller/InfraredController.java
  2. +4
    -1
      src/main/java/com/inspect/simulator/service/HikVisionService.java
  3. +205
    -17
      src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java

+ 7
- 1
src/main/java/com/inspect/simulator/controller/InfraredController.java View File

@ -50,8 +50,14 @@ public class InfraredController {
@ResponseBody
public AjaxResult capturePicture(@RequestBody Coordinate coordinate) {
return hikVisionService.analyzeInfrared(coordinate);
return hikVisionService.capturePicture(coordinate);
}
//图谱解析
@PostMapping("/ir/calculate/capturePicture")
@ResponseBody
public AjaxResult calculatePicture(@RequestBody Coordinate coordinate) {
return hikVisionService.calculatePicture(coordinate);
}
}

+ 4
- 1
src/main/java/com/inspect/simulator/service/HikVisionService.java View File

@ -15,8 +15,11 @@ public interface HikVisionService {
// AjaxResult cameraHongWaiHk(Camera camera) ;
//图谱解析
AjaxResult analyzeInfrared(Coordinate coordinate);
AjaxResult capturePicture(Coordinate coordinate);
//调用华软无人机获取csv温度
public float[][] UploadFtpImage( String url, String type, String image);
//图谱解析
AjaxResult calculatePicture(Coordinate coordinate);
}

+ 205
- 17
src/main/java/com/inspect/simulator/service/impl/HikVisionServiceImpl.java View File

@ -23,8 +23,10 @@ import com.sun.jna.ptr.IntByReference;
import okhttp3.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPSClient;
import org.aspectj.weaver.ast.Var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -69,6 +71,19 @@ public class HikVisionServiceImpl implements HikVisionService {
@Value("${file.hrFtpUrl:test}")
private String hrFtpUrl;
@Value("${file.ftpUrlAddress:test}")
private String ftpUrlAddress;
@Value("${file.ftpUrlAccount:test}")
private String ftpUrlAccount;
@Value("${file.ftpUrlPwd:test}")
private String ftpUrlPwd;
@Value("${file.ftpUrlPort:test}")
private Integer ftpUrlPort;
@Resource
private SftpClient sftpClient;
@ -127,7 +142,7 @@ public class HikVisionServiceImpl implements HikVisionService {
// }
// @Override
// @Override
// public AjaxResult cameraHongWaiHk(Camera camera) {
// //红外图谱信息
// InfraredInfo infraredInfo = new InfraredInfo();
@ -279,18 +294,34 @@ public class HikVisionServiceImpl implements HikVisionService {
// return AjaxResult.success(infraredInfo);
// }
@Override
public AjaxResult analyzeInfrared(Coordinate coordinate) {
public AjaxResult capturePicture(Coordinate coordinate) {
InfraredInfo infraredInfo = new InfraredInfo();
try {
//String imagePath = coordinate.getFilePath();
String imagePath = "/images/ir_test.jpg";
String imagePath = coordinate.getFilePath();
// String imagePath = "/images/ir_test.jpg";
InputStream inputStream = downloadFtp(imagePath);
String protocol = hrFtpUrl.substring(0, 6); // "ftp://"
String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/"
String[] userAndHost = withoutProtocol.split("@");
if (userAndHost.length != 2) {
throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符");
}
String[] userPass = userAndHost[0].split(":");
String username = userPass[0];
String password = userPass[1];
String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/"
String[] hostAndPort = hostPort.split(":");
String host = hostAndPort[0];
int port = Integer.parseInt(hostAndPort[1]);
String imageName = Paths.get(imagePath).getFileName().toString();
pic2Ftp(imageName,inputStream,host,port,username,password);
String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]";
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
if (coordinate.getImgType() == 1) {
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
infraredInfo = readDataHw(imageBytes, coordinate);
} else if (coordinate.getImgType() == 2) {
@ -306,7 +337,7 @@ public class HikVisionServiceImpl implements HikVisionService {
infraredInfo.setTemperatureMatrix(imageTem);
String s = ImageOverlaysUav(coordinate, infraredInfo);
infraredInfo.setOutPath(s);
}else {
} else {
return AjaxResult.error("温度解析错误!");
}
}
@ -591,7 +622,7 @@ public class HikVisionServiceImpl implements HikVisionService {
String fileName = file.getName();
String pureName = fileName.substring(0, fileName.lastIndexOf('.'));
String filename = picPath;
String outputPath =picPath+ pureName + "_" + markPicName + ".jpg";
String outputPath = picPath + pureName + "_" + markPicName + ".jpg";
//判断路径是否存在
File filePath = new File(filename);
if (!filePath.exists()) {
@ -710,7 +741,7 @@ public class HikVisionServiceImpl implements HikVisionService {
String fileName = file.getName();
String pureName = fileName.substring(0, fileName.lastIndexOf('.'));
String filename = picPath;
String outputPath =picPath+ pureName + "_" + markPicName + ".jpg";
String outputPath = picPath + pureName + "_" + markPicName + ".jpg";
//判断路径是否存在
File filePath = new File(filename);
if (!filePath.exists()) {
@ -814,7 +845,7 @@ public class HikVisionServiceImpl implements HikVisionService {
String fileName = file.getName();
String pureName = fileName.substring(0, fileName.lastIndexOf('.'));
String filename = picPath;
String outputPath =picPath+ pureName + "_" + markPicName + ".jpg";
String outputPath = picPath + pureName + "_" + markPicName + ".jpg";
//判断路径是否存在
File filePath = new File(filename);
if (!filePath.exists()) {
@ -822,8 +853,13 @@ public class HikVisionServiceImpl implements HikVisionService {
filePath.mkdirs();
}
try {
InputStream inputStreamPath = downloadFtp(imagePath);
if (inputStreamPath==null){
System.out.println("无法加载图片: " + imagePath);
}
// 加载原始图片
BufferedImage originalImage = ImageIO.read(new File(imagePath));
// BufferedImage originalImage = ImageIO.read(new File(imagePath));
BufferedImage originalImage = ImageIO.read(inputStreamPath);
if (originalImage == null) {
System.err.println("无法加载图片: " + imagePath);
return null;
@ -905,12 +941,19 @@ public class HikVisionServiceImpl implements HikVisionService {
}
}
g2d.dispose();
// 保存标注后的图片
File outputFile = new File(outputPath);
// 确保输出目录存在
outputFile.getParentFile().mkdirs();
ImageIO.write(annotatedImage, "jpg", outputFile);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(annotatedImage, "jpg", os);
InputStream inputStream = new ByteArrayInputStream(os.toByteArray());
picFtp(outputPath, inputStream,ftpUrlAddress,ftpUrlPort,ftpUrlAccount,ftpUrlPwd);
// // 保存标注后的图片
// File outputFile = new File(outputPath);
// // 确保输出目录存在
// outputFile.getParentFile().mkdirs();
//
// ImageIO.write(annotatedImage, "jpg", outputFile);
// System.out.println("标注后的图片已保存到: " + outputPath);
} catch (IOException e) {
@ -920,7 +963,6 @@ public class HikVisionServiceImpl implements HikVisionService {
return outputPath;
}
public float[][] UploadFtpImage(String url, String type, String image) {
type = "ftp";
@ -971,7 +1013,7 @@ public class HikVisionServiceImpl implements HikVisionService {
// Fall back to local file
try {
String localPath = picPath+"1.csv";
String localPath = picPath + "1.csv";
// String csvFileName = image.replaceAll("\\..+$", ".csv");
File csvFile = new File(localPath);
if (!csvFile.exists()) {
@ -1056,4 +1098,150 @@ public class HikVisionServiceImpl implements HikVisionService {
}
public void picFtp(String newPath, InputStream originalPath,String ftpUrlAddress,Integer ftpUrlPort,String ftpUrlAccount,String ftpUrlPwd) {
FTPSClient ftps = null;
try {
ftps = new FTPSClient(true);
ftps.connect(ftpUrlAddress, ftpUrlPort);
boolean loginRes = ftps.login(ftpUrlAccount, ftpUrlPwd);
System.out.println(loginRes);
ftps.setFileType(2);
ftps.enterLocalPassiveMode();
ftps.setControlEncoding("UTF-8");
ftps.setFileTransferMode(10);
ftps.execPROT("P");
boolean success = ftps.storeFile(newPath, originalPath);
if (!success) {
System.err.println("上传失败!FTP 返回: " + ftps.getReplyString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5. 关闭资源
try {
if (originalPath != null) originalPath.close();
if (ftps != null && ftps.isConnected()) {
ftps.logout();
ftps.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void pic2Ftp(String newPath, InputStream originalPath,String ftpUrlAddress,Integer ftpUrlPort,String ftpUrlAccount,String ftpUrlPwd) {
FTPClient ftps = null;
try {
ftps = new FTPClient();
ftps.connect(ftpUrlAddress, ftpUrlPort);
boolean loginRes = ftps.login(ftpUrlAccount, ftpUrlPwd);
System.out.println(loginRes);
ftps.setFileType(2);
ftps.enterLocalPassiveMode();
ftps.setControlEncoding("UTF-8");
ftps.setFileTransferMode(10);
boolean success = ftps.storeFile(newPath, originalPath);
if (!success) {
System.err.println("上传失败!FTP 返回: " + ftps.getReplyString());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 5. 关闭资源
try {
if (originalPath != null) originalPath.close();
if (ftps != null && ftps.isConnected()) {
ftps.logout();
ftps.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public InputStream downloadFtp(String downloadPath) {
InputStream inputStream = null;
FTPSClient ftps;
try {
ftps = new FTPSClient(true);
ftps.connect(ftpUrlAddress, ftpUrlPort);
boolean loginRes = ftps.login(ftpUrlAccount, ftpUrlPwd);
System.out.println(loginRes);
ftps.setFileType(2);
ftps.enterLocalPassiveMode();
ftps.setControlEncoding("UTF-8");
ftps.setFileTransferMode(10);
ftps.execPROT("P");
inputStream = ftps.retrieveFileStream(downloadPath);
if (inputStream == null) {
System.out.println("[FTP] DOWNLOAD FAIL, EMPTY STREAM:" + downloadPath);
}
} catch (Exception e) {
}
return inputStream;
}
@Override
public AjaxResult calculatePicture(Coordinate coordinate) {
InfraredInfo infraredInfo = new InfraredInfo();
try {
String imagePath = coordinate.getFilePath();
// String imagePath = "/images/ir_test.jpg";
InputStream inputStream = downloadFtp(imagePath);
String protocol = hrFtpUrl.substring(0, 6); // "ftp://"
String withoutProtocol = hrFtpUrl.substring(6); // "zthr02:zthr02@123.184.14.138:50021/"
String[] userAndHost = withoutProtocol.split("@");
if (userAndHost.length != 2) {
throw new IllegalArgumentException("URL 格式错误,缺少 @ 分隔符");
}
String[] userPass = userAndHost[0].split(":");
String username = userPass[0];
String password = userPass[1];
String hostPort = userAndHost[1].replace("/", ""); // 移除末尾的 "/"
String[] hostAndPort = hostPort.split(":");
String host = hostAndPort[0];
int port = Integer.parseInt(hostAndPort[1]);
picFtp("/",inputStream,host,port,username,password);
String imageName = Paths.get(imagePath).getFileName().toString();
String ftpUrlName = "[\"" + hrFtpUrl + imageName + "\"]";
if (coordinate.getImgType() == 1) {
byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
infraredInfo = readDataHw(imageBytes, coordinate);
} else if (coordinate.getImgType() == 2) {
String s = ImageOverlaysIvs(coordinate, infraredInfo);
infraredInfo.setOutPath(s);
} else if (coordinate.getImgType() == 3) {
//sdk版本调用x86_32
//float[][] imageTem = djService.getImageTem(coordinate.getFilePath());
//调用华软接口
float[][] imageTem = UploadFtpImage(hrUavUrl, null, ftpUrlName);
if (imageTem != null && imageTem.length > 0 && imageTem[0].length > 0) {
infraredInfo.setTemperatureMatrix(imageTem);
String s = ImageOverlaysUav(coordinate, infraredInfo);
infraredInfo.setOutPath(s);
} else {
return AjaxResult.error("温度解析错误!");
}
}
} catch (Exception e) {
e.printStackTrace();
return AjaxResult.error();
}
return AjaxResult.success(infraredInfo);
}
}

Loading…
Cancel
Save