How do I write a servlet which rotates images?

2019-09-13 18:16发布

Hi just wanted to share this servlet it takes 2 arguments ( img-> image name , rot ->rotation of the images) loads the image from the images directory rotates it and outputs it to the servlet stream

you can find it in the answer below

1条回答
再贱就再见
2楼-- · 2019-09-13 19:11



package at.buchinger.mapdisplay;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RotatedImage
 */
public class RotatedImage extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RotatedImage() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String img = (String) request.getParameter("img");
        String path = getServletContext().getRealPath("image/"+img);


        String r;
        r=request.getParameter("rot");
        if(r==null){
            r="0";
        }
        double rot = Double.parseDouble( r);

        BufferedImage image = ImageIO.read(new File(path));
        AffineTransform tx = new AffineTransform();

        tx.rotate(Math.toRadians(rot), image.getWidth()/2, image.getHeight()/2);

        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        image = op.filter(image, null);



           ServletContext sc = getServletContext();
            String filename = getServletContext().getRealPath(path);

            // Get the MIME type of the image
            String mimeType = sc.getMimeType(filename);
            if (mimeType == null) {
                sc.log("Could not get MIME type of "+filename);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }


            // Set content type
            response.setContentType(mimeType);
            ServletOutputStream out = response.getOutputStream();
            Iterator iter = ImageIO.getImageWritersByMIMEType(mimeType);

            ImageIO.write(image, "png", out);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}



查看更多
登录 后发表回答