简体   繁体   English

异常“java.lang.IllegalArgumentException: image == null!” 在 ImageIO 中

[英]Exception "java.lang.IllegalArgumentException: image == null!" in ImageIO

I'm trying to write a png file from a bytes array.我正在尝试从字节数组中写入一个png文件。

My application is throwing the following exception "java.lang.IllegalArgumentException: image == null!" in ImageIO我的应用程序抛出以下异常"java.lang.IllegalArgumentException: image == null!" in ImageIO "java.lang.IllegalArgumentException: image == null!" in ImageIO

I tested my solution using a random bytes array as you can see in the code snippet below, but the exception still being thrown.我使用随机字节数组测试了我的解决方案,如下面的代码片段所示,但仍然抛出异常。

Can you help me to identify why is it being thrown ?你能帮我找出为什么它会被抛出吗?

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import java.util.Random;
    import javax.imageio.ImageIO;
    public class Main {

        public static void main(String[] args) throws IOException {
            String fn = new String("C:\\Users\\frogwine\\Desktop\\P1.png");

            byte [] data = new byte[256];

            Random r = new Random();
            r.nextBytes(data);
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ByteArrayInputStream input_stream= new ByteArrayInputStream(data);
            BufferedImage final_buffered_image = ImageIO.read(input_stream);
            ImageIO.write(final_buffered_image , "png", new File(fn) );
        }


    }

Stacktrace:堆栈跟踪:

    "C:\Program Files\Java\jdk-13.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\lib\idea_rt.jar=57315:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\frogwine\Documents\deneme\out\production\deneme com.berk.Main
    Exception in thread "main" java.lang.IllegalArgumentException: image == null!
        at java.desktop/javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
        at java.desktop/javax.imageio.ImageIO.getWriter(ImageIO.java:1608)
        at java.desktop/javax.imageio.ImageIO.write(ImageIO.java:1540)
        at com.berk.Main.main(Main.java:23)

    Process finished with exit code 1

Well that's no surprise.嗯,这并不奇怪。 From the ImageIO.read documentation:ImageIO.read文档:

If no registered ImageReader claims to be able to read the resulting stream, null is returned.如果没有注册的 ImageReader 声称能够读取结果流,则返回 null。

Writing random bytes to an array is not going to create valid image data in any format, because image formats have a standard structure that consists, at the very least, of a header.将随机字节写入数组不会以任何格式创建有效的图像数据,因为图像格式具有标准结构,该结构至少由标题组成。 For this reason, ImageIO.read returns null.因此, ImageIO.read返回 null。

As Leo also mentioned just passing random bytes to ImageIO.read method, you cannot get a random image.正如 Leo 还提到仅将随机字节传递给 ImageIO.read 方法,您无法获得随机图像。 You will get null image and when you passed it to write method it will throw this IllegalArgumentException.您将获得空图像,当您将其传递给 write 方法时,它将抛出此 IllegalArgumentException。

Instead you can create empty image with predefined width and height and set RGB color values to get random image as below.相反,您可以创建具有预定义宽度和高度的空图像并设置 RGB 颜色值以获得如下随机图像。

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Main {

    public static void main(String[] args) throws IOException {
        String fn = new String("C:\\Users\\frogwine\\Desktop\\P1.png");
        ImageIO.write(generateRandomImage(300) , "png", new File(fn) );
    }


    public static BufferedImage generateRandomImage(int numOfPixels) {
        final double ASPECT_RATIO = 4.0 / 3.0;
        int width = 0;
        int height = 0;
        byte[] rgbValue = new byte[3];
        BufferedImage image = null;
        SecureRandom random = null;

        try {
            random = SecureRandom.getInstance("SHA1PRNG");

            width = (int) Math.ceil(Math.sqrt(numOfPixels * ASPECT_RATIO));
            height = (int) Math.ceil(numOfPixels / (double) width);

            image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    random.nextBytes(rgbValue);
                    image.setRGB(x, y,
                            byteToInt(rgbValue[0]) + (byteToInt(rgbValue[1]) << 8) + (byteToInt(rgbValue[2]) << 16));
                }
            }

            return image;
        } catch (NoSuchAlgorithmException nsaEx) {
            nsaEx.printStackTrace();
        }
        return null;
    }

    public static int byteToInt(int b) {
        int i = b;
        if (i < 0) {
            i = i + 256;
        }
        return i;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取image == java.lang.IllegalArgumentException时发生异常:image == null - Exception while reading image ==java.lang.IllegalArgumentException: image == null 线程“ main”中的异常java.lang.IllegalArgumentException:image == null - Exception in thread “main” java.lang.IllegalArgumentException: image == null java.lang.IllegalArgumentException: 输入 == null! 当使用 ImageIO.read 将图像加载为 bufferedImage 时 - java.lang.IllegalArgumentException: input == null! when using ImageIO.read to load image as bufferedImage java.lang.illegalArgumentException:图片==空吗? - java.lang.illegalArgumentException: Image==null? java.lang.IllegalArgumentException:image == null - java.lang.IllegalArgumentException: image == null java.lang.IllegalArgumentException:输入== null! 在javax.imageio.ImageIO.read(未知来源) - java.lang.IllegalArgumentException: input == null! at javax.imageio.ImageIO.read(Unknown Source) HttpClient异常:java.lang.IllegalArgumentException:host参数为null - HttpClient exception: java.lang.IllegalArgumentException: host parameter is null 给定的 id 不能是 null;。 嵌套异常是 java.lang.IllegalArgumentException - The given id must not be null!; nested exception is java.lang.IllegalArgumentException 线程“ main”中的异常java.lang.IllegalArgumentException:in不能为null - Exception in thread “main” java.lang.IllegalArgumentException: in must not be null Flyway 嵌套异常是 java.lang.IllegalArgumentException: Name must not be null - Flyway nested exception is java.lang.IllegalArgumentException: Name must not be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM