This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes How do I display JPEG, crop it, display cropped JPEG and undo crop. 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 » Other JSE/JEE APIs
Bookmark "How do I display JPEG, crop it, display cropped JPEG and undo crop." Watch "How do I display JPEG, crop it, display cropped JPEG and undo crop." New topic
Author

How do I display JPEG, crop it, display cropped JPEG and undo crop.

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
I need to learn how to display a JPEG, crop it using a mouse, re display the cropped JPEG and undo the crop. Can anyone refer me to example code or a book or a tutorial that will teach me this. I would like to use JAI.
Thanks,
Warren Bell
Netricks
Darrin Cartwright
Ranch Hand

Joined: Dec 27, 2002
Posts: 107
Hey Warren,
There is a good discussion of this in the book Java 2D Graphics from O'Reilly. I think I can give you the gist of it:
// you have to import JPEG support along
// with the usual suspects
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import java.io.*
// image reference
private BufferedImage myImage;
// a method to import a certain JPEG image
// might look like this
private void loadArrowImage(){
try
{
String filename = "MyPicture.jpg";
FileInputStream in = new FileInputStream( filename );
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder( in );
myImage = decoder.decodeAsBufferedImage();
in.close();
}
catch( ImageFormatException ife )
{
System.out.println("Image source file invalid.");
}
catch( FileNotFoundException fnfe )
{
System.out.println("Could not locate image file.");
}
catch( IOException ioe )
{
System.out.println("Error reading image file.");
}
}

// you display it by passing it to the graphics object
public void paint(Graphics g) {
// cast to Graphics2D object
Graphics2D g2 = (Graphics2D)g;
// call a drawImage method
g2.drawImage( myImage, 0, 0, null);
// or whatever works best for you
}
As far as cropping the image, you just create a Shape object to use as a clipping shape. The call looks like:
// in paint() method
g2.clip( myClippingShape );
g2.drawImage( myImage, 0, 0, null );
Hope this helps!
Darrin


Life is good on the Ponderosa,<br />but mind where you step.
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
Thanks for the reply. I read up on it and basically did what you explained.
Warren Bell
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I display JPEG, crop it, display cropped JPEG and undo crop.
 
Similar Threads
How do I display JPEG, crop it, display cropped JPEG and undo crop.
Image cropping in jsp
Crop an image in Java
jpeg in a Jpanel
How to crop image in java