1

我正在使用 Java 并结合两个图像。我保存组合图像并想删除叠加层,但似乎仍有流打开。而且我不知道哪个以及如何关闭它们。

f_overlay并且f_image都是文件。

  // load source images
    BufferedImage image = null;
    BufferedImage overlay = null;
    try {
        log.debug(f_image.getAbsolutePath());
        log.debug(f_overlay.getAbsolutePath());
        image = ImageIO.read(f_image);
        overlay = ImageIO.read(f_overlay);
    } catch (IOException e) {
        log.error(e.getMessage());
    }

     // create the new image, canvas size is the max. of both image sizes
    int w = Math.max(image.getWidth(), overlay.getWidth());
    int h = Math.max(image.getHeight(), overlay.getHeight());
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.drawImage(overlay, 0, 0, null);

    // Save as new image
    try {
        ImageIO.write(combined, "PNG", f_image);
    } catch (IOException e) {
        log.error(e.getMessage());
    }

    // we can delete the overlay now
    log.debug("Delete overlay: " + f_overlay.delete());

有什么建议吗?

4

1 回答 1

0

我看不出你的代码有什么问题。

但是,如果读取成功,我只会删除文件 f_overlay。重要的是,在对文件对象调用 delete() 之后,不得将该对象用于其他任何事情,因此最好分配 f_overlay=null

boolean state = f_overlay.delete();
f_overlay=null;
log.debug("Delete ... "+state);
于 2013-10-08T15:01:16.713 回答