Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Simple Image resizing example in Java

    • 0
    • 4
    • 1
    • 4
    • 0
    • 0
    • 0
    • 0
    • 524
    Comment on it

    Sometimes we need to resize image in Java to fulfil our requirement. You can use the below code to resize image in Java very easily:

    1. Define the dimension boundary of which you want resized image as below
    2. Dimension boundary = new Dimension(350, 350); // here you can provide custom height/width
    3. Get the Scaled dimension of the image you want to resize. In this method you will pass Original imageSize and boundary.
    4. /**
       * This method will retrieve scaled dimension of an image
       * @param imgSize
       * @param boundary
       * @return
       */
      public static Dimension getScaledDimension(BufferedImage imgSize, Dimension boundary) {
      
          int original_width = imgSize.getWidth();
          int original_height = imgSize.getHeight();
          int bound_width = boundary.width;
          int bound_height = boundary.height;
          int new_width = original_width;
          int new_height = original_height;
      
          // first check if we need to scale width
          if (original_width > bound_width) {
              //scale width to fit
              new_width = bound_width;
              //scale height to maintain aspect ratio
              new_height = (new_width * original_height) / original_width;
          }
      
          // then check if we need to scale even with the new height
          if (new_height > bound_height) {
              //scale height to fit instead
              new_height = bound_height;
              //scale width to maintain aspect ratio
              new_width = (new_height * original_width) / original_height;
          }
      
          return new Dimension(new_width, new_height);
      }
    5. After getting scaled dimension you can resize you image by using the below method:
     //Read the input stream to a BufferedImage  
       BufferedImage sourceImage = ImageIO.read(in);  
    
    /**
     * This method will resize you image according the provided dimension
     * @param originalImage
     * @param type
     * @param changedSize
     * @return
     */
    private  BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, Dimension changedSize){
    
        BufferedImage resizedImage = new BufferedImage(changedSize.width, changedSize.height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, changedSize.width, changedSize.height, null);
        g.dispose();    
        g.setComposite(AlphaComposite.Src);
    
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
    
        return resizedImage;
    }

    Hope this will help you.Good Luck :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: