将一个 BufferedImage 传递到一个用于图像文件的 FileInputStream 中

huangapple go评论72阅读模式
英文:

Passing a BufferedImage into a FileInputStream meant for image files

问题

我有两段独立的代码,需要让它们一起工作,其中一段是我自己编写的,另一段来自另一个项目。基本上,项目的一部分正在创建一个缓冲图像。以下是其中的一部分代码片段:

BufferedImage screenshot = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);

在另一段软件中,它接受一个图像文件,然后将其作为 JSON/POST 数据传递给另一个服务。以下是它接受文件的部分代码:

try(InputStream file = new FileInputStream("temp.png")) {
    sendFile(out, "file", file, "temp.png");
}

我在使用 Java 处理图像方面经验不多,我目前的解决方案是让第一个程序将 BufferedImage 写入文件,如下所示:

File tempFile = new File("/", "temp.png");
ImageIO.write(screenshot, "PNG", tempFile);

然后第二段代码能够正确处理它。问题出在需要删除临时文件以及由此引起的问题。有没有简化这个过程的方法?提前谢谢。

英文:

I have two independant pieces of code that I need to work together, one written by myself and one that is from another project. I essentially have one part of the project that is creating a buffered image. Here is a snippet from it:

BufferedImage screenshot = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);

In the other piece of software, it accepts an image file to then pass on as JSON/POST data for another service. Here is it accepting a file:

try(InputStream file = new FileInputStream("temp.png")) {
                sendFile(out, "file", file, "temp.png");
            }

I don't have much experience working with Images in Java, my current solution is to have the first program write the BufferedImage to a file like this:

File tempFile = new File("/", "temp.png");
ImageIO.write(screenshot, "PNG", tempFile);

Then the second piece of code is able to deal with it properly. The problem arises from it needing to delete the temp file and the issues of arising from that. Is there any way to simplify this? Thanks in advance.

答案1

得分: 0

解决方案是通过来自@Mas和评论者的链接找到的。

第一组代码添加了一个名为"screenshotOutput"的ByteArrayOutputStream

ByteArrayOutputStream screenshotOutput = new ByteArrayOutputStream();
ImageIO.write(screenshot, "png", screenshotOutput);

第二组代码被修改以接受一个ByteArrayInputStream流。

try(InputStream file = new ByteArrayInputStream(screenshotOutput.toByteArray());) {
                sendFile(out, "file", file, "temp.png");
            }

这解决了我的问题。再次感谢昨天的评论者们。

英文:

Solution was found thanks to the link from @Mas and commenters.

The first set of code had a ByteArrayOutputStream added called "screenshotOutput"

ByteArrayOutputStream screenshotOutput = new ByteArrayOutputStream();
ImageIO.write(screenshot, "png", screenshotOutput);

The second set of code was modified to accept a ByteArrayInput stream.

try(InputStream file = new ByteArrayInputStream(screenshotOutput.toByteArray());) {
                sendFile(out, "file", file, "temp.png");
            }

This fixed my issue. Thank you again to the commenters from yesterday.

huangapple
  • 本文由 发表于 2020年10月19日 13:46:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64421755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定