如何让java的ImageBuffer正确读取PNG文件? [英] How do I make java's ImageBuffer to read a PNG file correctly?

查看:1432
本文介绍了如何让java的ImageBuffer正确读取PNG文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,使用ImageBuffer和ImageIO打开一些PNG文件不起作用。这里有一些我正在使用的代码适用于调整JPG大小:

For some reason, opening up some PNG files using ImageBuffer and ImageIO does not work. Here's some code I am using that works fine for resizing/cropping JPGs:

BufferedImage image = ImageIO.read(new File(location));

BufferedImage croppedImage = image.getSubimage(
    cropInfo.getX(), cropInfo.getY(), cropInfo.getW(), cropInfo.getH());

BufferedImage resizedImage = new BufferedImage(
    TARGET_WIDTH, TARGET_HEIGHT, croppedImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(croppedImage, 0, 0, TARGET_WIDTH, TARGET_HEIGHT, null);
g.dispose();

this.changeContentType("image/png", ".png"); // not really relevant. just a property

ImageIO.write(resizedImage, "png", new File(location));

return resizedImage;

此函数的目标是获取给定的任何类型,调整大小并裁剪图像,然后使用相同的文件名将其保存到PNG。

The goal of this function is to take whatever type is given, resize and crop the image, and then save it to PNG with the same filename.

它适用于Windows,但如果我在Linux(lenny)上裁剪/调整大小,它会完全崩溃并抱怨类型该文件(它表示类型为0)。

It works on Windows, but if I crop/resize on Linux (lenny), it crashes altogether and complains about the type of the file (it says the type is 0).

java.lang.IllegalArgumentException: Unknown image type 0
    java.awt.image.BufferedImage.<init>(BufferedImage.java:490)
    trainingdividend.domain.file.ServerImage.resizeImage(ServerImage.java:68)
    trainingdividend.domain.file.ServerImage.cropAndResize(ServerImage.java:80)
    trainingdividend.service.user.UserAccountManagerImpl.cropAvatar(UserAccountManagerImpl.java:155)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

解决方案?

我可以使用另一个库吗?

Is there another library I can use altogether?

推荐答案

在Windows上运行我的函数时,croppedImaged.getType()返回因此,简单的hack是存储类型,检查它是否为0 ......如果是,则手动将值设置为5.

When running my function on Windows, croppedImaged.getType() returns the value 5. So, the simple "hack" is to store the type, check to see if it's 0... and if it is, set the value to 5 manually.

int imageType = croppedImage.getType();
if(imageType == 0) imageType = 5;

然后我们传入 imageType ,它应该适用于Linux。

We then pass in imageType instead and it should work on Linux.

我确信这有一个缺点,如果在其他情况下值为0,它会将其设置为5,这将是错误的。但是,这似乎适用于Linux上的常见图像类型,并没有造成任何问题。

I am sure this has the drawback that if the value is 0 in other cases, it will set it to 5 and that will be wrong. However, this seems to work for common image types on Linux and it hasn't caused any problems.

很明显,Java 1.6的Windows版本非常好,但Linux版本中有一个错误。

It's pretty clear that the Windows version of Java 1.6 is perfectly fine, but the Linux version has a bug in it.

这篇关于如何让java的ImageBuffer正确读取PNG文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆