Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cut part of an image from an existing image in java

Currently working in Java, i'd like to be able to select part of an image using the mouse pointer co ordinates. The area selected then needs to be cut from the existing image and used to create a new separate image.

Just like a few pointers on how to go about it. Thanks.

like image 787
Julio Avatar asked Jun 07 '10 04:06

Julio


1 Answers

If you want the user to be able to "click-and-drag" to select a rectangle you need to implement a MouseMotionListener. Have a look at the mouseDragged method:

void mouseDragged(MouseEvent e)
          Invoked when a mouse button is pressed on a component and then dragged.

When you need to get hold of the sub-image, you simply use

public BufferedImage getSubimage(int x, int y, int w, int h)
          Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.

If you want to save the resulting image to disk, I suggest you have a look at Saving a Generated Graphic to a PNG or JPEG File.

like image 118
aioobe Avatar answered Sep 24 '22 11:09

aioobe