image - How do I make java's ImageBuffer to read a PNG file correctly? -
for reason, opening png files using imagebuffer , imageio not work. here's code using works fine 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 relevant. property imageio.write(resizedimage, "png", new file(location)); return resizedimage;
the goal of function take whatever type given, resize , crop image, , save png same filename.
it works on windows, if crop/resize on linux (lenny), crashes altogether , complains type of file (it says type 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)
solutions?
is there library can use altogether?
when running function on windows, croppedimaged.gettype() returns value 5. so, simple "hack" store type, check see if it's 0... , if is, set value 5 manually.
int imagetype = croppedimage.gettype(); if(imagetype == 0) imagetype = 5;
we pass in imagetype instead , should work on linux.
i sure has drawback if value 0 in other cases, set 5 , wrong. however, seems work common image types on linux , hasn't caused problems.
it's pretty clear windows version of java 1.6 fine, linux version has bug in it.
Comments
Post a Comment