tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Java > Image IO > How to scale an image

How to scale an image 

Java Image IO provides pluggable architecture for accessing images transparently. It is simple, flexible and allows us to plugin various image readers and writers with ease. The following example shows scaling an image using Java Image IO.

stpatricks_08.gif

File Name  :  
com/bethecoder/tutorials/imageio/ScaleImageTest.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.imageio;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScaleImageTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] argsthrows IOException {

    File file = new File("C:/Temp/stpatricks_08.gif");
    BufferedImage image = ImageIO.read(file);
    
    int scaleX = (int) (image.getWidth() 1.5);
    int scaleY = (int) (image.getHeight() 1.5);

    Image scaledImg = image.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);
    BufferedImage resizedImage = new BufferedImage(
        scaleX, scaleY, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = resizedImage.createGraphics();
    graphics.drawImage(scaledImg, 00, scaleX, scaleY, null);
    graphics.dispose();
    
    ImageIO.write(resizedImage, "png"new File("c:/Temp/scaled_image.png"))
  }

}
   

It gives the following output,

scaled_image.png


                    Print

   



Books Recommendation

 
 
bl  br