我有一个jframe,上面有一个裁剪图像的按钮,我使用Marvin库来处理图像。每当我点击按钮时,在我关闭jframe窗口后,就会在文件夹中创建新的裁剪图像。我不知道为什么会发生这种情况,也不知道如何让它实时运行。感谢的任何帮助
Gui.java
cropBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Cropped successfully");
ImageManipulator.cropImage(60, 32, 182, 62);
}
});
作物方法
static MarvinImage cropImage(int x, int y, int width, int height) {
MarvinImage image = MarvinImageIO.loadImage("image.jpeg");
crop(image.clone(), image, x, y, width, height);
MarvinImageIO.saveImage(image, String.format("cropped-image-%s.%s", dateFormat.format(new Date()), format));
return image;
}
public static void crop(MarvinImage imageIn, MarvinImage imageOut, int x, int y, int width, int height) {
x = Math.min(Math.max(x, 0), imageIn.getWidth());
y = Math.min(Math.max(y, 0), imageIn.getHeight());
if (x + width > imageIn.getWidth()) {
width = imageIn.getWidth() - x;
}
if (y + height > imageIn.getHeight()) {
height = imageIn.getHeight() - y;
}
crop = checkAndLoadImagePlugin(crop, "org.marvinproject.image.segmentation.crop");
crop.setAttribute("x", x);
crop.setAttribute("y", y);
crop.setAttribute("width", width);
crop.setAttribute("height", height);
crop.process(imageIn, imageOut);
}
我不能100%确定,但我认为您的问题是在事件调度线程上运行裁剪函数。
尝试给它自己的线程:
cropBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Cropped successfully");
new Thread(new ImageThread()).start();
}
});
public class ImageThread implements Runnable{
@Override
public void run() {
ImageManipulator.cropImage(60, 32, 182, 62);
}
}