现在的位置: 首页 > 综合 > 正文

GraphicsMagickUtil图片压缩

2013年01月15日 ⁄ 综合 ⁄ 共 3109字 ⁄ 字号 评论关闭

package com.lenovo.shop.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

public class GraphicsMagickUtil {

  private static final Log log = LogFactoryImpl.getLog(GraphicsMagickUtil.class);
 
   
 /**
  * resize实现
  * @param srcPath 源图片路径
  * @param desPath 目标图片路径
  * @param sw      源图片宽度
  * @param sh      源图片高度
  * @param dw      目标图片宽度
  * @param dh      目标图片高度
  * @throws Exception
  */
 public static void cropImage(String srcPath, String desPath, int dw, int dh) throws Exception {
  log.info("srcPath:"+srcPath+" desPath:"+desPath+" dw:"+dw+" dh:"+dh);
  Map map = getImgInfo(new File(srcPath));
  if(map ==null ) return ;
  int sw = (Integer) map.get("w");
  int sh = (Integer) map.get("h");
  if (sw <= 0 || sh <= 0 || dw <= 0 || dh <= 0) {
   return;
  }
  IMOperation op = new IMOperation();
  op.addImage();
  // 如果源图宽度和高度都小于目标宽高,则仅仅压缩图片
  if ((sw <= dw) && (sh <= dh)) {
   op.resize(sw, sh);
  }else{
   op.resize(dw, dh);// 压缩图片
  }
//  // 如果源图宽度小于目标宽度,并且源图高度大于目标高度
//  if ((sw <= dw) && (sh > dh)) {
//   op.resize(dw, dh);// 压缩图片
////   op.append().crop(sw, dh, 0, (sh - dh) / 2); // 切割图片
//  }
//  // 如果源宽度大于目标宽度,并且源高度小于目标高度
//  if ((sw > dw) && (sh <= dh)) {
//   op.resize(dw, dh);
////   op.append().crop(dw, sh, (sw - dw) / 2, 0);
//  }
//  // 如果源图宽、高都大于目标宽高
//  if (sw > dw && sh > dh) {
//   float ratiow = (float) dw / sw; // 宽度压缩比
//   float ratioh = (float) dh / sh; // 高度压缩比
//   // 宽度压缩比小(等)于高度压缩比(是宽小于高的图片)
//   if (ratiow >= ratioh) {
//    int ch = (int) (ratiow * sh);// 压缩后的图片高度
//    op.resize(dw, dh);// 按目标宽度压缩图片
////    op.append().crop(dw, dh, 0, (ch > dh) ? ((ch - dh) / 2) : 0);// 根据高度居中切割压缩后的图片
//   } else {// (宽大于高的图片)
//    int cw = (int) (ratioh * sw);// 压缩后的图片宽度
//    op.resize(dw, dh);// 按计算的宽度进行压缩
////    op.append().crop(dw, dh, (cw > dw) ? ((cw - dw) / 2) : 0, 0);// 根据宽度居中切割压缩后的图片
//   }
//  }

  op.addImage();
  ConvertCmd convert = new ConvertCmd(true);
  convert.run(op, srcPath, desPath);// BufferedImage or String

 }

 /**
     * 计算图片尺寸大小等信息:w宽、h高、s大小。异常时返回null。
     *
     * @param imgpath 图片路径
     * @return 图片信息map
     */
    public static Map<String, Integer> getImgInfo(File imgfile) {
            Map<String, Integer> map = new HashMap<String, Integer>(3);
            try {
                    FileInputStream fis = new FileInputStream(imgfile);
                    BufferedImage buff = ImageIO.read(imgfile);
                    map.put("w", buff.getWidth() * 1);
                    map.put("h", buff.getHeight() * 1);
                    fis.close();
            } catch (FileNotFoundException e) {
                    System.err.println("所给的图片文件" + imgfile.getPath() + "不存在!计算图片尺寸大小信息失败!");

                    map = null;
            } catch (IOException e) {
                    System.err.println("计算图片" + imgfile.getPath() + "尺寸大小信息失败!");

                    map = null;
            }
            return map;
    }
   
 public static void main(String[] args) {
  try {
   long st= System.currentTimeMillis();
   cropImage("C:/TEMP/aa.jpg", "C:/TEMP/300/aa.jpg", 300, 300);
   
   long se = System.currentTimeMillis();
   System.out.println("time="+(se-st));
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 

}

【上篇】
【下篇】

抱歉!评论已关闭.