【发布时间】:2021-11-23 13:17:43
【问题描述】:
我有一个带有按钮的 jframe 来裁剪图像,并且我使用 Marvin libray 来处理图像。每当我单击按钮时,在我关闭 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);
}
【问题讨论】:
-
从您问题中的代码 sn-ps 看来,您正在裁剪图像克隆,但您正在保存与加载相同的图像。
-
@abra,我想
crop()函数正在覆盖原始图像。我的猜测是参数类似于(来源,目的地)。无论哪种方式,这都不能解释 OP 的问题 -
我不知道为什么会这样——我们也不知道。这应该实时发生,除非 Marvin API 进行某种缓存。您需要阅读 API,因为基于对它应该如何工作的最佳猜测,发布的代码似乎没有任何问题。与此同时,无需使用 Marvin API。只需使用 ImageIO 类来读取/写入图像。然后,您可以使用 BufferedImage 类来裁剪图像。该代码几乎与此处发布的代码相同。
标签: java swing marvin-framework