• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

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

we are using java 1.3 for developing a j2ee application using struts framework.to 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.
  • 2
  • 2
1 Solution
See https://jaistuff.dev.java.net/code/operators.Crop.html Ignore the Swing stuff - it's just for demo purposes

JAI is usable with JDK >= 1.3.
divyasindhuAuthor Commented:
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 java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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);

It's probably better to use classes that are supported than ones that are not, and are also undocumented
divyasindhuAuthor Commented:
I do agree with you. But we can not change our environment. So we had to follow this.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now