Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to edit a pictures

 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9298
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mateen Dar
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic