java Jmagick 图片缩略图处理

1、下载Jmagick和ImageMagick ,版本要一致,可以到http://downloads.jmagick.org/去下载就可以了,里面很全面的。

2、安装ImageMagick,并配置path。

3、下载的jmagick包含jmagick.jar,jmagick.dll文件,jmagick.dll需要copy到 windows目录下和jdk 的安装目录下,之后重启计算机。

4、还要注意如果部署到web应用,你在使用的class里面需要

      System.setProperty("jmagick.systemclassloader","no");

要不然会报出UnsatisfiedLinkError: no JMagick in java.library.path.

import java.awt.Dimension;
import java.io.IOException;

import magick.CompositeOperator;
import magick.CompressionType;
import magick.DrawInfo;
import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;
import magick.PixelPacket;
import magick.PreviewType;

public class ImageMagickUtil {

 /**
  * @param args
  * @throws IOException
  * @throws MagickException
  */
 public static void main(String[] args) throws IOException, MagickException {
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 9);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 8);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 7);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 6);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 5);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 4);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 3);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 2);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 1);
  // pressImage("E://1.jpg", "F://workspace//MZB//UI//Upload//sx//Image//WaterMarkImage.png", 0);
  // PressText("E://1.jpg", "民政部", "", "宋体", 1, 1, 1, 1);
  scaleRateImageFile("E://1.jpg", "E://11.jpg", 200, 150);
 }

 public static Dimension getDimension(String fileName) throws MagickException {
  System.setProperty("jmagick.systemclassloader", "no");
  ImageInfo info = null;
  MagickImage fromImage = null;
  Dimension dim = null;
  try {
   info = new ImageInfo(fileName);
   fromImage = new MagickImage(info);
   dim = fromImage.getDimension();
  } finally {
   if (fromImage != null) {
    fromImage.destroyImages();
   }
  }
  return dim ;
 }

 public static void scaleRateImageFile(String fromFileName, String toFileName, int toWidth, int toHeight)
   throws MagickException {
  System.out.println("ImageMagickUtil正在生成缩略图--" + toFileName);
  System.setProperty("jmagick.systemclassloader", "no");
  ImageInfo info = null;
  MagickImage fromImage = null;
  try {
   info = new ImageInfo(fromFileName);

   fromImage = new MagickImage(info);
   Dimension dim = fromImage.getDimension();
   double w = dim.getWidth();
   double h = dim.getHeight();
   if (w < toWidth && h < toHeight) {
    FileUtil.copy(fromFileName, toFileName);
    return ;
   }
   if (toWidth == 0) {
    if(h<=toHeight){
     FileUtil.copy(fromFileName, toFileName);
    }else{
     scaleRateImageFile(fromFileName, toFileName, toHeight / h);
    }
    return;
   } else if (toHeight == 0) {
    if(w<=toWidth){
     FileUtil.copy(fromFileName, toFileName);
    }else{
     scaleRateImageFile(fromFileName, toFileName, toWidth / w);
    }
    return;
   }
   if (toWidth / w > toHeight / h) {
    scaleRateImageFile(fromFileName, toFileName, toHeight / h);
   } else {
    scaleRateImageFile(fromFileName, toFileName, toWidth / w);
   }
  } finally {
   if (fromImage != null) {
    fromImage.destroyImages();
   }
  }
 }

 public static void scaleRateImageFile(String fromFileName, String toFileName, double rate) throws MagickException {
  System.out.println("ImageMagickUtil正在生成缩略图--" + toFileName);
  System.setProperty("jmagick.systemclassloader", "no");
  MagickImage fromImage = null;
  try {
   ImageInfo info = new ImageInfo(fromFileName);
   fromImage = new MagickImage(info);
   Dimension dim = fromImage.getDimension();
   double w = dim.getWidth();
   double h = dim.getHeight();
   MagickImage toImage = fromImage.scaleImage((int) (w * rate), (int) (h * rate));// 缩放操作
   toImage.setFileName(toFileName);// 设置输出的文件名
   toImage.writeImage(new ImageInfo(fromImage.getFileName())); // 保存
  } finally {
   if (fromImage != null) {
    fromImage.destroyImages();
   }
  }
  System.out.println("完毕");
 }

 public static void scaleFixedImageFile(String fromFileName, String toFileName, int toWidth, int toHeight)
   throws MagickException {
  System.out.print("ImageMagickUtil正在生成缩略图--" + toFileName);
  System.setProperty("jmagick.systemclassloader", "no");// 这个没什么好说的,照办就是了
  MagickImage fromImage = null;
  MagickImage toImage = null;
  try {
   ImageInfo info = new ImageInfo(fromFileName);
   fromImage = new MagickImage(info);
   toImage = fromImage.scaleImage(toWidth, toHeight);// 缩放操作
   toImage.setFileName(toFileName);// 设置输出的文件名
   toImage.writeImage(info); // 保存
  } finally {
   if (fromImage != null) {
    fromImage.destroyImages();
   }
   if (toImage != null) {
    toImage.destroyImages();
   }
  }
  System.out.println("完毕");
 }

 public static void pressText(String filePath, String pressText, int color, int fontSize, int position) {
  System.out.print("ImageMagickUtil正在打文字水印--" + filePath);
  System.setProperty("jmagick.systemclassloader", "no");
  MagickImage image = null;
  try {
   ImageInfo info = new ImageInfo(filePath);

   if (filePath.toUpperCase().endsWith("JPG") || filePath.toUpperCase().endsWith("JPEG")) {
    info.setCompression(CompressionType.JPEGCompression); // 压缩类别为JPEG格式
    info.setPreviewType(PreviewType.JPEGPreview); // 预览格式为JPEG格式
    info.setQuality(95);
   }

   image = new MagickImage(info);
   Dimension dim = image.getDimension();
   int wideth = (int) dim.getWidth();
   int height = (int) dim.getHeight();
   if (wideth <= 300 && height <= 300) {
    return;
   }
   DrawInfo drawInfo = new DrawInfo(info);

   drawInfo.setFill(PixelPacket.queryColorDatabase("white"));
   // drawInfo.setUnderColor(PixelPacket.queryColorDatabase("white"));
   drawInfo.setOpacity(0);
   drawInfo.setPointsize(100);

   // drawInfo.setFont("Arial"); // 英文使用此字体也可

   // // 注意这里解决中文字体问题,有以下两行才可正常显示
   String fontPath = "C:/WINDOWS/Fonts/SIMSUN.TTC";
   // String fontPath = "SIMSUN.TTC";
   drawInfo.setFont(fontPath);
   drawInfo.setTextAntialias(true);

   // Step 3: Writing The Text
   drawInfo.setText(pressText);
   drawInfo.setGeometry("+1500+1000");

   // Step 4: Annotating
   image.annotateImage(drawInfo);

   // Step 5: Writing the new file
   image.setFileName(filePath);
   image.writeImage(info);
   image.destroyImages();
   image = null;

  } catch (MagickException e) {
   e.printStackTrace();
  } finally {
   if (image != null) {
    image.destroyImages();
   }
  }
  System.out.println("完毕");
 }

 public final static void pressImage(String targetImg, String pressImg, int position) {
  System.out.print("ImageMagickUtil正在打图片水印--" + targetImg);
  System.setProperty("jmagick.systemclassloader", "no");
  MagickImage image = null;
  MagickImage press = null;
  try {
   ImageInfo imageInfo = new ImageInfo(targetImg);
   image = new MagickImage(imageInfo);
   Dimension dim = image.getDimension();
   int wideth = (int) dim.getWidth();
   int height = (int) dim.getHeight();
   if (wideth <= 300 && height <= 300) {
    return;
   }
   press = new MagickImage(new ImageInfo(pressImg));
   dim = press.getDimension();
   int wideth_press = (int) dim.getWidth();
   int height_press = (int) dim.getHeight();
   int x = 0, y = 0;
   int bianju = 20;
   int[][][] positions = new int[][][] {
     { { bianju, bianju }, { (wideth - wideth_press) / 2, bianju },
       { wideth - wideth_press - bianju, bianju } },
     { { bianju, (height - height_press) / 2 },
       { (wideth - wideth_press) / 2, (height - height_press) / 2 },
       { wideth - wideth_press - bianju, (height - height_press) / 2 } },
     { { bianju, height - height_press - bianju },
       { (wideth - wideth_press) / 2, height - height_press - bianju },
       { wideth - wideth_press - bianju, height - height_press - bianju } } };
   if (position == 0) {
    position = NumberUtil.getRandomInt(9)+1;
   }
   x = positions[(position - 1) / 3][(position - 1) % 3][0];
   y = positions[(position - 1) / 3][(position - 1) % 3][1];

   image.compositeImage(CompositeOperator.AtopCompositeOp, press, x, y);
   image.setFileName(targetImg);
   image.writeImage(imageInfo);
  } catch (MagickException e) {
   e.printStackTrace();
  } finally {
   if (image != null) {
    image.destroyImages();
   }
   if (press != null) {
    press.destroyImages();
   }
  }
  System.out.println("完毕");
 }
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值