增加ftp来给视觉服务修改照片
parent
b16f24dd96
commit
cf91bb8b65
@ -0,0 +1,74 @@
|
|||||||
|
package com.zhehekeji.web.service.client.image;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.Semaphore;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
public class ImageQueueReplacer {
|
||||||
|
|
||||||
|
private final String targetDirectory;
|
||||||
|
private final LinkedBlockingQueue<BufferedImage> imageQueue;
|
||||||
|
private final Semaphore semaphore;
|
||||||
|
private volatile boolean running = true;
|
||||||
|
|
||||||
|
public ImageQueueReplacer(String targetDirectory) {
|
||||||
|
this.targetDirectory = targetDirectory;
|
||||||
|
this.imageQueue = new LinkedBlockingQueue<>();
|
||||||
|
this.semaphore = new Semaphore(0); // 初始信号量为0
|
||||||
|
}
|
||||||
|
|
||||||
|
public void queueImage(BufferedImage image) {
|
||||||
|
imageQueue.add(image);
|
||||||
|
semaphore.release(); // 图片入队时释放信号量,告知有图片可处理
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startAutoReplace(String imageName) {
|
||||||
|
Thread autoReplaceThread = new Thread(() -> {
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
semaphore.acquire(); // 等待信号量,表明有图片或开始指令
|
||||||
|
BufferedImage imageToReplace;
|
||||||
|
while ((imageToReplace = imageQueue.poll()) != null) {
|
||||||
|
replaceImage(imageToReplace, imageName);
|
||||||
|
}
|
||||||
|
// 队列为空时,仅在第一次启动时执行覆盖(根据需求调整)
|
||||||
|
if (semaphore.availablePermits() == 0 && imageQueue.isEmpty()) {
|
||||||
|
replaceImage(null, imageName); // 使用null表示无图片可覆盖
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
autoReplaceThread.setDaemon(true);
|
||||||
|
autoReplaceThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void replaceImage(BufferedImage image, String imageName) {
|
||||||
|
if (image != null) {
|
||||||
|
File outputFile = new File(targetDirectory, imageName);
|
||||||
|
try {
|
||||||
|
ImageIO.write(image, "jpg", outputFile);
|
||||||
|
System.out.println("Image saved: " + outputFile.getAbsolutePath());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error saving image: " + e.getMessage());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
System.out.println("No image available to replace.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package com.zhehekeji.web.service.client.image;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class ImageSaver {
|
||||||
|
|
||||||
|
private final String targetDirectory;
|
||||||
|
|
||||||
|
public ImageSaver(String targetDirectory) {
|
||||||
|
this.targetDirectory = targetDirectory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void saveImage(BufferedImage image, String imageName) {
|
||||||
|
File outputFile = new File(targetDirectory, imageName);
|
||||||
|
try {
|
||||||
|
ImageIO.write(image, "jpg", outputFile);
|
||||||
|
System.out.println("Image saved: " + outputFile.getAbsolutePath());
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error saving image: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue