Image resizing in java -
Image resizing in java -
i have used below code resizing image , storing in temp file
public file resizeimage(inputstream fileinputstream, string filename, int neww,int newh) throws exception { graphics2d g = null; file file2= file.createtempfile("result",filenameutils.getextension(filename)); bufferedimage img = null; file tempfile1 = file.createtempfile("temp1file",filenameutils.getextension(filename)); // if not resizing send temp file1 fileoutputstream outputstream = null; try{ outputstream = new fileoutputstream(tempfile1); int read = 0; byte[] bytes = new byte[1024]; while ((read = fileinputstream.read(bytes)) != -1) { outputstream.write(bytes, 0, read); } outputstream.close(); outputstream = null; img = imageio.read(new fileinputstream(tempfile1)); int w = img.getwidth(); int h = img.getheight(); bufferedimage dimg = new bufferedimage(neww, newh, img.gettype()); g = dimg.creategraphics(); g.setrenderinghint(renderinghints.key_interpolation,renderinghints.value_interpolation_bilinear); g.drawimage(img, 0, 0, neww, newh, 0, 0, w, h, null); imageio.write(dimg, filenameutils.getextension(filename), file2); } catch(socketexception e) { e.printstacktrace(); throw e; } catch(exception e) { e.printstacktrace(); throw e; } { if(outputstream != null) outputstream.close(); if(fileinputstream != null) fileinputstream.close(); if(g != null) g.dispose(); if(img != null) img.flush(); } homecoming file2; }
when reading tempfile1 in highlighted line getting exception socket exception : socket closed. can help me this
you can utilize imgscalr purpose!
java image-resizing
Comments
Post a Comment