【问题标题】:Directly convert BufferedImage in a File object [duplicate]直接在 File 对象中转换 BufferedImage [重复]
【发布时间】:2014-02-14 10:39:50
【问题描述】:

我在互联网上看到了很多关于如何将文件转换为 BufferedImage 的示例,但我需要进行计数器转换。

我尝试了一些方法,但都很复杂。

我想知道是否有直接的方法可以做到这一点。

我的代码中有这个:

for (FileItem item : formItems) {
                // processes only fields that are not form fields
                if (!item.isFormField()) {
                    Date date = new Date();
                    String fileName = new File(item.getName()).getName();
                    String filePath = uploadPath + date.getTime() + fileName + ".tmp";
                    File storeFile = new File(filePath);

                    BufferedImage tempImg = ImageIO.read(storeFile);

                    //I make process in the tempImg

                    //I need save it
                    item.write(tempImg);
                }
            }

我不需要写一个 FileItem,而是我已经处理的 BufferedImage。

【问题讨论】:

  • 为什么不显示您尝试过的内容?为什么不告诉我们他们为什么不为你工作?为什么让我们重复您已经阅读过的信息?请注意,ImageIO.write(...) 用于写作,ImageIO.read(...) 用于阅读对我来说效果很好。
  • 对不起,我会解释得更好。
  • 我更新问题。
  • 好的.....那么你的代码可以工作吗?失败?抛出异常?什么?
  • 它没有给出错误,因为实际上问题是我该如何做到这一点。要使用 FileItem 保存,我需要一个文件,而我拥有的是 BufferedImage

标签: java


【解决方案1】:
File outputfile = new File("image.jpg");
ImageIO.write(bufferedImage, "jpg", outputfile);

这是你要找的吗?

【讨论】:

  • 不工作的人。在磁盘中,没有文件被写入。
  • Netbeans?我不使用Netbeans。是的,我查看了文件夹。显示绝对路径,去了那里,文件没有。
  • 你能用你的代码现在的样子更新你的 OP 吗?
  • 什么是 scaledImg?你所写的甚至不应该编译。首先创建“空图像”,如下所示:File outputfile = new File("emptyImage.jpg"); 然后读取文件信息并“绘制空图像”,如下所示:ImageIO.write(tempImg, "tmp", outputfile);
  • 我更改:ImageIO.write(tempImg, "jpg", storeFile); //item.write(tempImg);
猜你喜欢
  • 1970-01-01
  • 2015-01-21
  • 2016-11-25
  • 2014-03-11
  • 2012-09-15
  • 2020-02-16
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多