1
回答
使用JMagick压缩图片大小尺寸
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我们经常需要实现图片上传的功能,但是光是上传图片可能还是远远不够的,我们必须对我们上传的图片进行处理,改变大小等等。JMagick是 ImageMagick提供的一套使用Java调用ImageMagick的API接口,功能非常强大,下面介绍如果使用这个API处理图片的大小。引入 JMagick需要的类库:

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

我们需要把图片保存为 两个尺寸,这里要提前定义两种常量:

public static String SIZENAME_LARGE = "large";
public static String SIZENAME_SMALL = "small";

下面介绍如何使用JMagick,里面用到的ImageUtil稍后会介绍,FileUtil是操作文件的工具类,这里就暂时不介绍了:

MagickImage source = ImageUtil.getMagickImage("image file name");
Map map = processHead(source);
System.out.ptineln(map.get(SIZENAME_LARGE));
System.out.ptineln(map.get(SIZENAME_SMALL));


ImageUtil.getMagickImage(byte[] byte)

public static MagickImage getMagickImage(byte[] byte) throws MagickException {
ImageInfo info = new ImageInfo();
return new MagickImage(info, byte);
}





processHead(MagickImage source)


private Map processHead(MagickImage source) throws Exception {
  MagickImage regulate = null;
  MagickImage large = null;
  MagickImage small = null;

  //保存图片的临时目录
  String tempPath = "....";
  //随即生成一个文件名,真是情况生成的目录可能更复杂
  String fn = FileUtil.getInstance().getRandName();
  String largeName = SIZENAME_LARGE + "_" + fn + ".jpg";
  String smallName = SIZENAME_SMALL + "_" + fn + ".jpg";

  try {
    //改变图片大小
    regulate = ImageUtil.regulate(source);
    large = ImageUtil.resizePhoto(regulate, tempPath + largeName, 500, 750);
    small = ImageUtil.resizePhoto(large, tempPath + smallName, 90, 120);
    byte[] largeByte = large.imageToBlob(new ImageInfo());
    byte[] smallByte = small.imageToBlob(new ImageInfo());

    //保存图片的目录
    String uploadPath = "....";
    //写文件到目录
    FileUtil.getInstance().writeFile(uploadPath + largeName, largeByte);
    FileUtil.getInstance().writeFile(uploadPath + smallName, smallByte);

    //返回图片地址
    Map map = new HashMap();
    map.put(SIZENAME_LARGE, "/" + dir + largeName);
    map.put(SIZENAME_SMALL, "/" + dir + smallName);
    return map;
  } catch (Exception e) {
    throw e;
  } finally {
    if (small != null)
      small.destroyImages();
    if (large != null)
      large.destroyImages();
    if (regulate != null)
      regulate.destroyImages();
    }
  }
}



ImageUtil.regulate(MagickImage source)

 

public static MagickImage regulate(MagickImage source) throws MagickException {
  int width = (int) source.getDimension().getWidth();
  int height = (int) source.getDimension().getHeight();
  if ((height + 0.0) / width > 1.5) {
    MagickImage image = null;
    MagickImage scaled = null;
    try {
      int newHeight = (int) Math.round(width * 1.5);
      Rectangle rect = new Rectangle(0, (height - newHeight) / 2, width, newHeight);
      ImageInfo info = null;
      info = new ImageInfo();

      scaled = source.cropImage(rect);
      return scaled;
    } finally {
      if (source != null) {
        source.destroyImages();
      }
    }
  } else {
    return source;
  }
}

 

ImageUtil.resizePhoto(MagickImage source, String destPathName, int maxWidth, int maxHeight)

 

public static MagickImage resizePhotoStep(MagickImage source, String destPathName, int maxWidth, int maxHeight) throws MagickException {
  int width = 0;
  int height = 0;
  boolean change = true;
  width = (int) source.getDimension().getWidth();
  height = (int) source.getDimension().getHeight();
  if (maxWidth > width && maxHeight > height) {
    change = false;
  } else {
    if (width > 0 && height > 0) {
      if (height / width > maxHeight / maxWidth) {
        width = width * maxHeight / height;
        height = maxHeight;
      } else {
        height = height * maxWidth / width;
        width = maxWidth;
      }
    }
  }

  MagickImage scaled = null;
  scaled = source.scaleImage(width, height);
  scaled.setFileName(destPathName);
  return scaled;
}

举报
红薯
发帖于9年前 1回/2K+阅
顶部