java - Error with code to make bufferedImage smaller -


i have image (147 kb), , want downsize under 100kb. code below attempts this, when image comes out on other side, widht , height scaled down, disk space on image goes 147 250! it's supposed smaller not higher...

can please tell me why code isn't doing this?

thanks

//save image         bufferedimage resizeimagepng = resizeimage(originalimage, type, newlargeimagelocation);      //resize , save     imageio.write(resizeimagepng, "png", new file(newsmallimagelocation));    //create new image     private static bufferedimage resizeimage(bufferedimage originalimage, int type, string newlargelocation) throws exception{              //get size of image             file file =new file(newlargelocation);              //file size in kbs             double bytes = file.length();             double kilobytes = bytes/1024;              double smallwidth = originalimage.getwidth();             double smallheight = originalimage.getheight();              double downmult = 1;              //if image large downsize fit size             if (kilobytes>max_image_size_in_kylobites) {                  downmult = max_image_size_in_kylobites/kilobytes;                  smallwidth *= downmult;                 smallheight *= downmult;             }              //final dimensions             int finalheight = (int)smallheight;             int finalwidth = (int)smallwidth;              //init after             bufferedimage after = new bufferedimage(finalwidth, finalheight, bufferedimage.type_int_argb);              //scale             affinetransform @ = new affinetransform();             at.scale(downmult, downmult);              //scale op             affinetransformop scaleop = new affinetransformop(at, affinetransformop.type_bilinear);             after = scaleop.filter(originalimage, after);              //return after             return after;         } 

2 issues, have resizing program resize images bit different:

first create dimension object (not needed designwise seems bit better) starting bufferedimage object called original

final int width = original.getwidth(); final int height = original.getheight(); final dimension d = new dimension(width, height); 

in case not filesize maximum width and/or height, without getting details calculate new dimension object based upon 1 created above. new dimension object called scaled.

this how obtain new scaled buffered image:

public bufferedimage resizeexact(final bufferedimage original, final dimension scaled, final dimension offset) {     final image newimage = original.getscaledinstance(scaled.width, scaled.height, image.scale_smooth);     final bufferedimage bufferedimage = new bufferedimage(newimage.getwidth(null),                                                           newimage.getheight(null),                                                           bufferedimage.type_int_bgr);     bufferedimage.creategraphics().drawimage(newimage, offset.width, offset.height, null);     return bufferedimage; } 

using code obtain bufferedimage called resizedimage needs written outputstream out, use code:

imageio.write(resized, "jpg", out); out.close(); 

and here 2nd issue: use standard imageio class write bufferedimage jpeg file. issue here jpeg encoders typically use compression factor, higher factor smaller resulting file more quality loss in resulting file. code uses default compression factor of 70%. suits me fine. if yoy want change this, reference explains how this: setting jpg compression level imageio in java. note here changing 100% lower compression factor.

your code snippet doesn't show how create jpeg file (i assume use jpeg well). if original image compressed ratio of 40% (unlikely because lousy image assume) un-jpeged have 100%, if wanted reduce file size 80% reduce image size 80%, if compress using 70% compression factor end result aroung 56% bigger 40%.

hope helps. if specify how write jeg image (maybe using library standard of javax.imageio) there might way specify compression factor.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -