Results 1 to 5 of 5
  1. #1
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default 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.

    Java Code:
    Image image = images[i];
    			Image newImage = createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(1, 1, 21, 20)));
    Throws error:

    The method createImage(FilteredImageSource) is undefined for the type ImageManager

    I imported everything and it just doesnt recognize createImage();

  2. #2
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default 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:

  3. #3
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to crop Images?

    Nope it still doesnt recognize the function

  4. #4
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default 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;
    }
    }

  5. #5
    sgthale is offline Member
    Join Date
    Apr 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to crop Images?

    Doesnt work for me. What you wrote is just the handler for loading images with extensions. ARE YOU SURE YOU ARE NOT RUNNING AN APPLET? I want an application

Similar Threads

  1. help with images
    By talia in forum New To Java
    Replies: 2
    Last Post: 12-11-2011, 11:45 PM
  2. Crop many images out of one image? Please help!
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 09-06-2011, 09:10 PM
  3. Replies: 0
    Last Post: 12-16-2010, 05:14 PM
  4. Replies: 4
    Last Post: 12-16-2010, 04:32 PM
  5. Images
    By Witik in forum New To Java
    Replies: 7
    Last Post: 09-13-2010, 01:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •