jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Rotate an Image on server side Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Rotate an Image on server side" Watch "Rotate an Image on server side" New topic

Rotate an Image on server side

Tanay Agarwal

Joined: May 07, 2004
Posts: 1
I wrote this code to rotate an image on serverside, here I am using j2sdk1.4.1_04 on Unix environment. I will appreciate your help if someone can take a look on this code and currect it and make it to rotate an Image.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.util.Properties;
import java.net.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.io.*;
import java.io.OutputStream.*;
import java.io.FileInputStream;
import javax.imageio.*;
import com.sun.image.codec.jpeg.*;
import javax.media.jai.*;
import javax.media.jai.JAI.*;
import javax.media.jai.operator.*;
import javax.media.jai.operator.RotateDescriptor.*;
public class ImageTempServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
try {
OutputStream out = res.getOutputStream();
readImage("id", out);
catch (IOException ioe) {
catch (Exception e) {
private void readImage(String id, OutputStream out) {
try {
//URL url = new URL("http://localhost:7001/bea_logo_small.gif");
URL url = new URL("http://rcbdev:7170/Marketing/flamingo_s.jpeg");
URLConnection conn = url.openConnection();
RenderedImage image = null;
if(url != null) {
image = readImage(url);
image = rotateImage(image);
byte[] b = toByteArray(image);
catch (Exception e) {
public RenderedImage readImage(URL ins) throws Exception {
return (RenderedImage)ImageIO.read(ins);
public RenderedOp rotateImage(RenderedImage img) throws Exception {
RotateDescriptor rdesc = new RotateDescriptor();
return rdesc.create(img,1,1,90,0);
public byte[] toByteArray(RenderedImage img) throws Exception {
//ImageWriter imw = getWriter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
return out.toByteArray();
public ImageReader getReader() {
ImageReader imr = null;
Iterator it = ImageIO.getImageReadersByFormatName("jpeg");
imr = (ImageReader)it.next();
return imr;
public ImageWriter getWriter() {
ImageWriter imw = null;
Iterator it = ImageIO.getImageWritersByFormatName("jpeg");
imw = (ImageWriter)it.next();
return imw;
Mike Fuellbrandt

Joined: Jan 17, 2002
Posts: 14
I don't know how to do this using JAI if that's a requirement, but you can try this...
(I've changed the return type of your function to non JAI-specific return type)

If the null gives you grief, you may have to define an image to pass in.
Something like BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_BYTE_INDEXED);

Mike Fuellbrandt
[ May 10, 2004: Message edited by: Mike Fuellbrandt ]
I agree. Here's the link: http://aspose.com/file-tools
subject: Rotate an Image on server side
It's not a secret anymore!