*
The moose likes Java in General and the fly likes how to edit a pictures Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to edit a pictures" Watch "how to edit a pictures" New topic
Author

how to edit a pictures

Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
hi i am currently working on a small project that must open a pictures , edit it like draw lines or circles on it and save the picture with the new lines and circles. thanks .
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Start with the Java Tutorial chapter on Performing Custom Painting, then read the 2D Graphics Tutorial. That should cover rendering and drawing. For reading and writing, consult the Image IO documentation.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
hi i tried this. what u said. buthe line appears on the image but when i save it only the pictures is saved. following is the code i tried. desperately nead help

Image img = getToolkit().getImage("c:\\a.jpg");
try
{
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(img, 0);
tracker.waitForID(0);
}
catch (Exception e) {}

int iw = img.getWidth(this);
int ih = img.getHeight(this);
bi = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
big.drawImage(img,0,0,this);
big.setColor(Color.white);
big.setStroke(new BasicStroke(5.0f));
big.drawLine(10, 10, bw-10, bh-10);
try
{
Iterator writers = ImageIO.getImageWritersByFormatName("png");
imageWriter writer = (ImageWriter)writers.next();


File f = new File("c:\\myimage.png");

ImageOutputStream ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
writer.write(bi);
}
catch(Exception e)
{
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, let's see. You don't close the ImageOutputStream; not closing a file is always a bad thing. You have empty catch blocks, so that errors will go unreported. And you use a few variables (bw, bh) which aren't defined anywhere.

But I've never used the imageio API, and I saw this as a good opportunity to learn. So I took your code, wrapped it in a minmal class, changed bw and bh to iw and ih, respectively, added the "close" call, put some printStackTraces() into the catch blocks, and tried it. On my first few attempts, I got corrupted PNG files, until I realized I was killing the program before the whole file was written out. I put in a print statement to report when the file writing was done, after the file was closed. I then found that this worked exactly as you'd hope, drawing a white diagonal line across the image.

So I think you're very close; just take care of these details, and it should work for you too.


[Jess in Action][AskingGoodQuestions]
Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
hi .. thnaks it worked. but one more probkem the original file size is 122 kb. but the png file format is 0.98 mb .. huge file. carnt be sent to a mobile phone. is is due to my code, or is png a heavy format. thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

PNG has lossless compression, while JPEG has lossy compression. In general, a PNG of a given photographic image will always be larger -- often much larger -- than a JPEG.
Mateen Dar
Ranch Hand

Joined: Jan 14, 2005
Posts: 55
i have to questions:
1) the code above has to run on a web server. is it possible. ( i am confused about the this reference for the JFrame, in the media tracker and image height and width functions).or can i create a class derived from from jcomponent, and invoke that class's object from within my servlet passing the image path to be drawn on. will that work.

2) the picture is then sent to a mobile phone. which can only read png files. so how can i compress that png (or carnt i) . sorry for bothering u .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to edit a pictures
 
Similar Threads
Free UML diagram Tool
WA #1.....word association
GUI advice needed
moussaoui attempts guilty plea
Total search fails