简体   繁体   English

javaxt图像抛出异常

[英]javaxt image throwing exception

I am trying to resize an image using the javaxt.io.Image library. 我正在尝试使用javaxt.io.Image库调整图像的大小。 I am getting with the following code 我得到以下代码

//resizing image using a factor
                if ((fileSizeInMB>=2)&&(fileSizeInMB)<=4)//if the size is between 2 and 4
                {
                    factor=3;
                    image.resize(image.getWidth()/factor, image.getHeight()/factor);

                    image.saveAs(saveDirectory
                            + emaReceiverUtility.getNameOfAttachment(emailMessage)
                            + "_" + fileName);

                }

the following error. 以下错误。

java.lang.NullPointerException at javaxt.io.Image.getWidth

Can you help me figure out what I am doing wrong? 您能帮我弄清楚我做错了什么吗? What the code is trying to do is to divide the width and the height of the image by a factor. 代码试图做的是将图像的宽度和高度除以一个因子。 In this case the factor is 3. But the code is not working and I am having an exception at the following line 在这种情况下,因子为3。但是代码无法正常工作,我在下一行遇到异常

image.resize(image.getWidth()/factor, image.getHeight()/factor);

I know what is going on here. 我知道这是怎么回事。 When creating an instance of image, I just provided a name, instead of pointing a real image file. 创建图像实例时,我只是提供了一个名称,而不是指向实际的图像文件。 I changed the code like the following and now it is working. 我更改了如下代码,现在可以正常工作了。

int factor=1;
                Image image=new Image(part.getInputStream());

                log.info("the file name is "+fileName);
                //resizing image using a factor
                if ((fileSizeInMB>2)&&(fileSizeInMB)<=4)//if the size is between 2 and 4
                {
                    factor=3;
                    image.resize(image.getWidth()/factor, image.getHeight()/factor);

                    image.saveAs(saveDirectory
                            + emaReceiverUtility.getNameOfAttachment(emailMessage)
                            + "_" + fileName);

                }

Now the only problem is the code is not saving the resized image. 现在唯一的问题是代码没有保存调整大小的图像。 It keeps saving the old non-resized file. 它将继续保存旧的未调整大小的文件。 [EDIT] It is actually saving the file correctly, I just forgot a portion of code somewhere that re save the original file and since both files have the same name, the resized file is replaced all this time letting me think that the code is not working properly. [编辑]实际上是正确地保存了文件,我只是忘记了重新保存原始文件的某个地方的代码,并且由于两个文件都具有相同的名称,因此一直在替换大小调整后的文件,这让我认为代码不是好好工作。 Thanks all. 谢谢大家

The file does not exists. 该文件不存在。

Regards 问候

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM