java - How to create an image from an InputStream, resize it and save it? -
i have code inputstream
, create image:
part file; // more code try { inputstream = file.getinputstream(); file f = new file("c:\\imagenesalmacen\\qr\\olakease.jpg"); outputstream os = new fileoutputstream(f); byte[] buf = new byte[1024]; int len; while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } os.close(); is.close(); } catch (ioexception e) { system.out.println("error"); }
the problem have resize image before create if inputstream
so how resize inputstream
, create resized image. want set largest side of image 180px , resize other side proportion.
example:
image = 289px * 206px
resized image = 180px* 128px
i did this:
try { inputstream = file.getinputstream(); image image = imageio.read(is); bufferedimage bi = this.createresizedcopy(image, 180, 180, true); imageio.write(bi, "jpg", new file("c:\\imagenesalmacen\\qr\\olakease.jpg")); } catch (ioexception e) { system.out.println("error"); } bufferedimage createresizedcopy(image originalimage, int scaledwidth, int scaledheight, boolean preservealpha) { int imagetype = preservealpha ? bufferedimage.type_int_rgb : bufferedimage.type_int_argb; bufferedimage scaledbi = new bufferedimage(scaledwidth, scaledheight, imagetype); graphics2d g = scaledbi.creategraphics(); if (preservealpha) { g.setcomposite(alphacomposite.src); } g.drawimage(originalimage, 0, 0, scaledwidth, scaledheight, null); g.dispose(); return scaledbi; }
and did not use other code.
hope helps someone!
Comments
Post a Comment