我有一个带有按钮的 jframe 来裁剪图像,我使用 Marvin libray 来处理图像。每当我单击按钮时,在我关闭 jframe 窗口后,都会在文件夹中创建新的裁剪图像。我不知道为什么会发生这种情况以及如何使其实时工作。感谢任何帮助
桂.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);
}