Thursday, 10 November 2016

Crop image in Java

package com.gb.samples;

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

import javax.imageio.ImageIO;

public class Sample {

public static void main(String[] args) {
Image src = null;
try {
src = ImageIO.read(new File("C:/Users/vgrandhi/Desktop/sam/Converted_PdfFiles_to_Image/org_1.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

int x = 0, y = 0, w = 400, h = 1600;

BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);

try {
ImageIO.write(dst, "png", new File("C:/Users/vgrandhi/Desktop/sam/Converted_PdfFiles_to_Image/org_cropped.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

No comments:

Post a Comment