[Servlet]利用JMagick即時產生縮圖

剛好有個網友提到如何利用Servlet來產生縮圖,利用Servlet來產生而不存放成檔案,這種及時取得圖像的方法,比較不建議使用在需要大量輸出圖的狀況下,這樣子在記憶體的使用上會很重。

而為什麼使用ImageMagick來做縮圖呢?其實Java本身也可來製做縮圖,可是因為本身浮點運算的問題,效果並沒有很好,雖然麻煩了點,還是外掛dll來使用ImagicMagick會來的好。

JMagick ImageMagick等介紹及下載請參考:

JMagick-Java open source free影像壓縮

範例的結果:

jmagick1.png

利用imageToBlob這個函數來產生byte array做為輸出OutputStream

ps.記得把檔案先放在c糟,請自行下載範例圖

header.jpg
package yslifes.yku;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

public class JMagickScale {
    public static byte[] getCompressByte(String RealPathName,
            int CompressWidth, int CompressHeight) {
        byte[] data = new byte[1];
        if (System.getProperty("jmagick.systemclassloader") == null) {
            System.setProperty("jmagick.systemclassloader", "no");
        }
        try {
            // 來源路徑
            java.io.File source = new java.io.File(RealPathName);
            if (!source.exists())// 不存在的話則做縮圖
            {
                System.out.println("檔案不存在!!!");
                return data;
            }
            // System.out.println(URLPath+to.getName());
            // 否則則縮圖
            System.out.println("開始縮圖");
            ImageInfo info = new ImageInfo(source.getAbsolutePath());
            MagickImage image = new MagickImage(info);
            // resize image
            MagickImage scaleImg = image.scaleImage(CompressWidth,
                    CompressHeight);

            data = scaleImg.imageToBlob(info);

            System.out.println("縮圖成功!!" + CompressWidth + " " + CompressHeight);
            return data;
        } catch (MagickException e) {
            System.out.println("縮圖失敗:" + CompressWidth + " " + CompressHeight);

        }
        return data;
    }
}

Servlet的部份只是把參數做處理,而呼叫JMagickScale來做壓縮

package yslifes.yku;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: Image
 *
 */
public class Image extends javax.servlet.http.HttpServlet implements
        javax.servlet.Servlet {
    /*
     * (non-Java-doc)
     *
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */
    public Image() {
        super();
    }

    /*
     * (non-Java-doc)
     *
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
     * HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /*
     * (non-Java-doc)
     *
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
     * HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String FileName = request.getParameter("f");
        java.io.OutputStream ops = response.getOutputStream();
        if (FileName == null || FileName.trim().length() == 0) {
            ops.write(new byte[1]);
        } else {
            ops.write(yslifes.yku.JMagickScale.getCompressByte(
                    "c:/" + FileName, 500, 60));
        }
    }
}

原始碼下載:

如果傳如的值檔案不存在時會有下面結果:

jmagick2.png

One thought to “[Servlet]利用JMagick即時產生縮圖”

發表迴響