//This is logic to crop image in java , here the code for how to crop image in java only. not this code runs completely
public void cropImage() {
byte[] bytesOut = null;
try {
int x1 = 0, y1 = 0, cw = 0, ch = 0;
HttpServletRequest request = getRequest();
String params = request.getParameter("dimensions");
String str[] = params.split(",");
HttpSession session = request.getSession();
String fname = (String) session.getAttribute("fileupload_name");
if (StringUtils.isNotBlank(str[0])) {
x1 = str[0].equals("") ? 50 : Integer.parseInt(str[0]);
y1 = str[1].equals("") ? 50 : Integer.parseInt(str[1]);
cw = str[2].equals("") ? 50 : Integer.parseInt(str[2]);
ch = str[3].equals("") ? 50 : Integer.parseInt(str[3]);
}
byte[] bytes = (byte[]) session .getAttribute("fileupload_bytes");
File tfile = new File(fname);
FileOutputStream fout = new FileOutputStream(tfile);
fout.write(bytes);
fout.close();
try{
BufferedImage bimage = ImageIO.read(tfile);
BufferedImage bi = null;
bi = bimage.getSubimage(x1, y1, cw, ch);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String format = fname.substring(fname.lastIndexOf(".") + 1);
if (bi != null) {
ImageIO.write(bi, format, baos);
}
bytesOut = baos.toByteArray();
}catch(Exception exp){
log.info("Invalid image Width or Height");
}
} catch (Throwable th) {
ExceptionTrapSender.sendException(th);
}
}