Skip to content

Instantly share code, notes, and snippets.

@madan712
Created September 2, 2012 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madan712/3602317 to your computer and use it in GitHub Desktop.
Save madan712/3602317 to your computer and use it in GitHub Desktop.
This is the JSP which gets called when form is submitted from image.jsp
<%@ page import="java.awt.image.BufferedImage"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.IOException"%>
<%@ page import="javax.imageio.ImageIO"%>
<%!public String cropImage(int x, int y, int w, int h) {
String absolutePath = "C:/apache-tomcat-5.5.26/webapps/JavaXp/";
try {
BufferedImage originalImgage = ImageIO.read(new File(absolutePath+"pool.jpg"));
System.out.println("Original image dimension: "+originalImgage.getWidth()+"x"+originalImgage.getHeight());
BufferedImage SubImgage = originalImgage.getSubimage(x, y, w, h);
System.out.println("Cropped image dimension: "+SubImgage.getWidth()+"x"+SubImgage.getHeight());
File outputfile = new File(absolutePath + "croppedImage.jpg");
ImageIO.write(SubImgage, "jpg", outputfile);
System.out.println("Image cropped successfully: "+outputfile.getPath());
return outputfile.getName();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}%>
<%
int x = Integer.parseInt(request.getParameter("x"));
int y = Integer.parseInt(request.getParameter("y"));
int w = Integer.parseInt(request.getParameter("w"));
int h = Integer.parseInt(request.getParameter("h"));
String croppedImage = cropImage(x, y, w, h);
%>
Cropped image : <%= croppedImage %>
<br />
<img src="<%= croppedImage %>" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment