Andrew Thompson 写道:
>> Aki Laukkanen 写道:
>>>> Hi guys!
[quoted text clipped - 19 lines]
>
> Andrew T.
Thanks for you help. I think I have found how to do it.
Toolkit.getDefaultToolkit().getImage("beauty.gif").getGraphics().clip......
then, save the clip to a file. I think that's way. :)
Thank you very much , still.
Andrew Thompson - 06 Sep 2006 10:54 GMT
> Andrew Thompson 写道:
> >> Aki Laukkanen 写道:
[quoted text clipped - 5 lines]
> >>>
> >> Thanks for your hint, but would you please give me a sample.
..
> > The links provided by Aki are a good place to start in
> > learning how to do this yourself.
...
> Thanks for you help. I think I have found how to do it.
> Toolkit.getDefaultToolkit().getImage("beauty.gif").getGraphics().clip......
That is a good start, but I recommend you break it down
into shorter, and more statements (at least at the early
stages). Something more like..
Image image = Toolkit.getDefaultToolkit().getImage("beauty.gif");
// check the image was found
System.out.println("Image: " + image);
Graphics graphics = image.getGraphics();
// ...
> then, save the clip to a file. I think that's way. :)
Be sure to pop back by if (OK - when*) you hit a problem,
(it is also a good idea to read other threads around the group).
* Even experienced Java developers need to ask questions,
occasionally.
> Thank you very much , still.
;-)
Andrew T.
Simon - 06 Sep 2006 12:27 GMT
> Thanks for you help. I think I have found how to do it.
> Toolkit.getDefaultToolkit().getImage("beauty.gif").getGraphics().clip......
> then, save the clip to a file. I think that's way. :)
> Thank you very much , still.
Just to prevent you from some confusion that can easily arise when you learn to
work with Images and Graphics:
- Images can be particularly confusing because you can *draw* images (using
Graphics.drawImage()), and you can draw *onto* images, or rather, on the Image's
Graphics context.
- You can use a Graphics object *only* to paint onto something, e.g. onto an
image, onto the screen, or onto a printer. If the Graphics object belongs to an
Image object, you cannot get any information about the Image from the Graphics
or manipulate the image in any way other than painting on it. There is no
getPixel(x,y) or crop() method in Graphics. The clip-method you mentioned will
therefore not help you with your problem.
- You can use the Toolkit class to load the Image, but I recommend to look into
the ImageIO class or the javax.imageio package in general, which provides
methods for reading and also writing images to files.
- You will find that Image is lacking many methods you are probably expecting.
Most of the time, what you want is a BufferedImage. Note that the ImageIO
methods return BufferedImages. BufferedImage also has the method you want for
cropping.
Hope this helps,
Simon
Anatorian - 08 Sep 2006 10:06 GMT
Thanks for your help, I worked out it.
String picName = "E:\\home\\testphotos\\dipsydancing.jpg";
File f = new File(picName);
ImageInputStream ins = ImageIO.createImageInputStream(f);
Iterator readers = ImageIO.getImageReadersByFormatName("jpg");
ImageReader reader = (ImageReader) readers.next();
reader.setInput(ins);
ImageReadParam param = reader.getDefaultReadParam();
int imageIndex = 0;
int half_width = 0;
int half_height = 0;
half_width = reader.getWidth(imageIndex) / 2;
half_height = reader.getHeight(imageIndex) / 2;
Rectangle rect = new Rectangle(0, 0, half_width, half_height);
param.setSourceRegion(rect);
BufferedImage bi = reader.read(imageIndex, param);
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
File out = new File("e:\\out.jpg");
ImageOutputStream outstream = ImageIO.createImageOutputStream(out);
writer.setOutput(outstream);
writer.write(bi);
Simon 写道:
>> Thanks for you help. I think I have found how to do it.
>> Toolkit.getDefaultToolkit().getImage("beauty.gif").getGraphics().clip......
[quoted text clipped - 26 lines]
> Hope this helps,
> Simon