wood burning stoves 2.0*
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
Author

Rotate an Image on server side

Tanay Agarwal
Greenhorn

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.
Thanks,

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 {
res.setContentType("image/jpeg");
OutputStream out = res.getOutputStream();
readImage("id", out);
out.flush();
out.close();
}
catch (IOException ioe) {
System.out.println(ioe);
}
catch (Exception e) {
System.out.println(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();
conn.setDoInput(true);
RenderedImage image = null;
if(url != null) {
image = readImage(url);
image = rotateImage(image);
byte[] b = toByteArray(image);
out.write(b);
}
}
catch (Exception e) {
System.out.println(e);
}
}
public RenderedImage readImage(URL ins) throws Exception {
//ImageReader.read(ins);
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();
ImageIO.write(img,"jpeg",out);
return out.toByteArray();
}
public ImageReader getReader() {
ImageReader imr = null;
Iterator it = ImageIO.getImageReadersByFormatName("jpeg");
if(it.hasNext()){
imr = (ImageReader)it.next();
}
return imr;
}
public ImageWriter getWriter() {
ImageWriter imw = null;
Iterator it = ImageIO.getImageWritersByFormatName("jpeg");
if(it.hasNext()){
imw = (ImageWriter)it.next();
}
return imw;
}
}
Mike Fuellbrandt
Greenhorn

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
 
Similar Threads
Getting Different File Sizes from the server.
Requesting the method in the WS hangs
Image in memory?
Rotating buffered image
image file rotation