How to save the cropped image given the original image and the coordinates to crop

Posted on 2008-11-11
Last Modified: 2013-11-24
we are using java 1.3 for developing a j2ee application using struts upload the photo i am using <html:file> tag in the jsp. Only jpegs are accepted.
after uploading the photo, there is a option for cropping. in the front end, i am using Prototype JavaScript Image Cropper UI. a demo is available on net. the link is

using the tool, i can get the coordinates of the selected area.i am sending this coordinates to my action. After this point, i am stuck.

I have the original photo saved on the server. I have the coordinates of the cropped area. now i have to create a new photo(cropped photo) from the original photo. please help me to create the cropped photo and save on save on the server.
I can not use ImageIO since i am using java 1.3 version
can not use Swing since its a J2ee application
Dont want to use applets since the UI is already done and all that i want is to create a new jpeg file to save the cropped image. kindly help.
Question by:divyasindhu
    LVL 86

    Expert Comment

    See Ignore the Swing stuff - it's just for demo purposes

    JAI is usable with JDK >= 1.3.

    Accepted Solution

    JAI is not part of core Java. I do not want to install it. Hence i managed to solve it using BufferedImage. Here is the code which might be useful for other users:

    import java.awt.image.BufferedImage;

    import com.sun.image.codec.jpeg.JPEGCodec;
    import com.sun.image.codec.jpeg.JPEGEncodeParam;
    import com.sun.image.codec.jpeg.JPEGImageDecoder;
    import com.sun.image.codec.jpeg.JPEGImageEncoder;

    FileInputStream in = new FileInputStream("E:\\sample.jpg");
    FileOutputStream op = new FileOutputStream("E:\\cropped.jpg");
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(op);

    BufferedImage bufImage = decoder.decodeAsBufferedImage();
    BufferedImage cropped = bufImage.getSubimage(10,20,40,30);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(cropped);
    encoder.encode(cropped, param);

    LVL 86

    Expert Comment

    It's probably better to use classes that are supported than ones that are not, and are also undocumented

    Author Comment

    I do agree with you. But we can not change our environment. So we had to follow this.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now