Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I edit a jpg image through Java?

I have loaded a jpg image in which I want to draw letters and circles, given a x,y coordinate.

I have been trying to figure out the paintIcon of the ImageIcon class

public void paintIcon(Component c,
                      Graphics g,
                      int x,
                      int y)

Does this method allow me to edit jpg images the way I want to? What are supposd to be the Component c and Graphics g paramethers? What would I add to its body to paint circles or letters?

I'm working on Netbeans 6.5, do I have anything builtin for this task (instead of ImageIcon)?

like image 314
andandandand Avatar asked Jun 17 '09 21:06

andandandand


2 Answers

The pure-Java way is to use ImageIO to load the image as a BufferedImage. Then you can call createGraphics() to get a Graphics2D object; you can then draw whatever you want onto the image.

You can use an ImageIcon embedded in a JLabel to do the displaying, and you can add a MouseListener and/or a MouseMotionListener to the JLabel if you're trying to allow the user to edit the image.

like image 60
Michael Myers Avatar answered Sep 28 '22 08:09

Michael Myers


Manipulating images in Java can be achieved by using the Graphics or Graphics2D contexts.

Loading images such as JPEG and PNG can be performed by using the ImageIO class. The ImageIO.read method takes in a File to read in and returns a BufferedImage, which can be used to manipulate the image via its Graphics2D (or the Graphics, its superclass) context.

The Graphics2D context can be used to perform many image drawing and manipulation tasks. For information and examples, the Trail: 2D Graphics of The Java Tutorials would be a very good start.

Following is a simplified example (untested) which will open a JPEG file, and draw some circles and lines (exceptions are ignored):

// Open a JPEG file, load into a BufferedImage.
BufferedImage img = ImageIO.read(new File("image.jpg"));

// Obtain the Graphics2D context associated with the BufferedImage.
Graphics2D g = img.createGraphics();

// Draw on the BufferedImage via the graphics context.
int x = 10;
int y = 10;
int width = 10;
int height = 10;
g.drawOval(x, y, width, height);

g.drawLine(0, 0, 50, 50);

// Clean up -- dispose the graphics context that was created.
g.dispose();

The above code will open an JPEG image, and draw an oval and a line. Once these operations are performed to manipulate the image, the BufferedImage can be handled like any other Image, as it is a subclass of Image.

For example, by creating an ImageIcon using the BufferedImage, one can embed the image into a JButton or JLabel:

JLabel l = new JLabel("Label with image", new ImageIcon(img));
JButton b = new JButton("Button with image", new ImageIcon(img));

The JLabel and JButton both have constructors which take in an ImageIcon, so that can be an easy way to add an image to a Swing component.

like image 34
coobird Avatar answered Sep 28 '22 09:09

coobird