Results 1 to 5 of 5
Thread: How to crop Images?
- 12-08-2011, 06:16 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 48
- Rep Power
- 0
How to crop Images?
How do you go about cropping images IN AN APPLICATION. I tried using the following code but it only works if placed in an applet. WHICH I DONT WANT.
I have some array of Images.
Throws error:Java Code:Image image = images[i]; Image newImage = createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(1, 1, 21, 20)));
The method createImage(FilteredImageSource) is undefined for the type ImageManager
I imported everything and it just doesnt recognize createImage();
- 12-08-2011, 06:33 AM #2
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
Re: How to crop Images?
If it is a desktop application, you can use this technique I guess...
byte[] imagedata;
image img;
JFileChooser jFile = new JFileChooser();
try
{
JFileChooser fileChooser = new JFileChooser(".");
FileFilter filter1 = new ExtensionFileFilter("JPG and JPEG", new String[] { "JPG", "JPEG" });
fileChooser.setFileFilter(filter1);
int status = fileChooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
} else if (status == JFileChooser.CANCEL_OPTION)
{
}
System.out.println(file.getAbsolutePath().toString ());
if(file!=null)
{
String filePath = file.getAbsolutePath();
ImageIcon icon = new ImageIcon(filePath);
Image image = icon.getImage();
image = createImage(new FilteredImageSource(image.getSource(),new CropImageFilter(20, 20, 80, 80)));
lblImage.setIcon(new javax.swing.ImageIcon(image));
}
}
catch(Exception e)
{
e.PrintStackTrace();
}// TODO add your handling code here:
- 12-08-2011, 06:39 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 48
- Rep Power
- 0
Re: How to crop Images?
Nope it still doesnt recognize the function
- 12-08-2011, 06:50 AM #4
Member
- Join Date
- Dec 2011
- Location
- Kathmandu, Nepal
- Posts
- 15
- Rep Power
- 0
Re: How to crop Images?
if you are working on desktop application, add this class after the end of your java class it works:
class ExtensionFileFilter extends FileFilter {
String description;
String extensions[];
public ExtensionFileFilter(String description, String extension) {
this(description, new String[] { extension });
}
public ExtensionFileFilter(String description, String extensions[]) {
if (description == null) {
this.description = extensions[0];
} else {
this.description = description;
}
this.extensions = (String[]) extensions.clone();
toLower(this.extensions);
}
private void toLower(String array[]) {
for (int i = 0, n = array.length; i < n; i++) {
array[i] = array[i].toLowerCase();
}
}
public String getDescription() {
return description;
}
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
return true;
}
}
}
return false;
}
}
- 12-08-2011, 06:56 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 48
- Rep Power
- 0
Similar Threads
-
help with images
By talia in forum New To JavaReplies: 2Last Post: 12-11-2011, 11:45 PM -
Crop many images out of one image? Please help!
By Alerhau in forum New To JavaReplies: 3Last Post: 09-06-2011, 09:10 PM -
how to mark on 2 images at a time,both images are on different JPanel
By smitharavi in forum AWT / SwingReplies: 0Last Post: 12-16-2010, 05:14 PM -
how to scroll 2 images at a time(synchronisation),both images are on different panels
By smitharavi in forum AWT / SwingReplies: 4Last Post: 12-16-2010, 04:32 PM -
Images
By Witik in forum New To JavaReplies: 7Last Post: 09-13-2010, 01:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks