java - Inputstream to BufferedImage Conversion damages the file -
i uploading image files using servelt. want resize images. converts source bufferedimage using below lines.
inputstream imagestream = item.getinputstream();
bufferedimage imagebuffer = imageio.read(imagestream);
then resize image , write in location. but, of output files size 0
.
i using following code resize image.
affinetransform @ = new affinetransform(); if(sx != 0) at.scale( sx , sx ); affinetransformop ato = new affinetransformop(at, affinetransformop.type_bilinear); uploadimage = ato.filter(uploadimage, null); //uploadimage == bufferedimage
is there way convert inputstream bufferedimage without damaging image? sure image getting uploaded. but, after conversion bufferedimage, file damaged.
i uploading submitting form dopost() method. below line gives me inputstream list item.
inputstream imagestream = item.getinputstream();
and, writing
imageio.write(image, "jpg", new file(path + ".jpg"));
update
java.awt.image.imagingopexception: unable transform src image @ java.awt.image.affinetransformop.filter(unknown source) @ com.pricar.servlet.imageupload.imageuploadservlet.resize(imageuploadservlet.java:100) @ com.pricar.servlet.imageupload.imageuploadservlet.dopost(imageuploadservlet.java:74) @ javax.servlet.http.httpservlet.service(httpservlet.java:641) @ javax.servlet.http.httpservlet.service(httpservlet.java:722) @ org.mortbay.jetty.servlet.servletholder.handle(servletholder.java:511) @ org.mortbay.jetty.servlet.servlethandler.handle(servlethandler.java:390) @ org.mortbay.jetty.security.securityhandler.handle(securityhandler.java:216) @ org.mortbay.jetty.servlet.sessionhandler.handle(sessionhandler.java:182) @ org.mortbay.jetty.handler.contexthandler.handle(contexthandler.java:765) @ org.mortbay.jetty.webapp.webappcontext.handle(webappcontext.java:418) @ org.mortbay.jetty.handler.contexthandlercollection.handle(contexthandlercollection.java:230) @ org.mortbay.jetty.handler.handlercollection.handle(handlercollection.java:114) @ org.mortbay.jetty.handler.handlerwrapper.handle(handlerwrapper.java:152) @ org.mortbay.jetty.server.handle(server.java:326) @ org.mortbay.jetty.httpconnection.handlerequest(httpconnection.java:542) @ org.mortbay.jetty.httpconnection$requesthandler.content(httpconnection.java:943) @ org.mortbay.jetty.httpparser.parsenext(httpparser.java:756) @ org.mortbay.jetty.httpparser.parseavailable(httpparser.java:218) @ org.mortbay.jetty.httpconnection.handle(httpconnection.java:404) @ org.mortbay.io.nio.selectchannelendpoint.run(selectchannelendpoint.java:410) @ org.mortbay.thread.queuedthreadpool$poolthread.run(queuedthreadpool.java:582)
any suggesstions or link woulb appreciative!!!
thanks!
the reason code isn't working in
uploadimage = ato.filter(uploadimage, null); //uploadimage == bufferedimage
your destination image null.
you have create new bufferedimage put scaled version into, this:
bufferedimage dstimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); ato.filter(uploadimage, dstimage);
then, save dstimage (using imageio.write).
edit:
an easier way scale down image draw dstimage @ right size:
int dstwidth = 100; int dstheight = 100; inputstream imagestream = item.getinputstream(); bufferedimage srcimage = imageio.read(imagestream); if (srcimage == null) { system.err.println("no source image!"); } bufferedimage dstimage = new bufferedimage(dstwidth, dstheight, bufferedimage.type_int_rgb); dstimage.getgraphics().drawimage( srcimage, 0, 0, dstwidth, dstheight, null); imageio.write(dstimage, "jpg", new file(path + ".jpg"));
Comments
Post a Comment