Crop Image Using Java

 import java.awt.image.BufferedImage;  
 import java.io.File;  
 import java.io.IOException;  
 import javax.imageio.ImageIO;  
 public class test {  
      public static void main(String[] args) throws IOException {  
           test t = new test();  
           t.getCropImage();  
      }  
      public void getCropImage() throws IOException {  
           int widthOfNewImage = 100;  
           int heightOfNewImage = 100;  
           int offsetFromLeft = 10;  
           int offsetFromTop = 100;  
           String path = "input.gif";  
           String newPath = "newImage.png";  
           String imageFormat = "png";  
           BufferedImage image = ImageIO.read(new File(path));  
           int height = image.getHeight();  
           int width = image.getWidth();  
           System.out.println("Height : " + height + "\nWidth : " + width);  
           BufferedImage out = image.getSubimage(offsetFromLeft, offsetFromTop,  
                     widthOfNewImage, heightOfNewImage);  
           ImageIO.write(out, imageFormat, new File(newPath));  
      }  
 }  

1 comment: