Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

clip image using java

Thread view: 
Anatorian - 06 Sep 2006 09:44 GMT
Hi guys!
I want to cut an picture into 25 (5x5) pieces. Every small picture is
same large.
Aki Laukkanen - 06 Sep 2006 09:54 GMT
> Hi guys!
> I want to cut an picture into 25 (5x5) pieces. Every small picture is
> same large.

<http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html>
<http://java.sun.com/docs/books/tutorial/2d/TOC.html>

Signature

-Aki Laukkanen

Anatorian - 06 Sep 2006 10:06 GMT
Aki Laukkanen 写道:
>> Hi guys!
>> I want to cut an picture into 25 (5x5) pieces. Every small picture is
>> same large.
>
> <http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html>
> <http://java.sun.com/docs/books/tutorial/2d/TOC.html>

Thanks for your hint, but would you please give me a sample. Suppose the
 picture file's name is "beauty.gif", and I want to cut 100x100 area
right on the left top corner, and save it to file "clip.gif".
Thanks!
Andrew Thompson - 06 Sep 2006 10:29 GMT
> Aki Laukkanen 写道:
> >> Hi guys!
[quoted text clipped - 5 lines]
> >
> Thanks for your hint, but would you please give me a sample.

You will not get very far in learning Java if you expect
people here to spoon-feed the information to you.

Partly because you would not learn much from that,
but mostly because most people helping, are not
prepared to provide that level of help.

The links provided by Aki are a good place to start in
learning how to do this yourself.

If you run into problems and have specific questions,
any number of people will be willing to help resolve them.

Andrew T.
Anatorian - 06 Sep 2006 10:39 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.