resize - Java, Image turns black after resizing File in Java -
image turns black after resizing multipartfile in java ..
i trying save file in sql db.. need down image quality , size because of memory..after resizing gets black ..
any ideas?
private static int img_height = 1280; private static int img_width= 860; private static int imgtype = image.scale_smooth; try { bytearr = doc.getbytes(); imageicon imgicon = new imageicon(bytearr); int height = imgicon.geticonheight(); int width = imgicon.geticonwidth(); if(height > img_height || width > img_width){ int newwidth = width; int newheight = height; if(height > img_height){ newheight = img_height; } if(width > img_width){ newwidth = img_width; } system.out.println("img newheight = "+newheight); system.out.println("img newwidth = "+newwidth); image newimage = imgicon.getimage(); newimage = newimage.getscaledinstance(newwidth, newheight, imgtype); bufferedimage buffimage = new bufferedimage(newwidth, newheight, bufferedimage.type_int_rgb); graphics2d g2 = buffimage.creategraphics(); g2.setrenderinghint(renderinghints.key_interpolation, renderinghints.value_interpolation_bilinear); g2.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality); g2.drawimage(newimage, 0, 0, width, height, null); // g2.setcolor(color.white); // g2.fillrect(0, 0, newwidth, newheight); // g2.setcomposite(alphacomposite.srcover); // affinetransform scaletransform = affinetransform.getscaleinstance(width / (double) newimage.getwidth(null), height / (double) newimage.getheight(null)); // g2.drawimage(newimage, scaletransform, null); g2.dispose(); baos = new bytearrayoutputstream(); imageio.write(buffimage,"jpg", baos);
i had similar issue images transparent background. passing color.white
in graphics.drawimage
background color helped avoid black background.
graphics2d graphics = buffimage.creategraphics(); graphics.drawimage(newimage, 0, 0, image_width, image_height, color.white, null); graphics.setcomposite(alphacomposite.getinstance(alphacomposite.dst_in, 1.0f));
Comments
Post a Comment