|
|
@@ -90,6 +90,10 @@ public class CameraUtils {
|
|
|
* return boolean
|
|
|
* */
|
|
|
public Boolean getCameraImage(String filename,String exposureTime){
|
|
|
+ return getCameraImage(filename, exposureTime, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean getCameraImage(String filename,String exposureTime,String tempDirPath){
|
|
|
int nRet = MV_OK;
|
|
|
try{
|
|
|
// Open selected device
|
|
|
@@ -111,7 +115,8 @@ public class CameraUtils {
|
|
|
|
|
|
// 调焦,调整亮度, 值越大越亮
|
|
|
// nRet = MvCameraControl.MV_CC_SetEnumValueByString(hCamera, "ExposureTime", "268724.0000");
|
|
|
- nRet = MvCameraControl.MV_CC_SetFloatValue(hCamera, "ExposureTime", Float.parseFloat(exposureTime));
|
|
|
+ String exp = (exposureTime == null || exposureTime.trim().isEmpty()) ? "88724" : exposureTime.trim();
|
|
|
+ nRet = MvCameraControl.MV_CC_SetFloatValue(hCamera, "ExposureTime", Float.parseFloat(exp));
|
|
|
if (MV_OK != nRet)
|
|
|
{
|
|
|
this.error = "ExposureTime failed, errcode:" + nRet;
|
|
|
@@ -145,7 +150,7 @@ public class CameraUtils {
|
|
|
}
|
|
|
// System.err.println("11111111111111111");
|
|
|
// nRet = SaveImage(MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg,pData,stImageInfo,sn+"_"+record_time,filename);
|
|
|
- nRet = SaveImageTemp(MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg,pData,stImageInfo,filename);
|
|
|
+ nRet = SaveImageTemp(MV_SAVE_IAMGE_TYPE.MV_Image_Jpeg,pData,stImageInfo,filename,tempDirPath);
|
|
|
// nRet = SaveImageTemp(MV_SAVE_IAMGE_TYPE.MV_Image_Bmp,pData,stImageInfo,filename);
|
|
|
if(MV_OK != nRet)
|
|
|
{
|
|
|
@@ -315,6 +320,11 @@ public class CameraUtils {
|
|
|
// 保存图片到临时目录
|
|
|
public int SaveImageTemp(MV_SAVE_IAMGE_TYPE enSaveImageType,byte[] dataToSave,MV_FRAME_OUT_INFO stImageInfo,String fileName)
|
|
|
{
|
|
|
+ return SaveImageTemp(enSaveImageType, dataToSave, stImageInfo, fileName, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ public int SaveImageTemp(MV_SAVE_IAMGE_TYPE enSaveImageType,byte[] dataToSave,MV_FRAME_OUT_INFO stImageInfo,String fileName,String tempDirPath)
|
|
|
+ {
|
|
|
MV_SAVE_IMAGE_TO_FILE_PARAM_EX stSaveFileParam = new MV_SAVE_IMAGE_TO_FILE_PARAM_EX();
|
|
|
|
|
|
stSaveFileParam.imageType = enSaveImageType; // ch:需要保存的图像类型 | en:Image format to save
|
|
|
@@ -325,7 +335,9 @@ public class CameraUtils {
|
|
|
stSaveFileParam.data = dataToSave;
|
|
|
stSaveFileParam.methodValue = 1;
|
|
|
|
|
|
- String saveTempDir = MesClient.saveTempDir;
|
|
|
+ String saveTempDir = (tempDirPath != null && !tempDirPath.trim().isEmpty())
|
|
|
+ ? tempDirPath
|
|
|
+ : MesClient.getPhotoTempDir();
|
|
|
log.info("saveTempDir:"+saveTempDir);
|
|
|
File file = new File(saveTempDir);
|
|
|
if (!file.exists()) {
|
|
|
@@ -364,12 +376,26 @@ public class CameraUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ public static void ensureTempDirectory(String tempDirPath) {
|
|
|
+ try {
|
|
|
+ Files.createDirectories(Paths.get(tempDirPath));
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("创建临时目录失败: {}", tempDirPath, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean hasImageFiles(String directoryPath) {
|
|
|
+ List<File> files = getImageFilesInDirectory(directoryPath);
|
|
|
+ return files != null && !files.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 清空临时目录下的所有内容(保留目录本身)
|
|
|
* @param tempDirPath 临时目录路径
|
|
|
* @return 是否成功清空
|
|
|
*/
|
|
|
public static boolean cleanTempDirectory(String tempDirPath) {
|
|
|
+ ensureTempDirectory(tempDirPath);
|
|
|
Path tempDir = Paths.get(tempDirPath);
|
|
|
|
|
|
try {
|
|
|
@@ -417,21 +443,48 @@ public class CameraUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /** 将临时目录中的图片复制到 D 盘正式目录,不删除临时文件 */
|
|
|
+ public static String copyTempImagesToDirectory(String tempDirPath, String targetFolderName) {
|
|
|
+ try {
|
|
|
+ if (!hasImageFiles(tempDirPath)) {
|
|
|
+ log.warn("临时文件夹无图片: {}", tempDirPath);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ Path targetPath = Paths.get(MesClient.saveDir, targetFolderName);
|
|
|
+ Files.createDirectories(targetPath);
|
|
|
+ List<File> imageFiles = getImageFilesInDirectory(tempDirPath);
|
|
|
+ if (imageFiles == null || imageFiles.isEmpty()) {
|
|
|
+ log.warn("临时目录下无图片文件: {}", tempDirPath);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ for (File src : imageFiles) {
|
|
|
+ Path dest = targetPath.resolve(src.getName());
|
|
|
+ Files.copy(src.toPath(), dest, StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+ log.info("照片已保存到: {}", targetPath);
|
|
|
+ return targetPath.toString();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("照片保存失败: {} -> {}", tempDirPath, targetFolderName, e);
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
//提交-临时文件夹重命名
|
|
|
- public static String renameTempDirectory(String tempDirPath, String newFolderName) {
|
|
|
+ public static String renameTempDirectory(String tempDirPath, String newFolderName) {
|
|
|
try {
|
|
|
- // 1. 检查临时文件夹是否存在
|
|
|
Path sourcePath = Paths.get(tempDirPath);
|
|
|
- if (!Files.exists(sourcePath)) {
|
|
|
- log.info("临时文件夹不存在: {}", tempDirPath);
|
|
|
-
|
|
|
+ if (!Files.exists(sourcePath) || !hasImageFiles(tempDirPath)) {
|
|
|
+ log.warn("临时文件夹不存在或无图片: {}", tempDirPath);
|
|
|
+ return "";
|
|
|
}
|
|
|
|
|
|
- // 2. 生成目标文件夹路径(同一父目录下)
|
|
|
Path parentDir = sourcePath.getParent();
|
|
|
+ if (parentDir == null) {
|
|
|
+ log.error("无法解析父目录: {}", tempDirPath);
|
|
|
+ return "";
|
|
|
+ }
|
|
|
Path targetPath = parentDir.resolve(newFolderName);
|
|
|
|
|
|
- // 3. 执行重命名(原子操作,允许覆盖已存在文件夹)
|
|
|
Files.move(
|
|
|
sourcePath,
|
|
|
targetPath,
|
|
|
@@ -439,18 +492,22 @@ public class CameraUtils {
|
|
|
StandardCopyOption.REPLACE_EXISTING
|
|
|
);
|
|
|
|
|
|
- //重命名文件夹后,把重命名后文件夹下的图片上传到服务器
|
|
|
-// uploadAllImagesFromDirectory(targetPath.toString());
|
|
|
- log.info("文件夹重命名成功: " + targetPath);
|
|
|
+ ensureTempDirectory(tempDirPath);
|
|
|
+ log.info("文件夹重命名成功: {}", targetPath);
|
|
|
return targetPath.toString();
|
|
|
} catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ log.error("文件夹重命名失败: {} -> {}", tempDirPath, newFolderName, e);
|
|
|
}
|
|
|
return "";
|
|
|
}
|
|
|
|
|
|
+ public static void uploadAllImagesFromDirectory(String directoryPath, String folderName) {
|
|
|
+ String sn = MesClient.product_sn_z != null ? MesClient.product_sn_z.getText().trim() : "";
|
|
|
+ uploadAllImagesFromDirectory(directoryPath, folderName, sn, MesClient.OPRNO_FRONT, false);
|
|
|
+ }
|
|
|
+
|
|
|
//指定文件夹下文件上传
|
|
|
- public static void uploadAllImagesFromDirectory(String directoryPath,String folderName) {
|
|
|
+ public static void uploadAllImagesFromDirectory(String directoryPath, String folderName, String sn, String oprno, boolean resetSideOnSuccess) {
|
|
|
// 1. 获取目录下所有图片文件
|
|
|
List<File> imageFiles = getImageFilesInDirectory(directoryPath);
|
|
|
if (imageFiles.isEmpty()) {
|
|
|
@@ -463,8 +520,8 @@ public class CameraUtils {
|
|
|
|
|
|
HashMap<String, String> hashMap = new HashMap<>();
|
|
|
|
|
|
- hashMap.put("sn",MesClient.product_sn.getText());
|
|
|
- hashMap.put("oprno",MesClient.mes_gw);
|
|
|
+ hashMap.put("sn", sn);
|
|
|
+ hashMap.put("oprno", oprno);
|
|
|
hashMap.put("cateSn","LXXT");
|
|
|
hashMap.put("lineSn",MesClient.mes_line_sn);
|
|
|
hashMap.put("remark",folderName);
|
|
|
@@ -472,7 +529,23 @@ public class CameraUtils {
|
|
|
CommonResp<String> resp = HttpUtils.uploadFilesWithParams(imageFiles, url, hashMap);
|
|
|
if ("true".equals(resp.getResult())){
|
|
|
MesClient.setMenuStatus("上传成功",0);
|
|
|
- MesClient.resetScanA();
|
|
|
+ MesClient.getUser();
|
|
|
+ String ucode = MesClient.user_menu != null ? MesClient.user_menu.getText().trim() : "";
|
|
|
+ JSONObject qualityResp = com.mes.ui.DataUtil.sendQuality(sn, "OK", ucode, oprno);
|
|
|
+ if (qualityResp == null || !Boolean.TRUE.equals(qualityResp.getBoolean("result"))) {
|
|
|
+ MesClient.setMenuStatus("图片已上传,质量提交失败请重试", -1);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (MesClient.OPRNO_BACK.equals(oprno)) {
|
|
|
+ MesClient.resetScanBack();
|
|
|
+ MesClient.setMenuStatus("反面提交成功,请扫下一件", 0);
|
|
|
+ } else if (MesClient.OPRNO_FRONT.equals(oprno)) {
|
|
|
+ MesClient.resetScanFront();
|
|
|
+ MesClient.setMenuStatus("正面提交成功,请扫下一件", 0);
|
|
|
+ } else {
|
|
|
+ MesClient.resetScanA();
|
|
|
+ MesClient.setMenuStatus("结果提交成功,请扫下一件", 0);
|
|
|
+ }
|
|
|
}else{
|
|
|
MesClient.setMenuStatus("上传失败-"+resp.getMessage(),-1);
|
|
|
}
|