[英]Java file operations after opening image with ImageIO
I am working with Java and combine two images. 我正在使用Java并合并两个图像。 I save the combined image and want to delete the overlay, but it seems there are still streams open. 我保存了合并的图像并想要删除叠加层,但是似乎仍有流在打开。 And i don't know which and how to close them. 而且我不知道哪些以及如何关闭它们。
f_overlay
and f_image
are both Files. 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());
Are there any suggestions? 有什么建议吗?
I can't see anything wrong in your code. 我看不到您的代码有什么问题。
However, I would only delete the file f_overlay if the reading was successful. 但是,如果读取成功,我只会删除文件f_overlay。 Important, after you call delete() on the file object, you must not use the object for anything else, so best is to assign f_overlay=null 重要的是,在对文件对象调用delete()之后,不得将其用于其他任何事情,因此最好分配f_overlay = null
boolean state = f_overlay.delete();
f_overlay=null;
log.debug("Delete ... "+state);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.