Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce image file size in java

Tags:

java

I want to reduce image file size of an uploaded image before saving it into server (to reduce loading time). how can I do it using java?

like image 735
Anand Avatar asked Dec 15 '10 11:12

Anand


People also ask

How do I make an image smaller in Java?

You can resize an image in Java using the getScaledInstance() function, available in the Java Image class. We'll use the BufferedImage class that extends the basic Image class. It stores images as an array of pixels.


2 Answers

This kind of question has been answered quite a few times on this site. I suggest you check out How to resize the original image into a common size of image in Java? or search for java image resize on this site.

like image 173
Gabriel Reid Avatar answered Oct 14 '22 19:10

Gabriel Reid


May this following code help you it will resize and keep quality of image.

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
             if (!isMultipart) {
             } else {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                List items = null;
                try {
                     items = upload.parseRequest(request);
                } catch (FileUploadException e) {
                     System.out.println("Unable to load image" +  e.getMessage());}
                Iterator itr = items.iterator();
                while(itr.hasNext()) {
                     System.out.println(items.size());
                       FileItem item = (FileItem) itr.next();
                       if (!item.isFormField()) {
                          try {
                                 int size = 200;// size of the new image.
                                 //take the file as inputstream.
                                 InputStream imageStream = item.getInputStream();
                                 //read the image as a BufferedImage.
                                 BufferedImage image = javax.imageio.ImageIO.read(imageStream); 
                                 //cal the sacleImage method.
                                 BufferedImage newImage = scaleImage(image, size);
                                 String path = getServletContext().getRealPath("/image");
                                 //write file.
                                 File file = new File(path, "testimage.jpg");
                                 ImageIO.write(newImage, "JPG", file);
                          } catch (Exception e) {
                               System.out.println("Unable to save the image" + e.getMessage());
                               //System.out.println(path);
                  }//if
                }//iter
        }
    private BufferedImage scaleImage(BufferedImage bufferedImage, int size) {
         double boundSize = size;
            int origWidth = bufferedImage.getWidth();
            int origHeight = bufferedImage.getHeight();
            double scale;
            if (origHeight > origWidth)
                scale = boundSize / origHeight;
            else
                scale = boundSize / origWidth;
             //* Don't scale up small images.
            if (scale > 1.0)
                return (bufferedImage);
            int scaledWidth = (int) (scale * origWidth);
            int scaledHeight = (int) (scale * origHeight);
            Image scaledImage = bufferedImage.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
            // new ImageIcon(image); // load image
            // scaledWidth = scaledImage.getWidth(null);
            // scaledHeight = scaledImage.getHeight(null);
            BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = scaledBI.createGraphics();
                 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(scaledImage, 0, 0, null);
            g.dispose();
            return (scaledBI);
    }
like image 38
Umesh Markande Avatar answered Oct 14 '22 18:10

Umesh Markande