May 8, 2008

Image Cropping in java

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

}

May 6, 2008

Calling Url using java servlet

This is the java code servlet block, to ping a servlet on URL.
Here we are using URLConnection class to open a connection.

System.err.println("@@W@Calling Test Servlet:: Called");
URLConnection uc = new URL("http://www.example.com/Test").openConnection();
uc.setDoInput(false);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
uc.setRequestProperty("Content-Type", "application/octet-stream");
ObjectOutputStream out = new ObjectOutputStream(uc.getOutputStream());
HashMap hm = new HashMap();
hm.put("Key1", "Val1");

out.writeObject(hm);
out.flush();
out.close();