Monday, July 18, 2011

How to resize image using java.

How to resize image using java.


Steps

1. Import useful libraries in to our class.
    here we need

    import java.awt.image.BufferedImage;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;


2. Then we need to create a function ie 'imageIconResize'
   for resizing the image.and we need to pass some parameters in to
   that function
   
   here is the function.


  public BufferedImage imageIconResize(Image img,int postionX,int 
  postionY, int width, int hight)

  {

  BufferedImage bi = new BufferedImage(img.getWidth(null),
  img.getHeight(null),  BufferedImage.SCALE_FAST);
  Graphics g = bi.createGraphics();
  g.drawImage(img, postionX, postionY, width, hight, null, null);
  return bi;


  }
3. Then pass the parameters in to our function. for that we must
   have a image.
   In my example i have byte array of image.then convert it to
   ImageIcon.

   ie

  iconImage = new ImageIcon(bytes);
   
   I use getImage() function for get image from icon object.
   
  iconImage = new ImageIcon(bytes);
  Image img = iconImage.getImage();


4. So we have a image then we call the function it return a
   BufferedImage object.
   we need to convert it to ImageIcon.
   
 BufferedImage imageIconResize = imageIconResize(img,10,15, 50, 50);
 ImageIcon newIcon = new ImageIcon(imageIconResize);

   
5. Changing the value of parameters will change the size of the
   image.

No comments:

Post a Comment