How to Read JPEG image into BufferedImage object using Java -
this not duplicated question here, because i've been searching solution long time in google , stackoverflow, , still cannot find solution.
i have these 2 images:
these 2 images same website same prefix , same format. difference size: first larger, while second smaller.
i downloaded both of images local folder , used java read them bufferedimage objects. however, when outputted bufferedimages local files, found first image red, while second normal(same original). what's wrong code?
byte[] rawdata = getrawbytesfromfile(imagefilepath); // code read raw bytes image file imageinputstream iis = imageio.createimageinputstream(new bytearrayinputstream(rawdata)); bufferedimage img = imageio.read(iis); fileoutputstream fos = new fileoutputstream(outputimagepath, false); imageio.write(img, "jpeg", fos); fos.flush(); fos.close();
ps: used gimp open first image , detected color mode 'srgb', no alpha or other stuff.
this apparently know bug, saw several suggestions (this one) suggest using toolkit#createimage
instead, apparently ignores color model.
i tested , seems work fine.
public class testimageio01 { public static void main(string[] args) { try { image in = toolkit.getdefaulttoolkit().createimage("c:\\hold\\test\\13652375852388.jpg"); joptionpane.showmessagedialog(null, new jlabel(new imageicon(in)), "yeah", joptionpane.information_message); bufferedimage out = new bufferedimage(in.getwidth(null), in.getheight(null), bufferedimage.type_int_rgb); graphics2d g2d = out.creategraphics(); g2d.drawimage(in, 0, 0, null); g2d.dispose(); imageio.write(out, "jpg", new file("c:\\hold\\test\\test01.jpg")); } catch (exception ex) { ex.printstacktrace(); } } }
nb- used joptionpane
verify incoming image. when using imageio
comes in red tinge, toolkit
looks fine.
updated
and explantation
Comments
Post a Comment