如何编写旋转图像的 servlet?

发布于 2024-11-14 08:57:24 字数 114 浏览 2 评论 0原文

您好,只是想分享这个 servlet,它需要 2 个参数( img-> 图像名称,rot -> 图像的旋转)从图像目录加载图像,旋转它并将其输出到 servlet 流,

您可以在回答如下

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2024-11-21 08:57:24


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

}





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

}



~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文