How do I make java's ImageBuffer to read a PNG file correctly?

前端 未结 3 1163
一生所求
一生所求 2021-01-12 03:02

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:



        
3条回答
  •  独厮守ぢ
    2021-01-12 04:03

    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;
    

    We then pass in imageType instead and it should work on 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.

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

提交回复
热议问题