ezzitt
ezzitt

Reputation: 117

Creating a sub image in Java

I have an image 800x480. How do I create a new sub image of the following coordinates?

115, 235, 580, 202 (x, y, width, height)

Upvotes: 3

Views: 8731

Answers (1)

flavio.donze
flavio.donze

Reputation: 8100

You could use the BufferedImage, do the following to get a subimage:

BufferedImage img = ImageIO.read(new File("yourPath"));
BufferedImage subimage = img.getSubimage(115, 235, 580, 202);
ImageIO.write(subimage, "png", new File("outputPath"));

This example works with the filesystem, you could also use streams.

JavaDoc:

public BufferedImage getSubimage(int x, int y, int w, int h)

Parameters:
x - the X coordinate of the upper-left corner of the specified rectangular region
y - the Y coordinate of the upper-left corner of the specified rectangular region
w - the width of the specified rectangular region
h - the height of the specified rectangular region

Upvotes: 8

Related Questions