从服务器得到所有图片信息,图片压缩代码

2015/7/26问题集

从服务器得到所有图片信息

servlet

package com.zsz.man;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zsz.develop.UserInfoBean;
import com.zsz.develop.UserinfoDao;
import com.zsz.main.ImageUtil;

/**
 * Servlet implementation class GetAllManServlet
 */
@WebServlet("/GetAllManServlet")
public class GetAllManServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

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


    ServletContext context = getServletContext();
    String contentType = context.getInitParameter("contentType");
    String jsonAction = request.getParameter("action");
    int jsonImageSize =Integer.parseInt(request.getParameter("imageSize")); 
    String jsonName = request.getParameter("name");
    System.out.println("jsonName:"+jsonName);
    String outStr = "";
    Gson gson = new Gson();
    ManDao manDao=new ManDaoImpl();


    if (jsonAction.equals("getAll")) {
        List<ManBean> manList=manDao.getAllMan(jsonName);;
        // 將所有照片縮小
        for (int i = 0; i < manList.size(); i++) {
            ManBean manBean = manList.get(i);
            manBean.setImage(ImageUtil.shrink(manBean.getImage(), jsonImageSize));
        }
        outStr = gson.toJson(manList);
    }

    response.setContentType(contentType);
    PrintWriter out = response.getWriter();
    System.out.println(outStr);
    out.print(outStr);
    out.close();

    }

}

重点在这一段:

  • 判断功能选项
  • 取得实体类集合
  • 循环取出每张图片、并对他进行压缩
  • 把压缩后的数据传递给实体类,并转为json格式输出

    if (jsonAction.equals(“getAll”)) {

        List<ManBean> manList=manDao.getAllMan(jsonName);;
        // 將所有照片縮小
        for (int i = 0; i < manList.size(); i++) {
            ManBean manBean = manList.get(i);
            manBean.setImage(ImageUtil.shrink(manBean.getImage(), jsonImageSize));
        }
        outStr = gson.toJson(manList);
    }
    

    图片压缩代码

调用方法:manBean.setImage(ImageUtil.shrink(manBean.getImage(), jsonImageSize));

package com.zsz.main;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil {

/**
 * 將圖形縮小後回傳,如果發生錯誤就直接回傳原圖, 例如:imageSize值<=1、無法取得圖的寬高、發生Exception
 * 
 * @param srcImageData
 *            來源圖形資料
 * @param scaleSize
 *            欲將圖形縮至多少尺寸以下,50代表將尺寸縮小至50x50以內
 * @return 縮小完畢的圖形資料
 */
public static byte[] shrink(byte[] srcImageData, int scaleSize) {
    ByteArrayInputStream bais = new ByteArrayInputStream(srcImageData);
    // 縮小比例,4代表除以4
    int sampleSize = 1;
    int imageWidth = 0;
    int imageHeight = 0;

    // 如果imageSize為0、負數(代表錯誤)或1(1代表與原圖一樣大小),就直接回傳原圖資料
    if (scaleSize <= 1) {
        return srcImageData;
    }

    try {
        BufferedImage srcBufferedImage = ImageIO.read(bais);
        // 如果無法識別圖檔格式(TYPE_CUSTOM)就回傳TYPE_INT_ARGB;否則回傳既有格式
        int type = srcBufferedImage.getType() == BufferedImage.TYPE_CUSTOM ? BufferedImage.TYPE_INT_RGB
                : srcBufferedImage.getType();
        imageWidth = srcBufferedImage.getWidth();
        imageHeight = srcBufferedImage.getHeight();
        if (imageWidth == 0 || imageHeight == 0) {
            return srcImageData;
        }
        // 只要圖檔較長的一邊超過指定長度(desireSize),就計算欲縮小的比例
        // 並將圖檔寬高都除以欲縮小比例
        int longer = Math.max(imageWidth, imageHeight);
        if (longer > scaleSize) {
            sampleSize = longer / scaleSize;
            imageWidth = srcBufferedImage.getWidth() / sampleSize;
            imageHeight = srcBufferedImage.getHeight() / sampleSize;
        }
        BufferedImage scaledBufferedImage = new BufferedImage(imageWidth,
                imageHeight, type);
        Graphics graphics = scaledBufferedImage.createGraphics();
        graphics.drawImage(srcBufferedImage, 0, 0, imageWidth, imageHeight,
                null);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(scaledBufferedImage, "jpg", baos);
        return baos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return srcImageData;
    }
}
}