我正在尝试使用Java构建管道架构(当前使用NetBeans 8.2,Windows 10平台64位,jdk 8.0)。一个程序旨在打开一个jpg图像,将其内部存储为BufferedImage并将其传递给下一个程序(通过System.out),该程序假定是从System.in输入流中获取字节,然后将其转换回BufferedImage并应用一些过滤器,以便它将图像再次传递到System.out输出流。我应该在Windows Shell中执行的命令是:
java -jar OpenImage.jar "lenna.jpg" | java -jar ApplyBlur.jar 10 | java -jar SaveImage.jar "result.jpg" ... (and so on)
I have already tried the ImageIO.write method to write to the System.out stream and read it back again with ImageIO.read, but the BufferedImage created is always null. I've also tried to convert the image to byte array (as advised here https://www.tutorialspoint.com/How-to-convert-Byte-Array-to-Image-in-java), pass it through a ByteArrayOutputStream and finally to the System.out, but the image is still null.
// The OpenImage program:
image = ImageIO.read(new File("lenna.jpg"));
if(image == null) {
System.err.println("Image is null!");
}
ImageIO.write(image, "jpg", System.out);
// The SaveImage program:
BufferedImage image = ImageIO.read(System.in);
我期望从System.in成功读取ImageIO.read(InputStream)方法,但是生成的图像始终为null。