[英]Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException: im == null
hi i'm working on a project in which i need to make changes to BASE64 string of an image(jpg)...so at first when i didn't made any changes, the ImageReader was properly working and my image was displayed properly..but when i made changes to my BASE64 string the above exception came..i searched a lot and came to know that im==null comes when ByteStream is not jpeg,png,gif..etc..so what if i have a new type of ByteStream...what should i use?? 嗨,我正在进行一个项目,我需要更改BASE64图像的字符串(jpg)...所以,当我没有做任何更改时,ImageReader正常工作,我的图像显示正确..但是当我对我的BASE64字符串进行更改时,上面的异常来了..我搜索了很多并且知道当ByteStream不是jpeg,png,gif..etc以及如果我有的话,im == null一种新型的ByteStream ......我该怎么用? or what ever my BASE64 string is i need to convert that to an image..so how can i do that??
或者我的BASE64字符串是什么,我需要将其转换为图像..所以我该怎么办?
here is my code snippet:this is to convert BASE64 string to an image 这是我的代码片段:这是将BASE64字符串转换为图像
public static BufferedImage decodeToImage(String imageString) throws IOException {
BufferedImage image = null;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(imageString);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
}
catch (Exception e) {
e.printStackTrace();
}
ImageIO.write(image, "jpg", new File("d:/CopyOfTestImage.jpg"));
return image;
}
Have a look at the Javadocs for ImageIO.read : 看一下ImageIO.read的Javadocs :
Returns a
BufferedImage
as the result of decoding a suppliedInputStream
with anImageReader
chosen automatically from among those currently registered.返回一个
BufferedImage
作为解码提供的InputStream
的结果,其中ImageReader
是从当前注册的ImageReader
自动选择的。 TheInputStream
is wrapped in anImageInputStream
.InputStream
包装在ImageInputStream
。 If no registeredImageReader
claims to be able to read the resulting stream,null
is returned .如果没有已注册的
ImageReader
声称能够读取生成的流,则返回null
。 [emphasis mine][强调我的]
The read method can return null
, yet you are not checking for this. read方法可以返回
null
,但是你没有检查它。 In fact the method is probably returning null, which is why ImageIO.write
throws an exception when you pass null into it. 其实方法很可能返回null,这就是为什么
ImageIO.write
抛出当你传递null进去异常。
First things first, you need to check for error conditions and handle them appropriately (including the null return, but also including any exceptions that are thrown, which you currently catch and ignore). 首先,您需要检查错误条件并适当地处理它们(包括null返回,但也包括抛出的任何异常,您当前捕获并忽略这些异常)。
Now if you're getting null back from ImageIO.read, it means the bytes you passed into the read method did not appear to be a valid image in any known format. 现在,如果您从ImageIO.read返回null,则意味着您传入read方法的字节似乎不是任何已知格式的有效图像。 You need to look in more detail at the modifications you're making to the base64 string, and ensure that what you're doing is valid, and results in a valid image.
您需要更详细地查看对base64字符串所做的修改,并确保您正在执行的操作有效,并生成有效的图像。 Alternatively, if you're getting some other exception thrown, then you need to handle that appropriately.
或者,如果您正在获得其他异常,那么您需要适当地处理它。
(As a general rule, don't throw away/skip over errors, because then when things go wrong you have no idea why!) (作为一般规则,不要丢弃/跳过错误,因为当出现问题时你不知道为什么!)
change this line: 改变这一行:
ImageIO.write(image, "jpg", new File("d:/CopyOfTestImage.jpg"));
to something like this 这样的事情
image = ImageIO.read(getClass().getResource("/resources/CopyOfTestImage.jpg"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.