【问题标题】:Image processing delayed on JavaJava 上的图像处理延迟
【发布时间】: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


【解决方案1】:

我不是 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);
         }
}

【讨论】:

  • 谢谢,很遗憾问题依旧
  • 嗯,我还在想事件调度线程上发生了一些事情。这是有道理的。您是否使用java.awt.EventQueue.invokeLater() 运行您的框架?
  • 不,我没有使用它
  • @RowanFrank,哦。 Java Swing 不是线程安全的。它要求稍后使用调用。见这里stackoverflow.com/questions/5780936/…
  • 让我看看这个,我不知道那个。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 2013-03-13
  • 2021-10-04
相关资源
最近更新 更多